refresh method

Future<bool> refresh({
  1. bool raiseEvents = true,
})

Implementation

Future<bool> refresh({bool raiseEvents = true}) async {
  if (!webUseRefreshTokens) {
    //Web has a special case where it will use a hidden iframe. This just returns true because the iframe does it.
    //In this case we simply load from storage because the web implementation just stores the new values in storage for us.
    _identity = await OpenIdIdentity.load();
    return true;
  }

  while (_refreshing) await Future<void>.delayed(Duration(milliseconds: 200));

  try {
    _refreshing = true;
    if (_autoRenewTimer != null) _autoRenewTimer = null;

    if (this._identity == null ||
        this._identity!.refreshToken == null ||
        this._identity!.refreshToken!.isEmpty) return false;

    await _verifyDiscoveryDocument();

    final response = await OpenIdConnect.refreshToken(
      request: RefreshRequest(
        clientId: clientId,
        clientSecret: clientSecret,
        scopes: _getScopes(scopes),
        refreshToken: _identity!.refreshToken!,
        configuration: configuration!,
      ),
    );

    await _completeLogin(response);

    if (autoRefresh) {
      var refreshTime = _identity!.expiresAt.difference(DateTime.now().toUtc());
      refreshTime -= Duration(minutes: 1);

      _autoRenewTimer = Future.delayed(refreshTime, refresh);
    }

    if (raiseEvents) _raiseEvent(AuthEvent(AuthEventTypes.Refresh));

    return true;
  } on Exception catch (e) {
    clearIdentity();
    _raiseEvent(AuthEvent(AuthEventTypes.Error, message: e.toString()));
    return false;
  } finally {
    _refreshing = false;
  }
}