sendOtp method
Sends an OTP code to phone via POST /auth/otp/send.
On success: stores phone internally and transitions to
OtpStep.codeInput.
On failure: sets error state; step remains OtpStep.phoneInput.
@param phone E.164-formatted phone number (e.g. +905301234567).
Implementation
Future<void> sendOtp({required String phone}) async {
if (_isSubmitting) return;
_isSubmitting = true;
setLoading();
clearErrors();
try {
// 1. Capture phone early — needed in step 2 even if user navigates away.
_phoneNumber = phone;
final response = await Http.post(
'/auth/otp/send',
data: {'phone': phone},
);
if (!response.successful) {
handleApiError(
response,
fallback: trans('magic_starter.otp.send_error'),
);
return;
}
// 2. Advance to code input step on success.
_step = OtpStep.codeInput;
setSuccess(null);
} catch (e, stackTrace) {
Log.error('[MagicStarterOtpController.sendOtp] $e\n$stackTrace');
setError(trans('errors.unexpected'));
} finally {
_isSubmitting = false;
}
}