Files
chatgpt-ui/composables/useAuthFetch.js
2023-02-22 16:50:53 +08:00

22 lines
493 B
JavaScript

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
}