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 + } + } +})