sendOTP method

  1. @override
Future<RequestResponse<SendOTPResponse?>> sendOTP(
  1. String mobileNo, {
  2. bool? newOTP = false,
})
override

Sends a request to the backend with the mobile number. The response is a SendOTPResponse.

If newOTP is true, a previously sent OTP, won't be used. Instead a fresh one will be generated.

By default newOTP is false.

There are multiple reasons where this request will respond with failure which include:

  • SMS Settings not configured in the backend.
  • The mobile number is wrongly formatted.
  • The SMS provider responded with an error.

Implementation

@override
Future<RequestResponse<SendOTPResponse?>> sendOTP(String mobileNo,
    {bool? newOTP = false}) async {
  await getFrappe().checkAppInstalled(features: ['sendOTP']);

  final response = await Request.initiateRequest(
      url: config.hostUrl + '/api/method/renovation/auth.sms.generate',
      method: HttpMethod.POST,
      contentType: ContentTypeLiterals.APPLICATION_X_WWW_FORM_URLENCODED,
      data: <String, dynamic>{'mobile': mobileNo, 'newPIN': newOTP! ? 1 : 0});

  SendOTPResponse? sendOTPResponse;

  if (response.isSuccess) {
    sendOTPResponse =
        SendOTPResponse.fromJson(Request.convertToMap(response.rawResponse!));
  }
  if (sendOTPResponse != null && sendOTPResponse.status == 'fail') {
    response.error = ErrorDetail(
        title: 'SMS was not sent',
        type: RenovationError.BackendSettingError,
        info: (Information()
          ..httpCode = 400
          ..data = response.data
          ..cause = 'SMS settings may not be set'
          ..rawResponse = response.rawResponse));
    response.isSuccess = false;
  }

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