diff --git a/lib/features/home/presentation/pages/home_page.dart b/lib/features/home/presentation/pages/home_page.dart index f136a44..5253a5c 100644 --- a/lib/features/home/presentation/pages/home_page.dart +++ b/lib/features/home/presentation/pages/home_page.dart @@ -1,43 +1,113 @@ import 'package:flutter/material.dart'; -import 'package:supabase_flutter/supabase_flutter.dart'; +import 'package:flutter_riverpod/flutter_riverpod.dart'; +import '../../../../providers/auth_provider.dart'; /// Home page for authenticated users /// /// Displays welcome message and provides logout functionality /// This is the main landing page after successful authentication -class HomePage extends StatelessWidget { +class HomePage extends ConsumerWidget { const HomePage({super.key}); @override - Widget build(BuildContext context) { - final currentUser = Supabase.instance.client.auth.currentUser; + Widget build(BuildContext context, WidgetRef ref) { + final authState = ref.watch(authStateProvider); + final currentUser = authState.user; + final authNotifier = ref.read(authProvider.notifier); + return Scaffold( return Scaffold( appBar: AppBar( title: const Text('Sage'), backgroundColor: Theme.of(context).colorScheme.inversePrimary, actions: [ - IconButton( - icon: const Icon(Icons.logout), - onPressed: () async { - try { - await Supabase.instance.client.auth.signOut(); - } catch (e) { - if (context.mounted) { - ScaffoldMessenger.of(context).showSnackBar( - SnackBar( - content: Text('Logout failed: ${e.toString()}'), - backgroundColor: Colors.red, - ), - ); + if (authState.isLoading) + const Padding( + padding: EdgeInsets.all(16.0), + child: SizedBox( + width: 24, + height: 24, + child: CircularProgressIndicator(strokeWidth: 2), + ), + ) + else + IconButton( + icon: const Icon(Icons.logout), + onPressed: () async { + // Show confirmation dialog before logout + final confirmed = await showDialog( + context: context, + builder: (context) => AlertDialog( + title: const Text('Confirm Logout'), + content: const Text('Are you sure you want to log out?'), + actions: [ + TextButton( + onPressed: () => Navigator.of(context).pop(false), + child: const Text('Cancel'), + ), + TextButton( + onPressed: () => Navigator.of(context).pop(true), + child: const Text('Logout'), + ), + ], + ), + ); + + if (confirmed == true && context.mounted) { + try { + await authNotifier.signOut(); + if (context.mounted) { + ScaffoldMessenger.of(context).showSnackBar( + const SnackBar( + content: Text('Logged out successfully'), + backgroundColor: Colors.green, + ), + ); + } + } catch (e) { + if (context.mounted) { + ScaffoldMessenger.of(context).showSnackBar( + SnackBar( + content: Text('Logout failed: ${e.toString()}'), + backgroundColor: Colors.red, + ), + ); + } + } } - } - }, - tooltip: 'Logout', - ), + }, + tooltip: 'Logout', + ), ], ), - body: Center( + body: Column( + children: [ + // Show auth error if present + if (authState.error != null) + Container( + width: double.infinity, + padding: const EdgeInsets.all(16), + color: Colors.red.shade50, + child: Row( + children: [ + Icon(Icons.error, color: Colors.red.shade700), + const SizedBox(width: 8), + Expanded( + child: Text( + authState.error!, + style: TextStyle(color: Colors.red.shade700), + ), + ), + IconButton( + icon: const Icon(Icons.close), + onPressed: () => authNotifier.clearError(), + color: Colors.red.shade700, + ), + ], + ), + ), + Expanded( + child: Center( child: Column( mainAxisAlignment: MainAxisAlignment.center, children: [ @@ -126,9 +196,11 @@ class HomePage extends StatelessWidget { ), ), ], + ), ), - ), + ], ), ); + ); } } \ No newline at end of file