From 9347b7ac2506ca81079e72d4bb78af7ace51b1b2 Mon Sep 17 00:00:00 2001 From: Jesse Wierzbinski Date: Thu, 16 May 2024 21:19:06 -1000 Subject: [PATCH] feat: :sparkles: Automatically log out user if session has expired or is invalid --- composables/CacheRefresh.ts | 29 +++++++++++++++++++++++------ 1 file changed, 23 insertions(+), 6 deletions(-) diff --git a/composables/CacheRefresh.ts b/composables/CacheRefresh.ts index 76e2126..e46244c 100644 --- a/composables/CacheRefresh.ts +++ b/composables/CacheRefresh.ts @@ -2,6 +2,8 @@ import type { Mastodon } from "megalodon"; import type { InstanceWithExtra } from "./Instance"; export const useCacheRefresh = (client: MaybeRef) => { + if (process.server) return; + const tokenData = useTokenData(); const me = useMe(); const instance = useInstance(); @@ -9,20 +11,35 @@ export const useCacheRefresh = (client: MaybeRef) => { // Refresh custom emojis and instance data and me on every reload watchEffect(async () => { + console.log("Clearing cache"); if (tokenData.value) { await toValue(client) ?.verifyAccountCredentials() .then((res) => { 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) ?.getInstance() .then((res) => {