From 5889874720c5764df23c76be57f5b520ab75fa07 Mon Sep 17 00:00:00 2001 From: Mouriya Date: Thu, 8 Aug 2024 16:09:45 +0900 Subject: [PATCH] =?UTF-8?q?axios=E3=81=A7=E3=83=88=E3=83=BC=E3=82=AF?= =?UTF-8?q?=E3=83=B3=E5=8F=96=E5=BE=97=E3=82=92=E4=BF=AE=E6=AD=A3=E3=81=99?= =?UTF-8?q?=E3=82=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- frontend/src/boot/axios.ts | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) 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); }