2023-12-14 21:45:29 +08:00
|
|
|
import { computed, reactive, ref } from 'vue';
|
2024-04-16 00:03:00 +08:00
|
|
|
import { useRoute } from 'vue-router';
|
2021-08-13 14:22:35 +08:00
|
|
|
import { defineStore } from 'pinia';
|
2023-11-17 08:45:00 +08:00
|
|
|
import { useLoading } from '@sa/hooks';
|
2024-09-02 09:34:34 +08:00
|
|
|
import { fetchGetUserInfo, fetchLogin, fetchLogout } from '@/service/api';
|
2025-03-12 22:24:59 +08:00
|
|
|
import { useRouterPush } from '@/hooks/common/router';
|
2023-11-17 08:45:00 +08:00
|
|
|
import { localStg } from '@/utils/storage';
|
2025-03-12 22:24:59 +08:00
|
|
|
import { SetupStoreId } from '@/enum';
|
2025-03-18 22:54:52 +08:00
|
|
|
import { $t } from '@/locales';
|
2023-12-14 21:45:29 +08:00
|
|
|
import { useRouteStore } from '../route';
|
2024-06-15 00:49:24 +08:00
|
|
|
import { useTabStore } from '../tab';
|
2024-06-06 20:22:28 +08:00
|
|
|
import { clearAuthStorage, getToken } from './shared';
|
2023-11-17 08:45:00 +08:00
|
|
|
|
|
|
|
export const useAuthStore = defineStore(SetupStoreId.Auth, () => {
|
2024-04-16 00:03:00 +08:00
|
|
|
const route = useRoute();
|
2023-11-17 08:45:00 +08:00
|
|
|
const routeStore = useRouteStore();
|
2024-06-15 00:39:53 +08:00
|
|
|
const tabStore = useTabStore();
|
2024-04-15 16:20:30 +08:00
|
|
|
const { toLogin, redirectFromLogin } = useRouterPush(false);
|
2023-11-17 08:45:00 +08:00
|
|
|
const { loading: loginLoading, startLoading, endLoading } = useLoading();
|
|
|
|
|
|
|
|
const token = ref(getToken());
|
|
|
|
|
2024-06-06 20:22:28 +08:00
|
|
|
const userInfo: Api.Auth.UserInfo = reactive({
|
2024-08-16 16:33:11 +08:00
|
|
|
user: undefined,
|
2024-06-06 20:22:28 +08:00
|
|
|
roles: [],
|
2024-08-16 16:33:11 +08:00
|
|
|
permissions: []
|
2024-06-06 20:22:28 +08:00
|
|
|
});
|
2023-11-17 08:45:00 +08:00
|
|
|
|
2024-03-24 15:39:41 +08:00
|
|
|
/** is super role in static route */
|
|
|
|
const isStaticSuper = computed(() => {
|
|
|
|
const { VITE_AUTH_ROUTE_MODE, VITE_STATIC_SUPER_ROLE } = import.meta.env;
|
|
|
|
|
|
|
|
return VITE_AUTH_ROUTE_MODE === 'static' && userInfo.roles.includes(VITE_STATIC_SUPER_ROLE);
|
|
|
|
});
|
|
|
|
|
2023-12-14 21:45:29 +08:00
|
|
|
/** Is login */
|
2023-11-17 08:45:00 +08:00
|
|
|
const isLogin = computed(() => Boolean(token.value));
|
|
|
|
|
2023-12-14 21:45:29 +08:00
|
|
|
/** Reset auth store */
|
2023-11-17 08:45:00 +08:00
|
|
|
async function resetStore() {
|
|
|
|
const authStore = useAuthStore();
|
|
|
|
|
|
|
|
clearAuthStorage();
|
|
|
|
|
|
|
|
authStore.$reset();
|
|
|
|
|
2024-04-15 16:20:30 +08:00
|
|
|
if (!route.meta.constant) {
|
2023-11-17 08:45:00 +08:00
|
|
|
await toLogin();
|
2022-04-01 14:47:57 +08:00
|
|
|
}
|
2022-11-18 16:10:17 +08:00
|
|
|
|
2024-06-15 00:39:53 +08:00
|
|
|
tabStore.cacheTabs();
|
2023-11-17 08:45:00 +08:00
|
|
|
routeStore.resetStore();
|
|
|
|
}
|
2022-05-09 23:51:19 +08:00
|
|
|
|
2024-09-02 09:34:34 +08:00
|
|
|
async function logout() {
|
|
|
|
await fetchLogout();
|
|
|
|
resetStore();
|
|
|
|
}
|
|
|
|
|
2023-11-17 08:45:00 +08:00
|
|
|
/**
|
2023-12-14 21:45:29 +08:00
|
|
|
* Login
|
|
|
|
*
|
2024-03-24 15:39:41 +08:00
|
|
|
* @param [redirect=true] Whether to redirect after login. Default is `true`
|
2023-11-17 08:45:00 +08:00
|
|
|
*/
|
2025-04-24 17:36:38 +08:00
|
|
|
async function login(loginForm: Api.Auth.PwdLoginForm | Api.Auth.SocialLoginForm, redirect = true) {
|
2023-11-17 08:45:00 +08:00
|
|
|
startLoading();
|
2022-05-09 23:51:19 +08:00
|
|
|
|
2024-08-16 16:33:11 +08:00
|
|
|
const { VITE_APP_CLIENT_ID } = import.meta.env;
|
|
|
|
|
2024-09-03 12:19:57 +08:00
|
|
|
const loginData: Api.Auth.PwdLoginForm = {
|
2024-08-16 16:33:11 +08:00
|
|
|
...loginForm,
|
2025-04-24 17:36:38 +08:00
|
|
|
tenantId: loginForm.tenantId ?? '000000',
|
2024-08-16 16:33:11 +08:00
|
|
|
clientId: VITE_APP_CLIENT_ID!,
|
2025-04-24 17:36:38 +08:00
|
|
|
grantType: loginForm.grantType ?? 'password'
|
2024-08-16 16:33:11 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
const { data: loginToken, error } = await fetchLogin(loginData);
|
2023-11-17 08:45:00 +08:00
|
|
|
|
2024-01-16 01:50:12 +08:00
|
|
|
if (!error) {
|
|
|
|
const pass = await loginByToken(loginToken);
|
2023-11-17 08:45:00 +08:00
|
|
|
|
2024-01-16 01:50:12 +08:00
|
|
|
if (pass) {
|
2024-08-23 10:50:03 +08:00
|
|
|
await redirectFromLogin(redirect);
|
2024-12-26 11:07:19 +08:00
|
|
|
window.$notification?.success({
|
|
|
|
title: $t('page.login.common.loginSuccess'),
|
2025-03-18 22:54:52 +08:00
|
|
|
content: $t('page.login.common.welcomeBack', { userName: userInfo.user?.nickName }),
|
2024-12-26 11:07:19 +08:00
|
|
|
duration: 4500
|
|
|
|
});
|
2022-05-09 23:51:19 +08:00
|
|
|
}
|
2024-01-16 01:50:12 +08:00
|
|
|
} else {
|
2023-11-17 08:45:00 +08:00
|
|
|
resetStore();
|
2022-04-01 14:47:57 +08:00
|
|
|
}
|
2024-01-16 01:50:12 +08:00
|
|
|
|
|
|
|
endLoading();
|
2024-08-16 16:33:11 +08:00
|
|
|
|
|
|
|
return error ? Promise.reject(error) : Promise.resolve();
|
2022-04-01 14:47:57 +08:00
|
|
|
}
|
2023-11-17 08:45:00 +08:00
|
|
|
|
|
|
|
async function loginByToken(loginToken: Api.Auth.LoginToken) {
|
|
|
|
// 1. stored in the localStorage, the later requests need it in headers
|
2024-09-03 12:19:57 +08:00
|
|
|
localStg.set('token', loginToken.access_token!);
|
|
|
|
localStg.set('refreshToken', loginToken.refresh_token!);
|
2023-11-17 08:45:00 +08:00
|
|
|
|
2024-06-07 11:28:49 +08:00
|
|
|
// 2. get user info
|
|
|
|
const pass = await getUserInfo();
|
2024-06-06 20:22:28 +08:00
|
|
|
|
|
|
|
if (pass) {
|
2024-09-03 12:19:57 +08:00
|
|
|
token.value = loginToken.access_token!;
|
2024-06-06 20:22:28 +08:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2024-06-07 11:28:49 +08:00
|
|
|
async function getUserInfo() {
|
2024-01-16 01:50:12 +08:00
|
|
|
const { data: info, error } = await fetchGetUserInfo();
|
|
|
|
|
|
|
|
if (!error) {
|
2024-06-06 20:22:28 +08:00
|
|
|
// update store
|
2024-01-16 01:50:12 +08:00
|
|
|
Object.assign(userInfo, info);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
2023-11-17 08:45:00 +08:00
|
|
|
|
2024-01-16 01:50:12 +08:00
|
|
|
return false;
|
2023-11-17 08:45:00 +08:00
|
|
|
}
|
|
|
|
|
2024-06-07 11:28:49 +08:00
|
|
|
async function initUserInfo() {
|
|
|
|
const hasToken = getToken();
|
|
|
|
|
|
|
|
if (hasToken) {
|
|
|
|
const pass = await getUserInfo();
|
|
|
|
|
|
|
|
if (!pass) {
|
|
|
|
resetStore();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-11-17 08:45:00 +08:00
|
|
|
return {
|
|
|
|
token,
|
|
|
|
userInfo,
|
2024-03-24 15:39:41 +08:00
|
|
|
isStaticSuper,
|
2023-11-17 08:45:00 +08:00
|
|
|
isLogin,
|
|
|
|
loginLoading,
|
|
|
|
resetStore,
|
2024-06-06 20:22:28 +08:00
|
|
|
login,
|
2024-09-02 09:34:34 +08:00
|
|
|
logout,
|
2024-06-07 11:28:49 +08:00
|
|
|
initUserInfo
|
2023-11-17 08:45:00 +08:00
|
|
|
};
|
2022-01-03 22:20:10 +08:00
|
|
|
});
|