feat: enhance routing logic and improve login screen validation; add guarded API call for error handling

This commit is contained in:
Nils-Johan Gynther
2026-04-22 07:35:34 +02:00
parent e8de1d3625
commit 2ea18503ef
4 changed files with 117 additions and 54 deletions
@@ -13,73 +13,102 @@ class LoginScreen extends ConsumerStatefulWidget {
}
class _LoginScreenState extends ConsumerState<LoginScreen> {
final _formKey = GlobalKey<FormState>();
final _usernameCtrl = TextEditingController();
final _passwordCtrl = TextEditingController();
final _passwordFocus = FocusNode();
@override
void dispose() {
_usernameCtrl.dispose();
_passwordCtrl.dispose();
_passwordFocus.dispose();
super.dispose();
}
Future<void> _submit() async {
if (_usernameCtrl.text.trim().isEmpty || _passwordCtrl.text.isEmpty) {
if (!(_formKey.currentState?.validate() ?? false)) {
return;
}
await ref.read(authStateProvider.notifier).login(
_usernameCtrl.text.trim(),
_passwordCtrl.text,
);
if (mounted) {
final state = ref.read(authStateProvider);
if (state is AsyncData && state.value != null) {
if (context.mounted) context.go('/recipes');
}
}
// Router redirect handles navigation when authStateProvider updates.
}
@override
Widget build(BuildContext context) {
final authState = ref.watch(authStateProvider);
final isLoading = authState is AsyncLoading;
return Scaffold(
appBar: AppBar(title: const Text('Logga in')),
body: Padding(
padding: const EdgeInsets.all(24),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
TextField(
controller: _usernameCtrl,
decoration: const InputDecoration(labelText: 'Anvandarnamn'),
textInputAction: TextInputAction.next,
),
const SizedBox(height: 12),
TextField(
controller: _passwordCtrl,
decoration: const InputDecoration(labelText: 'Lösenord'),
obscureText: true,
textInputAction: TextInputAction.done,
onSubmitted: (_) => _submit(),
),
const SizedBox(height: 24),
if (authState is AsyncLoading)
const CircularProgressIndicator()
else
ElevatedButton(
onPressed: _submit,
child: const Text('Logga in'),
body: Center(
child: SingleChildScrollView(
padding: const EdgeInsets.all(24),
child: ConstrainedBox(
constraints: const BoxConstraints(maxWidth: 400),
child: Form(
key: _formKey,
child: Column(
mainAxisSize: MainAxisSize.min,
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
TextFormField(
controller: _usernameCtrl,
decoration:
const InputDecoration(labelText: 'Användarnamn'),
textInputAction: TextInputAction.next,
autofocus: true,
enabled: !isLoading,
onFieldSubmitted: (_) =>
FocusScope.of(context).requestFocus(_passwordFocus),
validator: (value) {
if (value == null || value.trim().isEmpty) {
return 'Ange ditt användarnamn.';
}
return null;
},
),
const SizedBox(height: 16),
TextFormField(
controller: _passwordCtrl,
focusNode: _passwordFocus,
decoration: const InputDecoration(labelText: 'Lösenord'),
obscureText: true,
textInputAction: TextInputAction.done,
enabled: !isLoading,
onFieldSubmitted: (_) => _submit(),
validator: (value) {
if (value == null || value.isEmpty) {
return 'Ange ditt lösenord.';
}
return null;
},
),
const SizedBox(height: 28),
if (isLoading)
const Center(child: CircularProgressIndicator())
else
FilledButton(
onPressed: _submit,
child: const Text('Logga in'),
),
if (authState is AsyncError)
Padding(
padding: const EdgeInsets.only(top: 16),
child: Text(
mapErrorToUserMessage(authState.error!),
textAlign: TextAlign.center,
style: TextStyle(
color: Theme.of(context).colorScheme.error),
),
),
],
),
if (authState is AsyncError)
Padding(
padding: const EdgeInsets.only(top: 12),
child: Text(
mapErrorToUserMessage(authState.error!),
textAlign: TextAlign.center,
style: TextStyle(color: Theme.of(context).colorScheme.error),
),
),
],
),
),
),
),
);
@@ -1,8 +1,10 @@
import 'package:flutter_riverpod/flutter_riverpod.dart';
import '../../../core/api/api_providers.dart';
import '../../../core/api/guarded_api_call.dart';
import '../../../features/auth/data/auth_providers.dart';
import 'recipe_repository.dart';
import '../domain/recipe.dart';
import 'recipe_repository.dart';
final recipeRepositoryProvider = Provider<RecipeRepository>((ref) {
return RecipeRepository(ref.watch(apiClientProvider));
@@ -10,5 +12,8 @@ final recipeRepositoryProvider = Provider<RecipeRepository>((ref) {
final recipesProvider = FutureProvider<List<Recipe>>((ref) async {
final token = await ref.watch(authStateProvider.future);
return ref.watch(recipeRepositoryProvider).fetchRecipes(token: token);
return guardedApiCall(
ref,
() => ref.read(recipeRepositoryProvider).fetchRecipes(token: token),
);
});