Skip to content

Commit

Permalink
Add log out to user settings page
Browse files Browse the repository at this point in the history
  • Loading branch information
lifegpc committed May 30, 2024
1 parent fb21e0c commit 7f03357
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 13 deletions.
15 changes: 12 additions & 3 deletions lib/globals.dart
Original file line number Diff line number Diff line change
Expand Up @@ -332,18 +332,27 @@ 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 &&
prefs.getBool("skipCreateRootUser") == true) {
return;
}
final loc = auth.status!.noUser ? "/create_root_user" : "/login";
if (state.path != loc) {
if (state?.path != loc) {
router.push(loc);
}
}
Expand Down
5 changes: 4 additions & 1 deletion lib/l10n/app_en.arb
Original file line number Diff line number Diff line change
Expand Up @@ -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: "
}
5 changes: 4 additions & 1 deletion lib/l10n/app_zh.arb
Original file line number Diff line number Diff line change
Expand Up @@ -237,5 +237,8 @@
"oldPassword": "旧密码",
"newPassword": "新密码",
"incorrectPassword": "不正确的密码。",
"changedPasswordSuccessfully": "修改密码成功。"
"changedPasswordSuccessfully": "修改密码成功。",
"logout": "退出登录",
"logoutConfirm": "是否退出登录",
"failedLogout": "退出登录失败:"
}
38 changes: 38 additions & 0 deletions lib/pages/settings/user.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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});

Expand Down Expand Up @@ -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)),
],
))),
]));
}

Expand Down
16 changes: 8 additions & 8 deletions lib/task.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down Expand Up @@ -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>;
Expand Down Expand Up @@ -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;
Expand All @@ -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;
Expand Down

0 comments on commit 7f03357

Please sign in to comment.