mirror of
https://github.com/versia-pub/frontend.git
synced 2025-12-06 16:38:20 +01:00
feat: ✨ Automatically log out user if session has expired or is invalid
This commit is contained in:
parent
44e6112335
commit
9347b7ac25
|
|
@ -2,6 +2,8 @@ import type { Mastodon } from "megalodon";
|
||||||
import type { InstanceWithExtra } from "./Instance";
|
import type { InstanceWithExtra } from "./Instance";
|
||||||
|
|
||||||
export const useCacheRefresh = (client: MaybeRef<Mastodon | null>) => {
|
export const useCacheRefresh = (client: MaybeRef<Mastodon | null>) => {
|
||||||
|
if (process.server) return;
|
||||||
|
|
||||||
const tokenData = useTokenData();
|
const tokenData = useTokenData();
|
||||||
const me = useMe();
|
const me = useMe();
|
||||||
const instance = useInstance();
|
const instance = useInstance();
|
||||||
|
|
@ -9,20 +11,35 @@ export const useCacheRefresh = (client: MaybeRef<Mastodon | null>) => {
|
||||||
|
|
||||||
// Refresh custom emojis and instance data and me on every reload
|
// Refresh custom emojis and instance data and me on every reload
|
||||||
watchEffect(async () => {
|
watchEffect(async () => {
|
||||||
|
console.log("Clearing cache");
|
||||||
if (tokenData.value) {
|
if (tokenData.value) {
|
||||||
await toValue(client)
|
await toValue(client)
|
||||||
?.verifyAccountCredentials()
|
?.verifyAccountCredentials()
|
||||||
.then((res) => {
|
.then((res) => {
|
||||||
me.value = res.data;
|
me.value = res.data;
|
||||||
|
})
|
||||||
|
.catch((err) => {
|
||||||
|
const code = err.response.status;
|
||||||
|
|
||||||
|
if (code === 401) {
|
||||||
|
// Reset tokenData
|
||||||
|
tokenData.value = null;
|
||||||
|
useEvent("notification:new", {
|
||||||
|
type: "error",
|
||||||
|
title: "Your session has expired",
|
||||||
|
message:
|
||||||
|
"You have been logged out. Please log in again.",
|
||||||
|
});
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
await toValue(client)
|
||||||
|
?.getInstanceCustomEmojis()
|
||||||
|
.then((res) => {
|
||||||
|
customEmojis.value = res.data;
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
toValue(client)
|
|
||||||
?.getInstanceCustomEmojis()
|
|
||||||
.then((res) => {
|
|
||||||
customEmojis.value = res.data;
|
|
||||||
});
|
|
||||||
|
|
||||||
toValue(client)
|
toValue(client)
|
||||||
?.getInstance()
|
?.getInstance()
|
||||||
.then((res) => {
|
.then((res) => {
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue