signInWithCode method

Future<UserSessionResult> signInWithCode(
  1. String phone,
  2. String code
)

Log in an existing user using phone number and SMS code (OTP - one time password) that is sent to the phone. In order to use phone and password based log in, the authentication provider needs to be Altogic, meaning a user with phone and password credentials exists in the app database and sign in using authorization codes needs to be enabled in your app authentication settings. Before calling this method, you need to call the sendSignInCode method to get the SMS code delivered to the phone.

If successful, this method returns the authorization grants (e.g., session object) of the user.

If phone number confirmation is enabled in your app authentication settings and if the phone of the user has not been verified yet, this method will return an error message.

phone Phone of the user

code SMS code (OTP - one time password)

Implementation

Future<UserSessionResult> signInWithCode(String phone, String code) async {
  checkRequired('phone', phone);
  checkRequired('code', code);

  var queryString = encodeUriParameters({'code': code, 'phone': phone});

  var apiResponse = await _fetcher.post<Map<String, dynamic>>(
      '/_api/rest/v1/auth/signin-code$queryString');

  if (apiResponse.errors != null) {
    return UserSessionResult(errors: apiResponse.errors);
  }

  var data = apiResponse.data!;

  var user = data['user'] == null
      ? null
      : User.fromJson(data['user'] as Map<String, dynamic>);

  var session = data['session'] != null
      ? null
      : Session.fromJson(data['session'] as Map<String, dynamic>);

  await _deleteLocalData();
  await _saveLocalData(user!, session!);
  _fetcher.setSession(session, user);

  return UserSessionResult(session: session, user: user);
}