OTP screen rewrite: 6 rounded boxes, auto-advance focus, auto-submit on the 6th digit, hardware-backspace on empty boxes (intercepted via Focus.onKeyEvent since TextField.onChanged doesn't fire on already-empty input), resend cooldown sourced from /api/shared/config/otp, and an inline error message under the boxes instead of a SnackBar. Several bugs fixed inline that surfaced during testing: - ref.listen inside build() accumulates listeners on every rebuild — the resend countdown's per-second setState was piling up duplicate listeners so one error triggered N callback fires. Moved to ref.listenManual in initState; subscription disposed in dispose(). - RouterNotifier was calling notifyListeners() on every auth state change including AsyncError, which rebuilt the Navigator/Scaffold mid-snackbar and visually duplicated the error toast. Now skips AsyncError and same-data-variant transitions. - ScaffoldMessenger.showSnackBar from a Riverpod listener callback could still render twice even with hideCurrentSnackBar — replaced with an inline error widget to sidestep the snackbar machinery entirely. - register_screen now uses context.go instead of context.push for the OTP route, so re-submitting the phone form doesn't stack multiple OtpScreen instances with active subscriptions. Lockout UX: AuthErrorInfo wraps the error message + code + retry_after_seconds parsed from the backend's structured error response. On rate-limit codes (OTP_COOLDOWN, OTP_RATE_LIMIT_PHONE, OTP_RATE_LIMIT_IP), the OTP screen extends "Kirim ulang kode" cooldown to match the server's wait, and the register screen disables "Kirim OTP" with a "Coba lagi dalam …" countdown. formatCountdown() in core/constants.dart renders Xd under 90 seconds and Xm Yd above (clearer than raw seconds for long lockouts). Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
152 lines
5.2 KiB
Dart
152 lines
5.2 KiB
Dart
import 'dart:async';
|
|
import 'package:flutter/material.dart';
|
|
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
|
import 'package:go_router/go_router.dart';
|
|
import '../../../core/auth/auth_notifier.dart';
|
|
import '../../../core/auth/social_auth_enabled.dart';
|
|
import '../../../core/constants.dart';
|
|
|
|
class RegisterScreen extends ConsumerStatefulWidget {
|
|
const RegisterScreen({super.key});
|
|
|
|
@override
|
|
ConsumerState<RegisterScreen> createState() => _RegisterScreenState();
|
|
}
|
|
|
|
class _RegisterScreenState extends ConsumerState<RegisterScreen> {
|
|
final _phoneController = TextEditingController();
|
|
ProviderSubscription<AsyncValue<AuthData>>? _authSub;
|
|
|
|
// Server-imposed lockout: when /otp/request returns 429, the backend
|
|
// includes retry_after_seconds. We disable "Kirim OTP" for that window.
|
|
int _lockoutSeconds = 0;
|
|
Timer? _lockoutTimer;
|
|
String? _errorMessage;
|
|
|
|
@override
|
|
void initState() {
|
|
super.initState();
|
|
// Listener registered once in initState — keeps it independent of the
|
|
// build cycle so it doesn't accumulate (see feedback_riverpod_listen_in_build).
|
|
_authSub = ref.listenManual<AsyncValue<AuthData>>(authProvider, (prev, next) {
|
|
if (!mounted) return;
|
|
final data = next.valueOrNull;
|
|
if (data is AuthOtpSentData) {
|
|
// Use go (replace) so re-submitting the phone form doesn't stack
|
|
// multiple OtpScreen instances with active listeners.
|
|
context.go('/auth/otp', extra: _phoneController.text.trim());
|
|
return;
|
|
}
|
|
if (next is AsyncError) {
|
|
final err = next.error;
|
|
setState(() => _errorMessage = err.toString());
|
|
if (err is AuthErrorInfo &&
|
|
err.retryAfterSeconds != null &&
|
|
(err.code == 'OTP_COOLDOWN' ||
|
|
err.code == 'OTP_RATE_LIMIT_PHONE' ||
|
|
err.code == 'OTP_RATE_LIMIT_IP')) {
|
|
_startLockout(err.retryAfterSeconds!);
|
|
}
|
|
} else if (next is AsyncData) {
|
|
if (_errorMessage != null) setState(() => _errorMessage = null);
|
|
}
|
|
});
|
|
}
|
|
|
|
@override
|
|
void dispose() {
|
|
_authSub?.close();
|
|
_lockoutTimer?.cancel();
|
|
_phoneController.dispose();
|
|
super.dispose();
|
|
}
|
|
|
|
void _startLockout(int seconds) {
|
|
_lockoutTimer?.cancel();
|
|
setState(() => _lockoutSeconds = seconds);
|
|
_lockoutTimer = Timer.periodic(const Duration(seconds: 1), (timer) {
|
|
if (!mounted) {
|
|
timer.cancel();
|
|
return;
|
|
}
|
|
setState(() {
|
|
if (_lockoutSeconds > 0) _lockoutSeconds--;
|
|
if (_lockoutSeconds <= 0) timer.cancel();
|
|
});
|
|
});
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
final authState = ref.watch(authProvider);
|
|
final isLoading = authState is AsyncLoading;
|
|
final isLockedOut = _lockoutSeconds > 0;
|
|
final canSubmit = !isLoading && !isLockedOut;
|
|
|
|
return Scaffold(
|
|
appBar: AppBar(title: const Text('Masuk / Daftar')),
|
|
body: Padding(
|
|
padding: const EdgeInsets.all(24),
|
|
child: Column(
|
|
crossAxisAlignment: CrossAxisAlignment.stretch,
|
|
children: [
|
|
if (kSocialAuthEnabled) ...[
|
|
ElevatedButton.icon(
|
|
icon: const Icon(Icons.g_mobiledata),
|
|
onPressed: isLoading ? null
|
|
: () => ref.read(authProvider.notifier).loginGoogle(),
|
|
label: const Text('Lanjut dengan Google'),
|
|
),
|
|
const SizedBox(height: 12),
|
|
ElevatedButton.icon(
|
|
icon: const Icon(Icons.apple),
|
|
onPressed: isLoading ? null
|
|
: () => ref.read(authProvider.notifier).loginApple(),
|
|
label: const Text('Lanjut dengan Apple'),
|
|
),
|
|
const Padding(
|
|
padding: EdgeInsets.symmetric(vertical: 24),
|
|
child: Row(children: [
|
|
Expanded(child: Divider()),
|
|
Padding(padding: EdgeInsets.symmetric(horizontal: 12), child: Text('atau')),
|
|
Expanded(child: Divider()),
|
|
]),
|
|
),
|
|
],
|
|
TextField(
|
|
controller: _phoneController,
|
|
decoration: const InputDecoration(
|
|
labelText: 'Nomor HP',
|
|
hintText: '+628xxxxxxxxxx',
|
|
border: OutlineInputBorder(),
|
|
),
|
|
keyboardType: TextInputType.phone,
|
|
),
|
|
const SizedBox(height: 12),
|
|
ElevatedButton(
|
|
onPressed: canSubmit ? () {
|
|
final phone = _phoneController.text.trim();
|
|
if (phone.isEmpty) return;
|
|
ref.read(authProvider.notifier).requestOtp(phone);
|
|
} : null,
|
|
child: isLoading
|
|
? const CircularProgressIndicator()
|
|
: Text(isLockedOut
|
|
? 'Coba lagi dalam ${formatCountdown(_lockoutSeconds)}'
|
|
: 'Kirim OTP'),
|
|
),
|
|
if (_errorMessage != null) ...[
|
|
const SizedBox(height: 12),
|
|
Text(
|
|
_errorMessage!,
|
|
textAlign: TextAlign.center,
|
|
style: TextStyle(color: Colors.red.shade700, fontSize: 13),
|
|
),
|
|
],
|
|
],
|
|
),
|
|
),
|
|
);
|
|
}
|
|
}
|