From 6bd5bb0aa0168a9a911b59ef6768ca8c5c2392fd Mon Sep 17 00:00:00 2001 From: AceDataCloud Date: Thu, 26 Jan 2023 00:24:23 +0800 Subject: [PATCH] refine auth --- nginx.conf | 8 +-- src/App.vue | 3 - src/components/common/VerificationAlert.vue | 64 --------------------- src/operators/auth/index.ts | 2 - src/operators/auth/models.ts | 6 -- src/operators/auth/operator.ts | 11 ---- src/operators/index.ts | 1 - src/pages/auth/Callback.vue | 11 +++- src/store/index.ts | 12 +++- 9 files changed, 20 insertions(+), 98 deletions(-) delete mode 100644 src/components/common/VerificationAlert.vue delete mode 100644 src/operators/auth/index.ts delete mode 100644 src/operators/auth/models.ts delete mode 100644 src/operators/auth/operator.ts diff --git a/nginx.conf b/nginx.conf index c58bcef6..0430f459 100644 --- a/nginx.conf +++ b/nginx.conf @@ -3,15 +3,11 @@ server { server_name localhost; proxy_http_version 1.1; - location /api/me { + location /api/v1/me { proxy_pass http://auth-backend:8000; } - location /api/token { - proxy_pass http://auth-backend:8000; - } - - location /api/ { + location /api/v1/ { proxy_pass http://data-backend:8000; } diff --git a/src/App.vue b/src/App.vue index 564dc4f2..445070ac 100644 --- a/src/App.vue +++ b/src/App.vue @@ -17,9 +17,6 @@ export default defineComponent({ return { locale: zhCn }; - }, - mounted() { - this.$store.dispatch('getMe'); } }); diff --git a/src/components/common/VerificationAlert.vue b/src/components/common/VerificationAlert.vue deleted file mode 100644 index 55b50a3c..00000000 --- a/src/components/common/VerificationAlert.vue +++ /dev/null @@ -1,64 +0,0 @@ - - - diff --git a/src/operators/auth/index.ts b/src/operators/auth/index.ts deleted file mode 100644 index 4a6cbae5..00000000 --- a/src/operators/auth/index.ts +++ /dev/null @@ -1,2 +0,0 @@ -export * from './models'; -export * from './operator'; diff --git a/src/operators/auth/models.ts b/src/operators/auth/models.ts deleted file mode 100644 index 8fc7d02c..00000000 --- a/src/operators/auth/models.ts +++ /dev/null @@ -1,6 +0,0 @@ -export interface IToken { - access?: string; - refresh?: string; -} - -export interface ITokenResponse extends IToken {} diff --git a/src/operators/auth/operator.ts b/src/operators/auth/operator.ts deleted file mode 100644 index deb25c00..00000000 --- a/src/operators/auth/operator.ts +++ /dev/null @@ -1,11 +0,0 @@ -import { AxiosResponse } from 'axios'; -import { httpClient } from '../instance'; -import { ITokenResponse, IToken } from './models'; - -class AuthOperator { - async refreshToken(payload: IToken): Promise> { - return httpClient.post('/token/refresh/', payload); - } -} - -export const authOperator = new AuthOperator(); diff --git a/src/operators/index.ts b/src/operators/index.ts index 7f31d1cd..fe24ecc1 100644 --- a/src/operators/index.ts +++ b/src/operators/index.ts @@ -1,6 +1,5 @@ export * from './instance'; export * from './application'; -export * from './auth'; export * from './service'; export * from './user'; export * from './api'; diff --git a/src/pages/auth/Callback.vue b/src/pages/auth/Callback.vue index 4ce4ba82..e90fa5b0 100644 --- a/src/pages/auth/Callback.vue +++ b/src/pages/auth/Callback.vue @@ -19,19 +19,26 @@ export default defineComponent({ }; }, async mounted() { + // get tokens from cookies this.accessToken = getCookie('ACCESS_TOKEN'); this.refreshToken = getCookie('REFRESH_TOKEN'); if (this.accessToken && this.refreshToken) { - await this.$store.dispatch('setAccessToken', this.accessToken); - await this.$store.dispatch('setRefreshToken', this.refreshToken); + // store token to global store + 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 }); + console.debug('remove cookie for tokens successfully'); + // get user info after get access token + this.$store.dispatch('getMe'); if (this.redirect) { this.$router.push(this.redirect); } diff --git a/src/store/index.ts b/src/store/index.ts index 2da75cc2..2713a41c 100644 --- a/src/store/index.ts +++ b/src/store/index.ts @@ -45,9 +45,15 @@ const store = createStore({ setUser({ commit }: ActionContext, payload: IUser) { commit('setUser', payload); }, - async getMe({ commit }: ActionContext) { - const { data: data } = await userOperator.getMe(); - commit('setUser', data); + getMe({ commit }: ActionContext) { + userOperator + .getMe() + .then(({ data: data }) => { + commit('setUser', data); + }) + .catch(() => { + console.error('failed to get user info'); + }); } }, getters: {