Logout Button Implemented

This commit is contained in:
2025-09-04 15:40:18 +05:30
parent 45dbf8464a
commit 845ce1fe78
3 changed files with 54 additions and 3 deletions

View File

@@ -49,6 +49,12 @@ class AuthRepository {
_tokenExpiryKey, token.expiresAt.toIso8601String()); _tokenExpiryKey, token.expiresAt.toIso8601String());
} }
Future<void> clearAuthTokens() async {
await _secureStorage.deleteAll();
}
Future<AuthToken?> _getAuthToken() async { Future<AuthToken?> _getAuthToken() async {
final accessToken = await _secureStorage.read(_accessTokenKey); final accessToken = await _secureStorage.read(_accessTokenKey);
final expiryString = await _secureStorage.read(_tokenExpiryKey); final expiryString = await _secureStorage.read(_tokenExpiryKey);

View File

@@ -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"),
),
],
);
}
}

View File

@@ -1,9 +1,23 @@
import 'package:flutter/material.dart'; 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 '../../l10n/app_localizations.dart';
import 'package:kmobile/features/profile/preferences/preference_screen.dart'; import 'package:kmobile/features/profile/preferences/preference_screen.dart';
class ProfileScreen extends StatelessWidget { class ProfileScreen extends StatelessWidget {
const ProfileScreen({super.key}); const ProfileScreen({super.key});
Future<void> _handleLogout(BuildContext context) async {
final auth = getIt<AuthRepository>();
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 @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
@@ -17,7 +31,7 @@ class ProfileScreen extends StatelessWidget {
children: [ children: [
ListTile( ListTile(
leading: const Icon(Icons.settings), leading: const Icon(Icons.settings),
title: Text(loc.preferences), // Localized "Preferences" title: Text(loc.preferences),
onTap: () { onTap: () {
Navigator.push( Navigator.push(
context, context,
@@ -29,8 +43,16 @@ class ProfileScreen extends StatelessWidget {
// You can add more profile options here later // You can add more profile options here later
ListTile( ListTile(
leading: const Icon(Icons.logout), leading: const Icon(Icons.logout),
title: const Text("Logout"), // Localized "Preferences" title: const Text("Logout"),
onTap: () { onTap: () async {
final shouldLogout = await showDialog<bool>(
context: context,
builder: (_) => const LogoutDialog(),
);
if (shouldLogout == true) {
await _handleLogout(context);
}
}, },
), ),
], ],