refine auth

This commit is contained in:
AceDataCloud committed 2023-01-26 00:24:23 +08:00
1 parent 679faf9fc2
commit 6bd5bb0aa0
9 files changed
+20 -98

No files matched your search

+2 -6
View File
@@ -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;
}
-3
View File
@@ -17,9 +17,6 @@ export default defineComponent({
return {
locale: zhCn
};
},
mounted() {
this.$store.dispatch('getMe');
}
});
</script>
@@ -1,64 +0,0 @@
<template>
<div></div>
</template>
<script lang="ts">
import { defineComponent } from 'vue';
import { ElMessageBox } from 'element-plus';
import { authOperator, ITokenResponse } from '@/operators';
import { getAuthBaseUrl } from '@/utils';
export default defineComponent({
name: 'VerificationAlert',
computed: {
verified() {
return this.$store.getters.user.isVerified;
},
refresh() {
return this.$route.query.refresh;
}
},
async mounted() {
if (this.refresh) {
authOperator
.refreshToken({
refresh: this.$store.getters.refreshToken
})
.then(async ({ data: data }: { data: ITokenResponse }) => {
await this.$store.dispatch('setAccessToken', data.access);
await this.$store.dispatch('getMe');
this.$router.push({
...this.$route,
query: {}
});
this.checkAndShowVerificationAlert();
});
} else {
this.checkAndShowVerificationAlert();
}
},
methods: {
checkAndShowVerificationAlert() {
if (this.verified) {
return;
}
ElMessageBox.alert(this.$t('auth.message.needVerification'), this.$t('common.message.info'), {
confirmButtonText: this.$t('common.button.verify'),
// cancelButtonText: this.$t('common.button.cancel'),
type: 'warning',
center: true,
showClose: false,
showCancelButton: false,
closeOnClickModal: false,
closeOnPressEscape: false,
closeOnHashChange: false
})
.then(() => {
const authBaseUrl = getAuthBaseUrl();
window.open(`${authBaseUrl}/user/verify?redirect=` + window.location.href + '?refresh=1');
})
.catch(() => {});
}
}
});
</script>
-2
View File
@@ -1,2 +0,0 @@
export * from './models';
export * from './operator';
-6
View File
@@ -1,6 +0,0 @@
export interface IToken {
access?: string;
refresh?: string;
}
export interface ITokenResponse extends IToken {}
-11
View File
@@ -1,11 +0,0 @@
import { AxiosResponse } from 'axios';
import { httpClient } from '../instance';
import { ITokenResponse, IToken } from './models';
class AuthOperator {
async refreshToken(payload: IToken): Promise<AxiosResponse<ITokenResponse>> {
return httpClient.post('/token/refresh/', payload);
}
}
export const authOperator = new AuthOperator();
-1
View File
@@ -1,6 +1,5 @@
export * from './instance';
export * from './application';
export * from './auth';
export * from './service';
export * from './user';
export * from './api';
+9 -2
View File
@@ -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);
}
+9 -3
View File
@@ -45,9 +45,15 @@ const store = createStore({
setUser({ commit }: ActionContext<IState, IState>, payload: IUser) {
commit('setUser', payload);
},
async getMe({ commit }: ActionContext<IState, IState>) {
const { data: data } = await userOperator.getMe();
commit('setUser', data);
getMe({ commit }: ActionContext<IState, IState>) {
userOperator
.getMe()
.then(({ data: data }) => {
commit('setUser', data);
})
.catch(() => {
console.error('failed to get user info');
});
}
},
getters: {