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; }