verifyOTP method

  1. @override
Future<RequestResponse<VerifyOTPResponse?>> verifyOTP(
  1. String mobileNo,
  2. String otp,
  3. bool loginToUser
)
override

Verifies the otp entered by the user against the one sent through sendOTP.

If loginToUser is true, the sessionStatus will be updated with the new session part of the API response.

In all cases, it returns the response VerifyOTPResponse.

Implementation

@override
Future<RequestResponse<VerifyOTPResponse?>> verifyOTP(
    String mobileNo, String otp, bool loginToUser) async {
  await getFrappe().checkAppInstalled(features: ['verifyOTP']);

  final response = await Request.initiateRequest(
      url: config.hostUrl + '/api/method/renovation/auth.sms.verify',
      method: HttpMethod.POST,
      contentType: ContentTypeLiterals.APPLICATION_X_WWW_FORM_URLENCODED,
      isFrappeResponse: false,
      data: <String, dynamic>{
        'mobile': mobileNo,
        'pin': otp,
        'loginToUser': loginToUser ? 1 : 0,
        'use_jwt': _useJwt ? 1 : 0
      });

  VerifyOTPResponse? verifyOTPResponse;

  if (response.isSuccess) {
    verifyOTPResponse = VerifyOTPResponse.fromJson(response.data!.message);
    if (verifyOTPResponse.status != 'verified') {
      response.isSuccess = false;
    } else {
      // successful login
      if (loginToUser) {
        // update session
        // response.data.user is expected from renovation_core

        final sessionStatusInfo =
            FrappeSessionStatusInfo.fromJson(response.data!.message);
        sessionStatusInfo.rawSession =
            Request.convertToMap(response.rawResponse!);

        updateSession(loggedIn: true, sessionStatus: sessionStatusInfo);
      }
    }
  }

  if (!response.isSuccess && response.error == null) {
    response.error = ErrorDetail(
        info: Information(
            data: verifyOTPResponse,
            httpCode: response.httpCode,
            rawResponse: response.rawResponse));
  }

  return response.isSuccess
      ? RequestResponse.success(verifyOTPResponse,
          rawResponse: response.rawResponse)
      : RequestResponse.fail(handleError('verify_otp', response.error));
}