diff --git a/src/operators/instance.ts b/src/operators/instance.ts index c654c8a6..7136df50 100644 --- a/src/operators/instance.ts +++ b/src/operators/instance.ts @@ -12,7 +12,7 @@ const httpClient: AxiosInstance = axios.create({ }); httpClient.interceptors.request.use((config) => { - const accessToken = store.getters.accessToken || getCookie('ACCESS_TOKEN'); + const accessToken = store.getters.accessToken; if (accessToken) { config.headers['Authorization'] = `Bearer ${accessToken}`; } diff --git a/src/pages/auth/Callback.vue b/src/pages/auth/Callback.vue index e90fa5b0..6807648c 100644 --- a/src/pages/auth/Callback.vue +++ b/src/pages/auth/Callback.vue @@ -2,6 +2,7 @@ import { defineComponent } from 'vue'; import { getCookie, removeCookie } from 'typescript-cookie'; import { getAuthBaseUrl, getDataBaseUrl } from '@/utils'; +import { removeCookies } from '@/utils/cookie'; interface IData { accessToken: string | undefined; @@ -27,15 +28,7 @@ export default defineComponent({ this.$store.dispatch('setAccessToken', this.accessToken); this.$store.dispatch('setRefreshToken', this.refreshToken); console.debug('set token successfully'); - const host = window.location.host; - // process test env and prod env, for example: - // auth.zhishuyun.com -> .zhishuyun.com - // auth.test.zhishuyun.com -> .zhishuyun.com - const domain = host.replace(/^\S+?\.(test\.)?/, '.'); - const path = '/'; - // remove tokens from cookies - removeCookie('ACCESS_TOKEN', { domain, path }); - removeCookie('REFRESH_TOKEN', { domain, path }); + removeCookies(); console.debug('remove cookie for tokens successfully'); // get user info after get access token this.$store.dispatch('getMe'); diff --git a/src/store/index.ts b/src/store/index.ts index 2713a41c..87552b31 100644 --- a/src/store/index.ts +++ b/src/store/index.ts @@ -1,6 +1,7 @@ import { createStore, ActionContext } from 'vuex'; import createPersistedState from 'vuex-persistedstate'; import { userOperator, IUser } from '@/operators'; +import { removeCookies } from '@/utils/cookie'; export interface IState { accessToken: string | undefined; @@ -24,10 +25,7 @@ const store = createStore({ state.refreshToken = payload; }, setUser(state: IState, payload: IUser): void { - state.user = { - ...state.user, - ...payload - }; + state.user = payload; } }, actions: { @@ -35,6 +33,7 @@ const store = createStore({ commit('setRefreshToken', undefined); commit('setAccessToken', undefined); commit('setUser', undefined); + removeCookies(); }, setRefreshToken({ commit }: ActionContext, payload: string) { commit('setRefreshToken', payload); diff --git a/src/utils/cookie.ts b/src/utils/cookie.ts new file mode 100644 index 00000000..1d414b76 --- /dev/null +++ b/src/utils/cookie.ts @@ -0,0 +1,13 @@ +import { removeCookie } from 'typescript-cookie'; + +export const removeCookies = () => { + const host = window.location.host; + // process test env and prod env, for example: + // auth.zhishuyun.com -> .zhishuyun.com + // auth.test.zhishuyun.com -> .zhishuyun.com + const domain = host.replace(/^\S+?\.(test\.|local\.)?/, '.'); + const path = '/'; + // remove tokens from cookies + removeCookie('ACCESS_TOKEN', { domain, path }); + removeCookie('REFRESH_TOKEN', { domain, path }); +};