From ce5bcb9dca93b6b1bdb02427b642f6237f6b4118 Mon Sep 17 00:00:00 2001 From: Tim <135014430+nagisa77@users.noreply.github.com> Date: Sun, 10 Aug 2025 11:59:23 +0800 Subject: [PATCH] Reset auth on 401 --- frontend_nuxt/plugins/auth-fetch.client.ts | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 frontend_nuxt/plugins/auth-fetch.client.ts diff --git a/frontend_nuxt/plugins/auth-fetch.client.ts b/frontend_nuxt/plugins/auth-fetch.client.ts new file mode 100644 index 000000000..0a07440b8 --- /dev/null +++ b/frontend_nuxt/plugins/auth-fetch.client.ts @@ -0,0 +1,21 @@ +import { clearToken } from '~/utils/auth' + +export default defineNuxtPlugin(() => { + if (process.client) { + const originalFetch = window.fetch + window.fetch = async (input, init) => { + const response = await originalFetch(input, init) + if (response.status === 401) { + try { + const data = await response.clone().json() + if (data && data.error === 'Invalid or expired token') { + clearToken() + } + } catch (e) { + // ignore JSON parsing errors + } + } + return response + } + } +})