Logout Button Implemented
This commit is contained in:
@@ -49,6 +49,12 @@ class AuthRepository {
|
||||
_tokenExpiryKey, token.expiresAt.toIso8601String());
|
||||
}
|
||||
|
||||
Future<void> clearAuthTokens() async {
|
||||
await _secureStorage.deleteAll();
|
||||
|
||||
}
|
||||
|
||||
|
||||
Future<AuthToken?> _getAuthToken() async {
|
||||
final accessToken = await _secureStorage.read(_accessTokenKey);
|
||||
final expiryString = await _secureStorage.read(_tokenExpiryKey);
|
||||
|
@@ -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"),
|
||||
),
|
||||
],
|
||||
);
|
||||
}
|
||||
}
|
@@ -1,10 +1,24 @@
|
||||
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<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
|
||||
Widget build(BuildContext context) {
|
||||
final loc = AppLocalizations.of(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<bool>(
|
||||
context: context,
|
||||
builder: (_) => const LogoutDialog(),
|
||||
);
|
||||
|
||||
if (shouldLogout == true) {
|
||||
await _handleLogout(context);
|
||||
}
|
||||
},
|
||||
),
|
||||
],
|
||||
|
Reference in New Issue
Block a user