91 lines
2.9 KiB
TypeScript
91 lines
2.9 KiB
TypeScript
/**
|
|
* features/auth/composables/useLogin.ts
|
|
* 登录页编排层:组合认证、公告展示与 OAuth 回调,provide 给 LoginView 子组件。
|
|
*/
|
|
import { onMounted, reactive } from 'vue';
|
|
import { useRouter, useRoute } from 'vue-router';
|
|
import { useI18n } from 'vue-i18n';
|
|
import { config } from '@/config';
|
|
import { useTheme } from '@/shared/composables/useTheme';
|
|
import { useMessage } from '@/shared/composables/useMessage';
|
|
import { useAuthStore } from '@/features/auth/stores/auth.store';
|
|
import loginBg from '@/assets/login-bg.png';
|
|
import { DEFAULT_POST_LOGIN_AGENT_ID } from '@/features/workspace/types/workspace.types';
|
|
import { useWorkspaceLocale } from '@/features/workspace/composables/useWorkspaceLocale';
|
|
import { continuationLocation, readSsoContinuation } from '@/features/sso';
|
|
import { useLoginAuth } from './useLoginAuth';
|
|
import { useLoginLanding } from './useLoginLanding';
|
|
|
|
export function useLogin() {
|
|
const { t, locale } = useI18n();
|
|
const msg = useMessage();
|
|
const router = useRouter();
|
|
const route = useRoute();
|
|
const authStore = useAuthStore();
|
|
const { setThemePreference, appliedTheme } = useTheme();
|
|
|
|
const deps = { t, locale, msg, router, route, setThemePreference, appliedTheme };
|
|
const landing = useLoginLanding();
|
|
|
|
const completePasswordLogin = async () => {
|
|
authStore.syncFromStorage();
|
|
const continuation = readSsoContinuation();
|
|
if (continuation) {
|
|
await router.replace(continuationLocation(continuation));
|
|
return;
|
|
}
|
|
await router.replace(`/chat/${DEFAULT_POST_LOGIN_AGENT_ID}`);
|
|
};
|
|
|
|
const completeOAuthLogin = async () => {
|
|
authStore.syncFromStorage();
|
|
const continuation = readSsoContinuation();
|
|
if (continuation) {
|
|
await router.replace(continuationLocation(continuation));
|
|
return;
|
|
}
|
|
await router.replace('/workspace/applications');
|
|
};
|
|
|
|
const loginAuth = useLoginAuth(deps, { completePasswordLogin, completeOAuthLogin });
|
|
const localeApi = useWorkspaceLocale(deps);
|
|
|
|
const loginBackgroundStyle = {
|
|
backgroundImage: `url(${loginBg})`,
|
|
backgroundSize: 'cover',
|
|
backgroundPosition: 'center',
|
|
backgroundRepeat: 'no-repeat',
|
|
};
|
|
|
|
onMounted(async () => {
|
|
localeApi.loadLanguagePreference();
|
|
void landing.fetchAnnouncements();
|
|
|
|
const handledCasdoor = await loginAuth.tryCasdoorCallbackLogin();
|
|
if (handledCasdoor) return;
|
|
|
|
const handledWecom = await loginAuth.tryWecomCallbackLogin();
|
|
if (handledWecom) return;
|
|
|
|
authStore.syncFromStorage();
|
|
if (authStore.isLoggedIn) {
|
|
const continuation = readSsoContinuation();
|
|
await router.replace(continuation ? continuationLocation(continuation) : '/workspace/applications');
|
|
return;
|
|
}
|
|
|
|
loginAuth.hydrateRememberedLoginState();
|
|
await landing.fetchPublicAgents();
|
|
landing.isLoading.value = false;
|
|
});
|
|
|
|
return reactive({
|
|
config,
|
|
msg,
|
|
loginBackgroundStyle,
|
|
...localeApi,
|
|
...landing,
|
|
...loginAuth,
|
|
});
|
|
}
|