verifyOtp method

Future<void> verifyOtp({
  1. required String otp,
})
inherited

Implementation

Future<void> verifyOtp({required String otp}) async {
  final verificationId = _verificationId;
  if (verificationId == null) {
    setLoading(isLoading: false, error: 'Verification ID is required');
    goToPage(VoAuthPageKeys.phoneSignIn);
    return;
  }

  if (otp.isEmpty) {
    setLoading(isLoading: false, error: 'OTP is required');
    return;
  }

  setLoading(isLoading: true, error: null);

  final result = await otpService.verifyOtp(
    VerifyOtpPayload(verificationId: verificationId, otp: otp),
  );

  await result.when(
    success: (response) async {
      if (response.data.hasAccount) {
        await _loginWithToken(response.data.verificationToken);
        return;
      }

      if (!ref.mounted) return;
      state = state.copyWith(
        verifyOtpResponse: response,
        isLoading: false,
        error: null,
      );
      goToPage(VoAuthPageKeys.phoneSignUp);
    },
    failure: (error) {
      if (!ref.mounted) return;
      setLoading(isLoading: false, error: error.message);
    },
  );
}