signOut static method

Future<void> signOut(
  1. {dynamic completionHandler(
    1. Exception?
    )?}
)

Implementation

static Future<void> signOut({Function(Exception?)? completionHandler}) async {
  logDebugMessage('SuperTokens.signOut: Signing out user');
  if (!(await doesSessionExist())) {
    logDebugMessage('SuperTokens.signOut: Session does not exist');
    SuperTokens.config.eventHandler(Eventype.SIGN_OUT);
    if (completionHandler != null) {
      logDebugMessage('SuperTokens.signOut: Calling completionHandler');
      completionHandler(null);
    }
    return;
  }

  Uri uri;
  try {
    uri = Uri.parse(SuperTokens.signOutUrl);
  } catch (e) {
    if (completionHandler != null) {
      completionHandler(SuperTokensException(
          "Please provide a valid apiDomain and apiBasePath"));
    }
    return;
  }

  logDebugMessage('SuperTokens.signOut: Using signOutUrl: ${uri}');
  http.Request signOut = http.Request('post', uri);
  signOut = SuperTokens.config.preAPIHook(APIAction.SIGN_OUT, signOut);

  late http.StreamedResponse resp;

  try {
    Client client = Client();
    resp = await client.send(signOut);
    if (resp.statusCode >= 300) {
      if (completionHandler != null) {
        completionHandler(SuperTokensException(
            "Sign out failed with response code ${resp.statusCode}"));
      }
      return;
    }
    http.Response response = await http.Response.fromStream(resp);
    SuperTokens.config.postAPIHook(APIAction.SIGN_OUT, signOut, response);

    var dataStr = response.body;
    Map<String, dynamic> data = jsonDecode(dataStr);

    if (data['status'] == 'GENERAL_ERROR') {
      if (completionHandler != null) {
        completionHandler(SuperTokensGeneralError(data['message']));
      }
    }
  } catch (e) {
    if (completionHandler != null) {
      completionHandler(SuperTokensException("Invalid sign out resopnse"));
    }
    return;
  }
}