verifyMobileOTP method

Future<GotrueSessionResponse> verifyMobileOTP(
  1. String phone,
  2. String token, {
  3. AuthOptions? options,
})

Send User supplied Mobile OTP to be verified

phone is the user's phone number WITH international prefix

token is the token that user was sent to their mobile phone

Implementation

Future<GotrueSessionResponse> verifyMobileOTP(
  String phone,
  String token, {
  AuthOptions? options,
}) async {
  try {
    final body = {
      'phone': phone,
      'token': token,
      'type': 'sms',
      'redirect_to': options?.redirectTo,
    };
    final fetchOptions = FetchOptions(headers);
    final response =
        await _fetch.post('$url/verify', body, options: fetchOptions);
    if (response.error != null) {
      return GotrueSessionResponse.fromResponse(response: response);
    } else {
      Session session =
          Session.fromJson(response.rawData as Map<String, dynamic>);
      // if the user in the current session is null, we get the user based on
      // the session's jwt token
      if (session.user == null) {
        final userResponse = await getUser(session.accessToken);
        if (userResponse.user != null) {
          session = session.copyWith(user: userResponse.user);
        }
      }
      return GotrueSessionResponse.fromResponse(
        response: response,
        data: session,
      );
    }
  } catch (e) {
    return GotrueSessionResponse(error: GotrueError(e.toString()));
  }
}