Implements automatic token removal and redirection to login upon 401 error status, improving user experience and security.
20 lines
553 B
TypeScript
20 lines
553 B
TypeScript
import { defu } from 'defu'
|
|
import { toast } from 'vue-sonner'
|
|
|
|
export function useAPI(api: string, options?: object): Promise<unknown> {
|
|
return $fetch(api, defu(options || {}, {
|
|
headers: {
|
|
Authorization: `Bearer ${localStorage.getItem('SinkSiteToken') || ''}`,
|
|
},
|
|
})).catch((error) => {
|
|
if (error?.status === 401) {
|
|
localStorage.removeItem('SinkSiteToken')
|
|
navigateTo('/dashboard/login')
|
|
}
|
|
if (error?.data?.statusMessage) {
|
|
toast(error?.data?.statusMessage)
|
|
}
|
|
return Promise.reject(error)
|
|
})
|
|
}
|