Skip to content

Commit 3553661

Browse files
authored
🐛 Fix bug: login modal not automatically pop up when login session is expired
🐛 Fix bug: login modal not automatically pop up when login session is expired
2 parents b20f44a + 01b9e53 commit 3553661

File tree

4 files changed

+9
-54
lines changed

4 files changed

+9
-54
lines changed

frontend/app/[locale]/setup/page.tsx

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -47,21 +47,7 @@ export default function CreatePage() {
4747
useEffect(() => {
4848
if (!userLoading) {
4949
if (!user) {
50-
// user not logged in, show login prompt
51-
modal.confirm({
52-
title: t('login.expired.title'),
53-
icon: <ExclamationCircleOutlined />,
54-
content: t('login.expired.content'),
55-
okText: t('login.expired.okText'),
56-
cancelText: t('login.expired.cancelText'),
57-
closable: false,
58-
onOk() {
59-
openLoginModal();
60-
},
61-
onCancel() {
62-
router.push('/');
63-
}
64-
});
50+
// user not logged in, do nothing
6551
return
6652
}
6753

frontend/components/auth/sessionListeners.tsx

Lines changed: 6 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
import { useEffect, useRef } from 'react';
44
import { useRouter, usePathname } from 'next/navigation';
5-
import { App } from 'antd';
5+
import { App, Modal } from 'antd';
66
import { ExclamationCircleOutlined } from '@ant-design/icons';
77
import { authService } from '@/services/authService';
88
import { EVENTS } from '@/types/auth';
@@ -28,13 +28,6 @@ export function SessionListeners() {
2828
const showSessionExpiredModal = () => {
2929
// 若已显示过,则直接返回
3030
if (modalShownRef.current) return;
31-
32-
// 修复:在首页不显示过期弹窗
33-
if (pathname === '/' || pathname?.startsWith('/?') ||
34-
pathname?.startsWith('/zh') || pathname?.startsWith('/en')) {
35-
return;
36-
}
37-
3831
modalShownRef.current = true;
3932

4033
modal.confirm({
@@ -50,6 +43,7 @@ export function SessionListeners() {
5043
} finally {
5144
// Mark the source as session expired
5245
setIsFromSessionExpired(true);
46+
Modal.destroyAll();
5347
openLoginModal();
5448
setTimeout(() => (modalShownRef.current = false), 500);
5549
}
@@ -83,12 +77,6 @@ export function SessionListeners() {
8377
// 监听会话过期事件
8478
useEffect(() => {
8579
const handleSessionExpired = (event: CustomEvent) => {
86-
// 修复:在首页不处理会话过期事件
87-
if (pathname === '/' || pathname?.startsWith('/?') ||
88-
pathname?.startsWith('/zh') || pathname?.startsWith('/en')) {
89-
return;
90-
}
91-
9280
// 直接调用封装函数
9381
showSessionExpiredModal();
9482
};
@@ -106,19 +94,14 @@ export function SessionListeners() {
10694
// 组件初次挂载时,如果发现本地已经没有 session,也立即弹窗
10795
useEffect(() => {
10896
if (typeof window !== 'undefined') {
109-
const localSession = localStorage.getItem('session');
110-
// 修复:只在非首页且没有session时才弹窗
111-
if (!localSession && pathname &&
112-
pathname !== '/' &&
113-
!pathname.startsWith('/?') &&
114-
!pathname.startsWith('/zh') &&
115-
!pathname.startsWith('/en')) {
97+
const localSession = localStorage.getItem('session');
98+
if (!localSession) {
11699
showSessionExpiredModal();
117100
}
118101
}
119102
// 该副作用只需在首次渲染时执行一次
120103
// eslint-disable-next-line react-hooks/exhaustive-deps
121-
}, [pathname]);
104+
}, []);
122105

123106
// 会话状态检查
124107
useEffect(() => {
@@ -127,8 +110,7 @@ export function SessionListeners() {
127110
try {
128111
// 尝试获取当前会话
129112
const session = await authService.getSession();
130-
// 修复:只在chat路径且没有session时才触发过期事件
131-
if (!session && pathname?.startsWith('/chat')) {
113+
if (!session) {
132114
window.dispatchEvent(new CustomEvent(EVENTS.SESSION_EXPIRED, {
133115
detail: { message: "登录已过期,请重新登录" }
134116
}));

frontend/hooks/useAuth.ts

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -130,13 +130,7 @@ export function AuthProvider({ children }: { children: (value: AuthContextType)
130130
if (!isLoading && !user) {
131131
// 页面加载完成后,如果没有登录,则触发会话过期事件
132132
// 只在非首页路径触发,且仅在之前有会话的情况下触发
133-
// 修复:添加更严格的路径检查,避免在首页触发
134-
if (pathname &&
135-
pathname !== '/' &&
136-
!pathname.startsWith('/?') &&
137-
!pathname.startsWith('/zh') &&
138-
!pathname.startsWith('/en') &&
139-
shouldCheckSession) {
133+
if (pathname && pathname !== '/' && !pathname.startsWith('/?') && shouldCheckSession) {
140134
window.dispatchEvent(new CustomEvent(EVENTS.SESSION_EXPIRED, {
141135
detail: { message: t('auth.sessionExpired') }
142136
}));
@@ -246,7 +240,7 @@ export function AuthProvider({ children }: { children: (value: AuthContextType)
246240
window.dispatchEvent(new StorageEvent("storage", { key: "session", newValue: localStorage.getItem("session") }))
247241

248242
// If on the chat page, trigger conversation list update
249-
if (pathname === '/chat') {
243+
if (pathname.includes('/chat')) {
250244
window.dispatchEvent(new CustomEvent('conversationListUpdated'))
251245
}
252246
}, 150)

frontend/services/api.ts

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -206,13 +206,6 @@ function handleSessionExpired() {
206206
return;
207207
}
208208

209-
// 修复:在首页不触发会话过期事件
210-
const currentPath = window.location.pathname;
211-
if (currentPath === '/' || currentPath.startsWith('/?') ||
212-
currentPath.startsWith('/zh') || currentPath.startsWith('/en')) {
213-
return;
214-
}
215-
216209
// 标记正在处理中
217210
window.__isHandlingSessionExpired = true;
218211

0 commit comments

Comments
 (0)