otp method

Future<AuthResponse<Session>> otp({
  1. required String email,
  2. required String otp,
  3. SignInCallbacks? callbacks,
})

Signs in with a one-time password (OTP).

email The user's email address. otp The one-time password code. callbacks Optional lifecycle callbacks.

Returns an AuthResponse containing the Session on success.

Implementation

Future<AuthResponse<Session>> otp({
  required String email,
  required String otp,
  SignInCallbacks? callbacks,
}) async {
  callbacks?.onRequest?.call();

  try {
    final response = await _dio.post(
      ApiEndpoints.signInOtp,
      data: {
        'email': email,
        'otp': otp,
      },
    );

    final session = Session.fromJson(response.data['session'] ?? response.data);
    await _tokenManager.setAccessToken(session.token);
    callbacks?.onSuccess?.call(session);

    return AuthResponse.success(session);
  } on DioException catch (e) {
    final error = AuthError.fromDio(e);
    callbacks?.onError?.call(error);
    return AuthResponse.error(error);
  }
}