Optimize the auth flow (#68)

Co-authored-by: AceDataCloud <office@acedata.cloud>
This commit is contained in:
GermeyandAceDataCloud authored and GitHub committed 2024-07-03 17:17:11 +00:00
1 parent 033cbc082c
commit 85aef535f6
23 files changed
+215 -245

No files matched your search

+2
View File
@@ -4,3 +4,5 @@ export * from './log';
export * from './initializer';
export * from './highlight';
export * from './is';
export * from './login';
export * from './site';
+26
View File
@@ -3,6 +3,7 @@ import favicon from '@/assets/images/favicon.ico';
import { getLocale } from '@/i18n';
import store from '@/store';
import { log } from './log';
import { IToken } from '@/models';
export const getDomain = () => {
const host = window.location.hostname;
@@ -140,3 +141,28 @@ export const initializeSite = async () => {
await store.dispatch('initializeSite');
}
};
/**
* Initialize token by code
*/
export const initializeToken = async () => {
const query = new URLSearchParams(window.location.search);
const code = query.get('code');
if (code) {
const token = await store.dispatch('getToken', code);
console.debug('get token', token);
}
};
/**
* Initialize user by token
*/
export const initializeUser = async () => {
const token = store.state.token as IToken;
if (!token?.access) {
console.debug('no access token, skip get user');
return;
}
const user = await store.dispatch('getUser');
console.debug('get user', user);
};
+11
View File
@@ -0,0 +1,11 @@
import { getBaseUrlAuth, getBaseUrlHub } from './baseUrl';
export const login = ({ redirect = '/', site = window.location.origin }: { redirect?: string; site?: string }) => {
const hubBaseUrl = getBaseUrlHub();
const authBaseUrl = getBaseUrlAuth();
// callback url used to init access token and then redirect back of `redirect`
const callbackUrl = `${hubBaseUrl}/auth/callback?redirect=${redirect}`;
// redirect to auth service to get access token then redirect back
const targetUrl = `${authBaseUrl}/auth/login?redirect=${callbackUrl}&site=${site}`;
window.location.href = targetUrl;
};