diff --git a/lib/data/repositories/auth_repository.dart b/lib/data/repositories/auth_repository.dart index 225b274..3ecf8c7 100644 --- a/lib/data/repositories/auth_repository.dart +++ b/lib/data/repositories/auth_repository.dart @@ -49,6 +49,12 @@ class AuthRepository { _tokenExpiryKey, token.expiresAt.toIso8601String()); } +Future clearAuthTokens() async { + await _secureStorage.deleteAll(); + + } + + Future _getAuthToken() async { final accessToken = await _secureStorage.read(_accessTokenKey); final expiryString = await _secureStorage.read(_tokenExpiryKey); diff --git a/lib/features/profile/preferences/logout_dialog.dart b/lib/features/profile/preferences/logout_dialog.dart index e69de29..3924731 100644 --- a/lib/features/profile/preferences/logout_dialog.dart +++ b/lib/features/profile/preferences/logout_dialog.dart @@ -0,0 +1,23 @@ +import 'package:flutter/material.dart'; + +class LogoutDialog extends StatelessWidget { + const LogoutDialog({super.key}); + + @override + Widget build(BuildContext context) { + return AlertDialog( + title: const Text("Logout"), + content: const Text("Are you sure you want to logout?"), + actions: [ + TextButton( + onPressed: () => Navigator.pop(context, false), // dismiss + child: const Text("No"), + ), + TextButton( + onPressed: () => Navigator.pop(context, true), // confirm + child: const Text("Yes"), + ), + ], + ); + } +} \ No newline at end of file diff --git a/lib/features/profile/profile_screen.dart b/lib/features/profile/profile_screen.dart index 516fdec..f41e142 100644 --- a/lib/features/profile/profile_screen.dart +++ b/lib/features/profile/profile_screen.dart @@ -1,9 +1,23 @@ import 'package:flutter/material.dart'; +import 'package:kmobile/data/repositories/auth_repository.dart'; +import 'package:kmobile/features/profile/preferences/logout_dialog.dart'; +import 'package:shared_preferences/shared_preferences.dart'; +import '../../di/injection.dart'; import '../../l10n/app_localizations.dart'; import 'package:kmobile/features/profile/preferences/preference_screen.dart'; class ProfileScreen extends StatelessWidget { const ProfileScreen({super.key}); + + + Future _handleLogout(BuildContext context) async { + final auth = getIt(); + final prefs = await SharedPreferences.getInstance(); + await prefs.clear(); // clear saved session/token + await auth.clearAuthTokens(); + // Navigate to login and remove all previous routes + Navigator.pushNamedAndRemoveUntil(context, '/login', (route) => false); + } @override Widget build(BuildContext context) { @@ -17,7 +31,7 @@ class ProfileScreen extends StatelessWidget { children: [ ListTile( leading: const Icon(Icons.settings), - title: Text(loc.preferences), // Localized "Preferences" + title: Text(loc.preferences), onTap: () { Navigator.push( context, @@ -29,8 +43,16 @@ class ProfileScreen extends StatelessWidget { // You can add more profile options here later ListTile( leading: const Icon(Icons.logout), - title: const Text("Logout"), // Localized "Preferences" - onTap: () { + title: const Text("Logout"), + onTap: () async { + final shouldLogout = await showDialog( + context: context, + builder: (_) => const LogoutDialog(), + ); + + if (shouldLogout == true) { + await _handleLogout(context); + } }, ), ],