From 4577df371a33e873965212ab029a6a2f2a3cb443 Mon Sep 17 00:00:00 2001 From: "maxiaozhe@alicorns.co.jp" Date: Thu, 9 Nov 2023 13:47:21 +0900 Subject: [PATCH] =?UTF-8?q?Token=E7=84=A1=E5=8A=B9=E3=81=AE=E9=9A=9B?= =?UTF-8?q?=E5=86=8D=E3=83=AD=E3=82=B0=E3=82=A4=E3=83=B3=E5=AF=BE=E5=BF=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- frontend/src/boot/axios.ts | 19 ++++- frontend/src/components/AppSelect.vue | 59 +++++++++++--- frontend/src/components/FieldSelect.vue | 2 +- frontend/src/router/index.ts | 101 ++++++++++++------------ frontend/src/stores/useAuthStore.ts | 5 +- 5 files changed, 120 insertions(+), 66 deletions(-) diff --git a/frontend/src/boot/axios.ts b/frontend/src/boot/axios.ts index 1e22234..53f50c1 100644 --- a/frontend/src/boot/axios.ts +++ b/frontend/src/boot/axios.ts @@ -1,5 +1,6 @@ import { boot } from 'quasar/wrappers'; import axios, { AxiosInstance } from 'axios'; +import {router} from 'src/router'; declare module '@vue/runtime-core' { interface ComponentCustomProperties { @@ -15,7 +16,23 @@ declare module '@vue/runtime-core' { // "export default () => {}" function below (which runs individually // for each client) const api:AxiosInstance = axios.create({ baseURL: process.env.KAB_BACKEND_URL }); - +const token=localStorage.getItem('token')||''; +if(token!==''){ + api.defaults.headers["Authorization"]='Bearer ' + token; +} +api.interceptors.response.use( + (response)=>response, + (error)=>{ + const orgReq=error.config; + if(error.response && error.response.status===401){ + localStorage.removeItem('token'); + router.replace({ + path:"/login", + query:{redirect:router.currentRoute.value.fullPath} + }); + } + } +) export default boot(({ app }) => { // for use inside Vue files (Options API) through this.$axios and this.$api diff --git a/frontend/src/components/AppSelect.vue b/frontend/src/components/AppSelect.vue index 79d0452..809c6a7 100644 --- a/frontend/src/components/AppSelect.vue +++ b/frontend/src/components/AppSelect.vue @@ -1,11 +1,20 @@ - + diff --git a/frontend/src/components/FieldSelect.vue b/frontend/src/components/FieldSelect.vue index 7f319f8..66fdc66 100644 --- a/frontend/src/components/FieldSelect.vue +++ b/frontend/src/components/FieldSelect.vue @@ -1,6 +1,6 @@