refresh method
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) {
final refreshTime = _identity!.expiresAt
.difference(DateTime.now().subtract(Duration(minutes: 1)));
_autoRenewTimer = Future.delayed(refreshTime, refresh);
}
if (raiseEvents) _raiseEvent(AuthEvent(AuthEventTypes.Refresh));
return true;
} on Exception catch (e) {
if (this._identity != null) {
await OpenIdIdentity.clear();
this._identity = null;
_raiseEvent(AuthEvent(AuthEventTypes.Error, message: e.toString()));
}
return false;
} finally {
_refreshing = false;
}
}