Add log out to user settings page

This commit is contained in:
2024-05-30 11:02:08 +08:00
parent fb21e0cd73
commit 7f033570b3
5 changed files with 66 additions and 13 deletions

View File

@@ -332,10 +332,19 @@ void clearAllStates(BuildContext context) {
checkAuth(context);
}
void clearAllStates2(GoRouterState? state, GoRouter router) {
auth.clear();
tags.clear();
tasks.clear();
checkAuth2(state, router);
}
void checkAuth(BuildContext context) {
checkAuth2(GoRouterState.of(context), GoRouter.of(context));
}
void checkAuth2(GoRouterState? state, GoRouter router) {
if (!auth.isAuthed && !auth.checked && !auth.isChecking) {
final state = GoRouterState.of(context);
final router = GoRouter.of(context);
auth.checkAuth().then((re) {
if (!re) {
if (auth.status!.noUser &&
@@ -343,7 +352,7 @@ void checkAuth(BuildContext context) {
return;
}
final loc = auth.status!.noUser ? "/create_root_user" : "/login";
if (state.path != loc) {
if (state?.path != loc) {
router.push(loc);
}
}

View File

@@ -237,5 +237,8 @@
"oldPassword": "Old password",
"newPassword": "New password",
"incorrectPassword": "Incorrect password.",
"changedPasswordSuccessfully": "Changed password successfully."
"changedPasswordSuccessfully": "Changed password successfully.",
"logout": "Log out",
"logoutConfirm": "Do you want to log out?",
"failedLogout": "Failed to log out: "
}

View File

@@ -237,5 +237,8 @@
"oldPassword": "旧密码",
"newPassword": "新密码",
"incorrectPassword": "不正确的密码。",
"changedPasswordSuccessfully": "修改密码成功。"
"changedPasswordSuccessfully": "修改密码成功。",
"logout": "退出登录",
"logoutConfirm": "是否退出登录",
"failedLogout": "退出登录失败:"
}

View File

@@ -47,6 +47,22 @@ Future<void> _changeUserPassword(
}
}
Future<void> _deleteToken(AppLocalizations i18n, GoRouter router) async {
try {
(await api.deleteToken()).unwrap();
clearAllStates2(null, router);
} catch (e, stack) {
String errmsg = e.toString();
if (e is (int, String)) {
_log.warning("Failed to delete token: $e");
} else {
_log.severe("Failed to delete token: $e\n$stack");
}
final snack = SnackBar(content: Text("${i18n.failedLogout}$errmsg"));
rootScaffoldMessengerKey.currentState?.showSnackBar(snack);
}
}
class _ChangeUsernameDialog extends StatefulWidget {
const _ChangeUsernameDialog({this.username});
@@ -254,6 +270,28 @@ class _UserSettingsPage extends State<UserSettingsPage> with ThemeModeWidget {
padding: const EdgeInsets.only(left: 16),
child: UserPermissionsChips(
permissions: auth.user!.permissions, readOnly: true)),
ListTile(
leading: const Icon(Icons.logout),
title: Text(i18n.logout),
onTap: () => showDialog(
context: context,
builder: (context) => AlertDialog(
title: Text(i18n.logout),
content: Text(i18n.logoutConfirm),
actions: [
TextButton(
onPressed: () {
_deleteToken(i18n, GoRouter.of(context));
context.pop();
},
child: Text(i18n.yes)),
TextButton(
onPressed: () {
context.pop();
},
child: Text(i18n.no)),
],
))),
]));
}

View File

@@ -27,12 +27,14 @@ class TaskManager {
bool _needClosed = false;
bool _waitClosed = false;
bool get closed => _closed;
int _connectId = 0;
void clear() {
tasks.clear();
tasksList.clear();
_connectId++;
_channel?.sink.add("{\"type\":\"close\"}");
_channel?.sink.close();
_channel = null;
tasks.clear();
tasksList.clear();
_closed = true;
}
@@ -98,8 +100,8 @@ class TaskManager {
Future<void> connect() async {
if (auth.canManageTasks != true) return;
try {
final url = api.getTaskUrl();
_channel = await connectWebSocket(url);
final cId = _connectId;
_channel = await connectWebSocket(api.getTaskUrl());
_channel!.stream.listen((event) {
try {
final data = jsonDecode(event) as Map<String, dynamic>;
@@ -198,8 +200,7 @@ class TaskManager {
}
}, onError: (e) {
_log.warning("Task websocket error: $e");
final url2 = api.getTaskUrl();
if (_allowReconnect && !_needClosed && url == url2) {
if (_allowReconnect && !_needClosed && _connectId == cId) {
_log.info("Reconnecting to task server in 5 seconds");
_reconnectTimer = Timer(const Duration(seconds: 5), () {
_reconnectTimer = null;
@@ -212,8 +213,7 @@ class TaskManager {
}, onDone: () {
_log.warning(
"WenSocket closed: ${_channel?.closeCode} ${_channel?.closeReason}");
final url2 = api.getTaskUrl();
if (_allowReconnect && !_needClosed && url == url2) {
if (_allowReconnect && !_needClosed && _connectId == cId) {
_log.info("Reconnecting to task server in 5 seconds");
_reconnectTimer = Timer(const Duration(seconds: 5), () {
_reconnectTimer = null;