|
1 | | -'use client'; |
2 | | -import { postUserLogin, SvcAuthFrontendGetAuth } from '@/api'; |
3 | | -import { Message } from '@/components'; |
4 | | -import { AuthContext } from '@/components/authProvider'; |
5 | | -import LoadingBtn from '@/components/loadingButton'; |
6 | | -import { aesCbcEncrypt } from '@/utils/aes'; |
7 | | -import { zodResolver } from '@hookform/resolvers/zod'; |
8 | | -import { Stack, TextField } from '@mui/material'; |
9 | | -import { useLocalStorageState } from 'ahooks'; |
10 | | -import Cookies from 'js-cookie'; |
11 | | -import { useContext, useEffect } from 'react'; |
12 | | -import { useForm } from 'react-hook-form'; |
13 | | -import { useSearchParams, useRouter } from 'next/navigation'; |
14 | | -import z from 'zod'; |
| 1 | +'use client' |
| 2 | +import { postUserLogin, SvcAuthFrontendGetAuth } from '@/api' |
| 3 | +import { Message } from '@/components' |
| 4 | +import { AuthContext } from '@/components/authProvider' |
| 5 | +import LoadingBtn from '@/components/loadingButton' |
| 6 | +import { useForum } from '@/contexts/ForumContext' |
| 7 | +import { aesCbcEncrypt } from '@/utils/aes' |
| 8 | +import { zodResolver } from '@hookform/resolvers/zod' |
| 9 | +import { Stack, TextField } from '@mui/material' |
| 10 | +import { useLocalStorageState } from 'ahooks' |
| 11 | +import Cookies from 'js-cookie' |
| 12 | +import { useContext, useEffect } from 'react' |
| 13 | +import { useForm } from 'react-hook-form' |
| 14 | +import { useSearchParams, useRouter } from 'next/navigation' |
| 15 | +import z from 'zod' |
15 | 16 |
|
16 | 17 | const schema = z.object({ |
17 | 18 | email: z.string().email('邮箱格式不正确').default(''), |
18 | 19 | password: z.string().min(5, '密码不能少于 5 位').default(''), |
19 | | -}); |
| 20 | +}) |
20 | 21 |
|
21 | | -const Account = ({ isChecked, passwordConfig }: { isChecked: boolean, passwordConfig?: SvcAuthFrontendGetAuth }) => { |
| 22 | +const Account = ({ isChecked, passwordConfig }: { isChecked: boolean; passwordConfig?: SvcAuthFrontendGetAuth }) => { |
22 | 23 | const [, setToken] = useLocalStorageState<string>('auth_token', { |
23 | 24 | defaultValue: '', |
24 | | - }); |
25 | | - const { user, loading, fetchUser } = useContext(AuthContext); |
26 | | - const searchParams = useSearchParams(); |
27 | | - const router = useRouter(); |
28 | | - const redirectUrl = searchParams?.get('redirect'); |
| 25 | + }) |
| 26 | + const { user, loading, fetchUser } = useContext(AuthContext) |
| 27 | + const { refreshForums } = useForum() |
| 28 | + const searchParams = useSearchParams() |
| 29 | + const router = useRouter() |
| 30 | + const redirectUrl = searchParams?.get('redirect') |
29 | 31 |
|
30 | 32 | const { |
31 | 33 | register, |
32 | 34 | handleSubmit, |
33 | 35 | formState: { errors }, |
34 | 36 | } = useForm({ |
35 | 37 | resolver: zodResolver(schema), |
36 | | - }); |
| 38 | + }) |
37 | 39 |
|
38 | 40 | useEffect(() => { |
39 | | - console.log('[Login Account] useEffect triggered:', { |
40 | | - loading, |
41 | | - userEmail: user.email, |
42 | | - userUid: user.uid, |
43 | | - redirectUrl, |
44 | | - userObject: user |
45 | | - }); |
46 | | - |
47 | 41 | // 只有在用户数据加载完成且确实有用户信息时才检查登录状态 |
| 42 | + // 但是要避免在登录过程中自动重定向,让登录逻辑自己处理跳转 |
48 | 43 | if (!loading && user.email && user.uid && user.uid > 0) { |
49 | | - console.log('[Login Account] User is authenticated, redirecting to:', redirectUrl || '/'); |
50 | | - // 如果用户已登录,重定向到指定页面或首页 |
51 | | - const targetUrl = redirectUrl || '/'; |
52 | | - // 使用 Next.js 路由进行客户端跳转 |
53 | | - router.replace(targetUrl); |
54 | | - } else if (!loading && user.uid === 0) { |
55 | | - // 明确知道用户未登录,不需要进一步处理 |
56 | | - console.log('[Login Account] User is not authenticated'); |
57 | | - } else { |
58 | | - console.log('[Login Account] User state is still loading or indeterminate'); |
| 44 | + // 注释掉自动重定向,让登录成功后的逻辑自己处理 |
| 45 | + // const targetUrl = redirectUrl || '/' |
| 46 | + // router.replace(targetUrl) |
59 | 47 | } |
60 | | - }, [loading, user.email, user.uid, redirectUrl]); |
| 48 | + }, [loading, user.email, user.uid, redirectUrl]) |
61 | 49 |
|
62 | 50 | // 监听认证清除事件,避免在登出后重复检查 |
63 | 51 | useEffect(() => { |
64 | 52 | const handleAuthCleared = () => { |
65 | | - console.log('[Login Account] Auth cleared event received, user should be logged out'); |
66 | 53 | // 认证清除后,确保用户状态为未登录 |
67 | 54 | // 这里不需要额外处理,因为 AuthProvider 已经会重置用户状态 |
68 | | - }; |
| 55 | + } |
69 | 56 |
|
70 | 57 | if (typeof window !== 'undefined') { |
71 | | - window.addEventListener('auth:cleared', handleAuthCleared); |
| 58 | + window.addEventListener('auth:cleared', handleAuthCleared) |
72 | 59 | return () => { |
73 | | - window.removeEventListener('auth:cleared', handleAuthCleared); |
74 | | - }; |
| 60 | + window.removeEventListener('auth:cleared', handleAuthCleared) |
| 61 | + } |
75 | 62 | } |
76 | | - }, []); |
| 63 | + }, []) |
77 | 64 | const onSubmit = (data: z.infer<typeof schema>) => { |
78 | | - const { password, email } = data; |
79 | | - const ciphertext = aesCbcEncrypt(password?.trim()); |
| 65 | + const { password, email } = data |
| 66 | + const ciphertext = aesCbcEncrypt(password?.trim()) |
80 | 67 | return postUserLogin({ email, password: ciphertext }) |
81 | 68 | .then(async (res) => { |
82 | | - setToken(res); |
| 69 | + setToken(res) |
83 | 70 | Cookies.set('auth_token', res, { |
84 | 71 | path: '/', |
85 | 72 | expires: 7, // 7 天 |
86 | 73 | sameSite: 'Lax', |
87 | | - }); |
| 74 | + }) |
88 | 75 | await fetchUser() |
| 76 | + |
| 77 | + // 直接调用refreshForums刷新论坛数据 |
| 78 | + const refreshedForums = await refreshForums() |
| 79 | + |
89 | 80 | // 登录成功后重定向 |
90 | | - const targetUrl = redirectUrl || '/'; |
91 | | - router.replace(targetUrl); |
| 81 | + let targetUrl = redirectUrl |
| 82 | + |
| 83 | + if (!targetUrl) { |
| 84 | + if (refreshedForums && refreshedForums.length > 0) { |
| 85 | + const forumUrl = `/forum/${refreshedForums[0].id}` |
| 86 | + router.replace(forumUrl) |
| 87 | + } else { |
| 88 | + router.replace('/') |
| 89 | + } |
| 90 | + } else { |
| 91 | + router.replace(targetUrl) |
| 92 | + } |
92 | 93 | }) |
93 | 94 | .catch((e) => { |
94 | | - Message.error(e || '登录失败'); |
95 | | - }); |
96 | | - }; |
| 95 | + Message.error(e || '登录失败') |
| 96 | + }) |
| 97 | + } |
97 | 98 |
|
98 | 99 | return ( |
99 | 100 | <Stack gap={2} sx={{ width: '100%', alignItems: 'center' }}> |
@@ -131,7 +132,7 @@ const Account = ({ isChecked, passwordConfig }: { isChecked: boolean, passwordCo |
131 | 132 | {passwordConfig?.button_desc || '登录'} |
132 | 133 | </LoadingBtn> |
133 | 134 | </Stack> |
134 | | - ); |
135 | | -}; |
| 135 | + ) |
| 136 | +} |
136 | 137 |
|
137 | | -export default Account; |
| 138 | +export default Account |
0 commit comments