getFreshAuthTokens method

Future<TokenResponse?> getFreshAuthTokens({
  1. required String clientID,
  2. required AuthEndpointsData authEndpointsData,
})

Implementation

Future<TokenResponse?> getFreshAuthTokens({
  required String clientID,
  required AuthEndpointsData authEndpointsData,
}) async {
  final String? refreshToken = await securedStorage.getRefreshToken();

  if (refreshToken != null) {
    try {
      final TokenRequest tokenRequest = TokenRequest(
        clientID,
        authEndpointsData.redirectUrl,
        discoveryUrl: authEndpointsData.openIDMetadataEndpoint,
        refreshToken: refreshToken,
        scopes: authEndpointsData.scopes,
      );

      final TokenResponse response = await _flutterAppAuth
          .token(tokenRequest)
          .timeout(
            const Duration(seconds: 10),
            onTimeout: () {
              throw TimeoutException(
                message: 'Timeout on getting fresh auth tokens',
              );
            },
          );

      return response;
    } catch (e) {
      await securedStorage.deleteAuthSessionTokensAndCredentials();
    }
  }
  return null;
}