refactor chat

This commit is contained in:
AceDataCloud committed 2023-12-30 15:23:03 +08:00
1 parent 565b367c59
commit cd5dbaa264
20 files changed
+192 -230

No files matched your search

+17 -33
View File
@@ -1,14 +1,15 @@
<script lang="ts">
import { defineComponent } from 'vue';
import { getAuthBaseUrl, getHubBaseUrl } from '@/utils';
import { oauthOperator } from '@/operators/auth/operator';
import { applicationOperator, userOperator } from '@/operators';
import { IUser } from '@/operators';
import { IToken } from '@/operators/auth/models';
import { ROUTE_AUTH_LOGIN } from '@/router';
interface IData {
accessToken: string | undefined;
refreshToken: string | undefined;
redirect: string | undefined;
code: string | undefined;
user: IUser | undefined;
}
export default defineComponent({
@@ -18,46 +19,29 @@ export default defineComponent({
accessToken: undefined,
refreshToken: undefined,
redirect: this.$route.query.redirect?.toString(),
code: this.$route.query.code?.toString()
code: this.$route.query.code?.toString(),
user: undefined
};
},
async mounted() {
// if auth code is provided, get token via oauth
if (this.code) {
console.debug('code found, try to get tokens');
const { data } = await oauthOperator.token({
code: this.code
});
this.accessToken = data.access_token;
this.refreshToken = data.refresh_token;
console.debug('get tokens successfully');
const data: IToken = await this.$store.dispatch('getToken', this.code);
this.accessToken = data.access;
this.refreshToken = data.refresh;
} else {
this.$router.push({ name: ROUTE_AUTH_LOGIN });
return;
}
// if token acquired, get user info
if (this.accessToken && this.refreshToken) {
// store token to global store
await this.$store.dispatch('common/setToken', {
refresh: this.refreshToken,
access: this.accessToken
});
console.debug('set token successfully');
const { data: user } = await userOperator.getMe();
await this.$store.dispatch('common/setUser', user);
console.debug('set user successfully');
const { data: applications } = await applicationOperator.getAll({
user_id: user.id
});
await this.$store.dispatch('common/setApplications', applications);
console.debug('set applications successfully');
const user = await this.$store.dispatch('getUser');
this.user = user;
if (this.redirect) {
this.$router.push(this.redirect);
}
} else {
console.debug('access token and refresh token not found, try to re-auth again');
const hubBaseUrl = getHubBaseUrl();
const authBaseUrl = getAuthBaseUrl();
// callback url used to init access token and then redirect back of `redirect`
const callbackUrl = `${hubBaseUrl}/auth/callback?redirect=${this.redirect}`;
// redirect to auth service to get access token then redirect back
const targetUrl = `${authBaseUrl}/auth/login?redirect=${callbackUrl}`;
window.location.href = targetUrl;
this.$router.push({ name: ROUTE_AUTH_LOGIN });
}
}
});