User can sign out of his account
This commit is contained in:
@ -42,9 +42,14 @@ class CurrentAuthState extends _$CurrentAuthState {
|
|||||||
/// Logs out, deletes the saved token and profile info from storage, and invalidates
|
/// Logs out, deletes the saved token and profile info from storage, and invalidates
|
||||||
/// the state.
|
/// the state.
|
||||||
Future<void> logout() async {
|
Future<void> logout() async {
|
||||||
|
final prefs = ref.read(prefsProvider).requireValue;
|
||||||
|
|
||||||
final secureStorage = ref.read(secureStorageProvider).requireValue;
|
final secureStorage = ref.read(secureStorageProvider).requireValue;
|
||||||
await secureStorage.removeToken();
|
await secureStorage.removeToken();
|
||||||
|
|
||||||
|
prefs.clearServerConfig();
|
||||||
|
prefs.clearAuthInfo();
|
||||||
|
|
||||||
ref
|
ref
|
||||||
// Invalidate the state so the auth state will be updated to authenticated.
|
// Invalidate the state so the auth state will be updated to authenticated.
|
||||||
.invalidateSelf();
|
.invalidateSelf();
|
||||||
|
@ -1,6 +1,12 @@
|
|||||||
|
import 'package:alert_dialog/alert_dialog.dart';
|
||||||
|
import 'package:confirm_dialog/confirm_dialog.dart';
|
||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
|
import 'package:go_router/go_router.dart';
|
||||||
import 'package:hooks_riverpod/hooks_riverpod.dart';
|
import 'package:hooks_riverpod/hooks_riverpod.dart';
|
||||||
|
import 'package:logging/logging.dart';
|
||||||
|
import 'package:moneymgr_mobile/providers/auth_state.dart';
|
||||||
import 'package:moneymgr_mobile/services/api/api_client.dart';
|
import 'package:moneymgr_mobile/services/api/api_client.dart';
|
||||||
|
import 'package:moneymgr_mobile/services/router/routes_list.dart';
|
||||||
import 'package:moneymgr_mobile/services/storage/prefs.dart';
|
import 'package:moneymgr_mobile/services/storage/prefs.dart';
|
||||||
|
|
||||||
class ProfileScreen extends HookConsumerWidget {
|
class ProfileScreen extends HookConsumerWidget {
|
||||||
@ -14,8 +20,37 @@ class ProfileScreen extends HookConsumerWidget {
|
|||||||
|
|
||||||
final profile = data.value?.authInfo();
|
final profile = data.value?.authInfo();
|
||||||
|
|
||||||
|
void onSettingsPressed() => context.push(settingsPage);
|
||||||
|
|
||||||
|
handleSignOut() async {
|
||||||
|
try {
|
||||||
|
if (!await confirm(
|
||||||
|
context,
|
||||||
|
content: Text("Do you really want to sign out of your account?"),
|
||||||
|
)) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
await ref.read(currentAuthStateProvider.notifier).logout();
|
||||||
|
} catch (e, s) {
|
||||||
|
Logger.root.warning("Failed to sign out! $e $s");
|
||||||
|
|
||||||
|
if (context.mounted) {
|
||||||
|
await alert(context, content: Text("Failed to sign you out! $e"));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return Scaffold(
|
return Scaffold(
|
||||||
appBar: AppBar(title: Text("Profile")),
|
appBar: AppBar(
|
||||||
|
title: Text("Profile"),
|
||||||
|
actions: [
|
||||||
|
IconButton(
|
||||||
|
onPressed: onSettingsPressed,
|
||||||
|
icon: const Icon(Icons.settings),
|
||||||
|
),
|
||||||
|
],
|
||||||
|
),
|
||||||
body: ListView(
|
body: ListView(
|
||||||
children: [
|
children: [
|
||||||
ListEntry(
|
ListEntry(
|
||||||
@ -39,6 +74,12 @@ class ProfileScreen extends HookConsumerWidget {
|
|||||||
icon: Icons.person,
|
icon: Icons.person,
|
||||||
),
|
),
|
||||||
ListEntry(title: "User mail", value: profile?.mail, icon: Icons.mail),
|
ListEntry(title: "User mail", value: profile?.mail, icon: Icons.mail),
|
||||||
|
Divider(),
|
||||||
|
ListEntry(
|
||||||
|
title: "Sign out",
|
||||||
|
icon: Icons.logout,
|
||||||
|
onTap: handleSignOut,
|
||||||
|
),
|
||||||
],
|
],
|
||||||
),
|
),
|
||||||
);
|
);
|
||||||
@ -49,20 +90,23 @@ class ListEntry extends StatelessWidget {
|
|||||||
final String title;
|
final String title;
|
||||||
final String? value;
|
final String? value;
|
||||||
final IconData icon;
|
final IconData icon;
|
||||||
|
final Function()? onTap;
|
||||||
|
|
||||||
const ListEntry({
|
const ListEntry({
|
||||||
super.key,
|
super.key,
|
||||||
required this.title,
|
required this.title,
|
||||||
required this.value,
|
this.value,
|
||||||
required this.icon,
|
required this.icon,
|
||||||
|
this.onTap,
|
||||||
});
|
});
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Widget build(BuildContext context) {
|
Widget build(BuildContext context) {
|
||||||
return ListTile(
|
return ListTile(
|
||||||
title: Text(title),
|
title: Text(title),
|
||||||
subtitle: Text(value ?? ""),
|
subtitle: value != null ? Text(value!) : null,
|
||||||
leading: Icon(icon),
|
leading: Icon(icon),
|
||||||
|
onTap: onTap,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -17,6 +17,14 @@ packages:
|
|||||||
url: "https://pub.dev"
|
url: "https://pub.dev"
|
||||||
source: hosted
|
source: hosted
|
||||||
version: "1.0.0"
|
version: "1.0.0"
|
||||||
|
alert_dialog:
|
||||||
|
dependency: "direct main"
|
||||||
|
description:
|
||||||
|
name: alert_dialog
|
||||||
|
sha256: "6f63afeaad3006a489fa5fda92a795219aa3e52dc2991178e99577ceabcf2036"
|
||||||
|
url: "https://pub.dev"
|
||||||
|
source: hosted
|
||||||
|
version: "1.0.2"
|
||||||
analyzer:
|
analyzer:
|
||||||
dependency: transitive
|
dependency: transitive
|
||||||
description:
|
description:
|
||||||
@ -185,6 +193,14 @@ packages:
|
|||||||
url: "https://pub.dev"
|
url: "https://pub.dev"
|
||||||
source: hosted
|
source: hosted
|
||||||
version: "1.19.1"
|
version: "1.19.1"
|
||||||
|
confirm_dialog:
|
||||||
|
dependency: "direct main"
|
||||||
|
description:
|
||||||
|
name: confirm_dialog
|
||||||
|
sha256: "99b431d2f13bf64a6056fc8f1b2b85a1fc7be572da3f28a17f8a009438542327"
|
||||||
|
url: "https://pub.dev"
|
||||||
|
source: hosted
|
||||||
|
version: "1.0.4"
|
||||||
convert:
|
convert:
|
||||||
dependency: transitive
|
dependency: transitive
|
||||||
description:
|
description:
|
||||||
|
@ -76,6 +76,10 @@ dependencies:
|
|||||||
# Qr Code library
|
# Qr Code library
|
||||||
mobile_scanner: ^7.0.1
|
mobile_scanner: ^7.0.1
|
||||||
|
|
||||||
|
# Show dialogs
|
||||||
|
confirm_dialog: ^1.0.4
|
||||||
|
alert_dialog: ^1.0.2
|
||||||
|
|
||||||
dev_dependencies:
|
dev_dependencies:
|
||||||
flutter_test:
|
flutter_test:
|
||||||
sdk: flutter
|
sdk: flutter
|
||||||
|
Reference in New Issue
Block a user