Files
swiftcontrol/lib/services/email_otp_auth_service.dart
Jonas Bark 2eca5d4178 feat(auth): sign in with an emailed six-digit code
Riders without a Google, Apple, GitHub or Facebook account can now sign in
with their email address: Supabase mails a six-digit code and EmailLoginForm
redeems it via verifyOTP.

Deliberately code-only rather than a clickable magic link — a link has to
travel back through bikecontrol://, which is unreliable on Windows and Linux,
and corporate mail scanners routinely open (and thereby burn) one-shot links
before the rider ever sees the message. A typed code behaves the same on every
platform, including web.

The Supabase side is the send-auth-email hook in the website repo, which
renders the branded BikeControl mail through Resend.

Also folds the pop/onBack block that was duplicated across all four OAuth
handlers into _afterSignIn(), which adds the mounted guard those calls were
missing.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
2026-08-20 08:52:34 +02:00

33 lines
1.2 KiB
Dart

import 'package:bike_control/utils/core.dart';
import 'package:supabase_flutter/supabase_flutter.dart';
/// Email sign-in with a one-time code: Supabase mails a six-digit code, the
/// rider types it back. Kept behind an interface so the sign-in form can be
/// tested without a live Supabase, mirroring [SupportChatService]'s injection.
abstract class EmailOtpAuth {
/// Mails a fresh sign-in code to [email], creating the account on first use.
Future<void> sendCode(String email);
/// Redeems [code] for a session. Throws [AuthException] when it is wrong,
/// expired, or already used.
Future<void> verifyCode({required String email, required String code});
}
class SupabaseEmailOtpAuth implements EmailOtpAuth {
SupabaseEmailOtpAuth({SupabaseClient? supabase}) : _supabase = supabase ?? core.supabase;
final SupabaseClient _supabase;
@override
Future<void> sendCode(String email) => _supabase.auth.signInWithOtp(email: email);
@override
Future<void> verifyCode({required String email, required String code}) => _supabase.auth.verifyOTP(
email: email,
token: code,
// `email` covers both templates the flow can produce: `magiclink` for a
// returning rider and `signup` for one Supabase just created.
type: OtpType.email,
);
}