getAccessTokenPayloadSecurely static method

Future<Map<String, dynamic>> getAccessTokenPayloadSecurely()

Implementation

static Future<Map<String, dynamic>> getAccessTokenPayloadSecurely() async {
  Map<String, dynamic>? frontToken = await FrontToken.getToken();
  if (frontToken == null)
    throw SuperTokensException("Session does not exist");
  int accessTokenExpiry = frontToken['ate'] as int;
  Map<String, dynamic> userPayload = frontToken['up'] as Map<String, dynamic>;

  if (accessTokenExpiry < DateTime.now().millisecondsSinceEpoch) {
    logDebugMessage('SuperTokens.getAccessTokenPayloadSecurely: access token has expired, trying to refresh');
    bool retry = await SuperTokens.attemptRefreshingSession();

    if (retry) {
      logDebugMessage('SuperTokens.getAccessTokenPayloadSecurely: Retry was successful, extracting payload');
      return getAccessTokenPayloadSecurely();
    }
    else
      throw SuperTokensException("Could not refresh session");
  }
  return userPayload;
}