Skip to content

Commit 9347b7a

Browse files
committed
feat: ✨ Automatically log out user if session has expired or is invalid
1 parent 44e6112 commit 9347b7a

File tree

1 file changed

+23
-6
lines changed

1 file changed

+23
-6
lines changed

composables/CacheRefresh.ts

+23-6
Original file line numberDiff line numberDiff line change
@@ -2,26 +2,43 @@ import type { Mastodon } from "megalodon";
22
import type { InstanceWithExtra } from "./Instance";
33

44
export const useCacheRefresh = (client: MaybeRef<Mastodon | null>) => {
5+
if (process.server) return;
6+
57
const tokenData = useTokenData();
68
const me = useMe();
79
const instance = useInstance();
810
const customEmojis = useCustomEmojis();
911

1012
// Refresh custom emojis and instance data and me on every reload
1113
watchEffect(async () => {
14+
console.log("Clearing cache");
1215
if (tokenData.value) {
1316
await toValue(client)
1417
?.verifyAccountCredentials()
1518
.then((res) => {
1619
me.value = res.data;
20+
})
21+
.catch((err) => {
22+
const code = err.response.status;
23+
24+
if (code === 401) {
25+
// Reset tokenData
26+
tokenData.value = null;
27+
useEvent("notification:new", {
28+
type: "error",
29+
title: "Your session has expired",
30+
message:
31+
"You have been logged out. Please log in again.",
32+
});
33+
}
1734
});
18-
}
1935

20-
toValue(client)
21-
?.getInstanceCustomEmojis()
22-
.then((res) => {
23-
customEmojis.value = res.data;
24-
});
36+
await toValue(client)
37+
?.getInstanceCustomEmojis()
38+
.then((res) => {
39+
customEmojis.value = res.data;
40+
});
41+
}
2542

2643
toValue(client)
2744
?.getInstance()

0 commit comments

Comments
 (0)