From abbfc84b2fe7510955a0db9b9eabb534d09fd795 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=B4=94=E5=BA=86=E6=89=8D=E4=B8=A8=E9=9D=99=E8=A7=85?= Date: Wed, 3 Jan 2024 11:32:32 +0800 Subject: [PATCH] Fix auth cookie persist path (#20) --- ...hub-d72476ab-e67b-49d9-a13f-61f48d2563f4.json | 7 +++++++ src/constants/env.ts | 3 --- src/constants/errorCode.ts | 1 - src/constants/index.ts | 1 - src/operators/instance.ts | 12 +----------- src/pages/auth/Callback.vue | 2 +- src/utils/initializer.ts | 16 ++++++++++------ 7 files changed, 19 insertions(+), 23 deletions(-) create mode 100644 change/@zhishuyun-hub-d72476ab-e67b-49d9-a13f-61f48d2563f4.json delete mode 100644 src/constants/env.ts diff --git a/change/@zhishuyun-hub-d72476ab-e67b-49d9-a13f-61f48d2563f4.json b/change/@zhishuyun-hub-d72476ab-e67b-49d9-a13f-61f48d2563f4.json new file mode 100644 index 0000000..630f3d2 --- /dev/null +++ b/change/@zhishuyun-hub-d72476ab-e67b-49d9-a13f-61f48d2563f4.json @@ -0,0 +1,7 @@ +{ + "type": "patch", + "comment": "fix bug of perist cookie", + "packageName": "@zhishuyun/hub", + "email": "cqc@cuiqingcai.com", + "dependentChangeType": "patch" +} diff --git a/src/constants/env.ts b/src/constants/env.ts deleted file mode 100644 index 21fe878..0000000 --- a/src/constants/env.ts +++ /dev/null @@ -1,3 +0,0 @@ -export const ENV_LOCAL = 'local'; -export const ENV_TEST = 'test'; -export const ENV_PRODUCTION = 'production'; diff --git a/src/constants/errorCode.ts b/src/constants/errorCode.ts index 40cadc2..ef5e4e3 100644 --- a/src/constants/errorCode.ts +++ b/src/constants/errorCode.ts @@ -1,4 +1,3 @@ -export const ERROR_CODE_UNVERIFIED = 'unverified'; export const ERROR_CODE_DUPLICATION = 'duplication'; export const ERROR_CODE_BUSY = 'busy'; export const ERROR_CODE_API_ERROR = 'api_error'; diff --git a/src/constants/index.ts b/src/constants/index.ts index 6a1b109..3777092 100644 --- a/src/constants/index.ts +++ b/src/constants/index.ts @@ -1,3 +1,2 @@ export * from './errorCode'; -export * from './env'; export * from './endpoint'; diff --git a/src/operators/instance.ts b/src/operators/instance.ts index 9a31522..f30bba4 100644 --- a/src/operators/instance.ts +++ b/src/operators/instance.ts @@ -1,4 +1,3 @@ -import router, { ROUTE_AUTH_LOGIN } from '@/router'; import store from '@/store'; import { getBaseUrlData } from '@/utils'; import axios, { AxiosInstance } from 'axios'; @@ -10,16 +9,13 @@ const httpClient: AxiosInstance = axios.create({ 'Content-type': 'application/json', Accept: 'application/json' }, - paramsSerializer: function (params) { + paramsSerializer(params) { return qs.stringify(params, { arrayFormat: 'repeat' }); } }); httpClient.interceptors.request.use((config) => { const accessToken = store.state.token?.access; - if (!config.headers) { - config.headers = {}; - } if (accessToken) { config.headers['Authorization'] = `Bearer ${accessToken}`; } @@ -33,12 +29,6 @@ httpClient.interceptors.response.use( (error) => { if (error?.response?.status === 401) { store.dispatch('resetToken'); - // router.push({ - // name: ROUTE_AUTH_LOGIN, - // query: { - // redirect: router?.currentRoute?.value?.path - // } - // }); } return Promise.reject(error); } diff --git a/src/pages/auth/Callback.vue b/src/pages/auth/Callback.vue index f5b58e7..26df870 100644 --- a/src/pages/auth/Callback.vue +++ b/src/pages/auth/Callback.vue @@ -2,7 +2,6 @@ import { defineComponent } from 'vue'; import { IUser } from '@/operators'; import { IToken } from '@/operators/auth/models'; -import { ROUTE_AUTH_LOGIN } from '@/router'; interface IData { accessToken: string | undefined; @@ -41,6 +40,7 @@ export default defineComponent({ await this.$router.push(this.redirect); } } else { + // reset token and trigger popup login window this.$store.dispatch('resetToken'); } } diff --git a/src/utils/initializer.ts b/src/utils/initializer.ts index 05a4e1a..97e4149 100644 --- a/src/utils/initializer.ts +++ b/src/utils/initializer.ts @@ -4,10 +4,14 @@ export const initializeCookies = () => { // parse the query string and set to cookies const query = new URLSearchParams(window.location.search); const inviterId = query.get('inviter_id'); - const expiration = new Date(); - expiration.setDate(expiration.getDate() + 7); - console.log('set INVITER_ID to cookies', inviterId); - setCookie('INVITER_ID', inviterId, { - expires: expiration - }); + if (inviterId) { + // set the cookie to expire in 7 days + const expiration = new Date(); + expiration.setDate(expiration.getDate() + 7); + console.log('set INVITER_ID to cookies', inviterId); + setCookie('INVITER_ID', inviterId, { + expires: expiration, + path: '/' + }); + } };