mirror of
https://github.com/nagisa77/OpenIsle.git
synced 2026-02-20 22:11:01 +08:00
Merge pull request #459 from nagisa77/codex/reset-login-state-on-401-error
Reset auth on token expiry
This commit is contained in:
21
frontend_nuxt/plugins/auth-fetch.client.ts
Normal file
21
frontend_nuxt/plugins/auth-fetch.client.ts
Normal file
@@ -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
|
||||
}
|
||||
}
|
||||
})
|
||||
Reference in New Issue
Block a user