feat: auth plugin

This commit is contained in:
Rafi
2023-02-22 16:50:53 +08:00
parent eb7f062144
commit 3e46512c15
5 changed files with 143 additions and 107 deletions

View File

@@ -0,0 +1,21 @@
export const useAuthFetch = async (url, options = {}) => {
const { $auth } = useNuxtApp()
const token = await $auth.retrieveToken()
if (!token) {
return await $auth.redirectToLogin()
}
options = Object.assign(options, {
headers: {
'Authorization': 'Bearer ' + token
}
})
const res = await useFetch(url, options)
if (res.error.value && res.error.value.status === 401) {
await $auth.logout()
}
return res
}