update logout logic
This commit is contained in:
1 parent
f22d21bb53
commit
7e23b014b6
4 files changed
+19
-14
No files matched your search
@@ -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}`;
|
||||
}
|
||||
|
||||
@@ -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');
|
||||
|
||||
+3
-4
@@ -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<IState, IState>, payload: string) {
|
||||
commit('setRefreshToken', payload);
|
||||
|
||||
@@ -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 });
|
||||
};
|
||||
Reference in new issue
Block a user