diff --git a/frontend/src/boot/axios.ts b/frontend/src/boot/axios.ts index 4cdfc56..e6d7cd4 100644 --- a/frontend/src/boot/axios.ts +++ b/frontend/src/boot/axios.ts @@ -1,6 +1,7 @@ import { boot } from 'quasar/wrappers'; import axios, { AxiosInstance } from 'axios'; import {router} from 'src/router'; +import { useAuthStore } from 'src/stores/useAuthStore'; declare module '@vue/runtime-core' { interface ComponentCustomProperties { @@ -15,9 +16,10 @@ declare module '@vue/runtime-core' { // good idea to move this instance creation inside of the // "export default () => {}" function below (which runs individually // for each client) +const authStore = useAuthStore() const api:AxiosInstance = axios.create({ baseURL: process.env.KAB_BACKEND_URL }); -const token=JSON.parse(localStorage.getItem('auth') ?? '{}')?.token ?? ''; -if(token!==''){ +const token = authStore.token; +if(token && token!==''){ api.defaults.headers["Authorization"]='Bearer ' + token; } //axios例外キャプチャー @@ -27,11 +29,7 @@ api.interceptors.response.use( if (error.response && error.response.status === 401) { // 認証エラーの場合再ログインする console.error('(; ゚Д゚)/認証エラー(401):', error); - localStorage.removeItem('token'); - router.replace({ - path:"/login", - query:{redirect:router.currentRoute.value.fullPath} - }); + authStore.logout() } return Promise.reject(error); }