doesSessionExist static method

Future<bool> doesSessionExist()

Use this function to verify if a users session is valid

Implementation

static Future<bool> doesSessionExist() async {
  Map<String, dynamic>? tokenInfo = await FrontToken.getToken();

  logDebugMessage('SuperTokens.doesSessionExist: Got token info: ${jsonEncode(tokenInfo)}');
  if (tokenInfo == null) {
    logDebugMessage('SuperTokens.doesSessionExist: token info is null');
    return false;
  }

  int now = DateTime.now().millisecondsSinceEpoch;
  int accessTokenExpiry = tokenInfo["ate"];

  if (accessTokenExpiry != null && accessTokenExpiry < now) {
    logDebugMessage('SuperTokens.doesSessionExist: access token has expired');
    LocalSessionState preRequestLocalSessionState =
        await SuperTokensUtils.getLocalSessionState();

    var resp =
        await Client.onUnauthorisedResponse(preRequestLocalSessionState);

    if (resp.error != null) {
      // Here we dont throw the error and instead return false, because
      // otherwise users would have to use a try catch just to call doesSessionExist
      return false;
    }

    return resp.status == UnauthorisedStatus.RETRY;
  }

  return true;
}