/* Copyright (C) 2025 QuantumNous This program is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details. You should have received a copy of the GNU Affero General Public License along with this program. If not, see . For commercial licensing, please contact support@quantumnous.com */ import { API, updateAPI } from './api'; import { clearUserData, consumePostLoginRedirect, setUserData } from './data'; import { showError, showSuccess } 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, successMessage, redirectOverride, }) { 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 || !res.data?.data) { 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 }); const message = successMessage === undefined ? t('登录成功!') : successMessage; if (message) { showSuccess(message); } // org_only(仅企业资源)用户默认落企业空间,与 AuthRedirect 一致:默认落地页 // (秘钥管理)挂载即调用被 BlockOrgOnlyMode 拦截的 /api/token,会先弹报错再被 // orgConfined 踢回 /org;企业中心整体关闭时 orgConfined 不生效,会一直停在报错页 const defaultRedirect = verifiedUser.org_only_mode === true ? '/org' : undefined; navigate(redirectOverride ?? consumePostLoginRedirect(defaultRedirect), { replace: true, }); return true; }