Files
monkey-ai-web/src/helpers/loginFlow.js
T
zhanjunhao 2aa9d73325 chore(repo): 移除重复版权文件头并纳入共享单测
集中保留原版权声明,移除文件头检查和全局测试忽略规则。验证:全量单测、受影响用例复测及静态检查通过。
2026-11-20 11:23:49 +08:00

62 lines
1.8 KiB
JavaScript

import { API, updateAPI } from './api';
import { clearUserData, consumePostLoginRedirect, setUserData } from './data';
import { showError } from './utils';
const SESSION_NOT_PERSISTED_MESSAGE =
'登录成功但浏览器未能保存登录会话,请确认 Cookie 未被拦截后重试';
// Verifies that the session cookie issued by the login endpoint was actually
// persisted by the browser before treating the user as logged in. Some
// environments (third-party-cookie blocking, privacy extensions, mismatched
// SameSite/Secure attrs behind a proxy) silently drop the Set-Cookie, which
// previously left users stuck on a half-logged-in state that only a manual
// cache wipe could recover from. Returns true on success, false on failure.
export async function finishLogin({
data,
userDispatch,
navigate,
t = (s) => s,
redirect,
enterprise = false,
}) {
setUserData(data);
// Recreate the axios instance so any in-flight request map from a prior
// session is dropped before we issue the verification GET.
updateAPI();
let verifiedUser;
try {
const res = await API.get('/api/user/self', {
skipErrorHandler: true,
disableDuplicate: true,
});
if (
!res.data?.success ||
data?.id == null ||
String(res.data?.data?.id) !== String(data.id)
) {
throw new Error(res.data?.message || 'session verify failed');
}
verifiedUser = res.data.data;
} catch {
clearUserData();
updateAPI();
userDispatch({ type: 'logout' });
showError(t(SESSION_NOT_PERSISTED_MESSAGE));
return false;
}
setUserData(verifiedUser);
userDispatch({ type: 'login', payload: verifiedUser });
navigate(
consumePostLoginRedirect({
redirect,
enterprise,
orgOnly: verifiedUser.org_only_mode === true,
}),
{ replace: true },
);
return true;
}