Skip to content

Commit 91d26c7

Browse files
awesomeYGawesomeYG
andauthored
refactor: 重构用户状态管理,使用AuthContext统一管理用户状态 (#46)
- 移除Header组件的initialUser参数,改为使用AuthContext - 在登录成功后调用fetchUser更新用户状态 - 简化用户状态管理逻辑,避免props传递 Co-authored-by: awesomeYG <gang.yang@chaitin.com>
1 parent 9f6f93e commit 91d26c7

File tree

3 files changed

+8
-15
lines changed

3 files changed

+8
-15
lines changed

ui/front/src/app/layout.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,7 @@ export default async function RootLayout(props: { children: React.ReactNode }) {
146146
getAuthConfigData(),
147147
getUserData()
148148
])
149+
149150
const brand = brandResponse || null
150151

151152
return (
@@ -172,7 +173,7 @@ export default async function RootLayout(props: { children: React.ReactNode }) {
172173
<AppRouterCacheProvider>
173174
<ThemeProvider theme={theme}>
174175
<CssBaseline />
175-
<Header initialUser={user} brandConfig={brand} initialForums={forums} />
176+
<Header brandConfig={brand} initialForums={forums} />
176177
<main id='main-content'>
177178
{props.children}
178179
</main>

ui/front/src/app/login/ui/account.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ const Account = ({ isChecked, passwordConfig }: { isChecked: boolean, passwordCo
2222
const [, setToken] = useLocalStorageState<string>('auth_token', {
2323
defaultValue: '',
2424
});
25-
const { user, loading } = useContext(AuthContext);
25+
const { user, loading, fetchUser } = useContext(AuthContext);
2626
const searchParams = useSearchParams();
2727
const router = useRouter();
2828
const redirectUrl = searchParams?.get('redirect');
@@ -85,7 +85,7 @@ const Account = ({ isChecked, passwordConfig }: { isChecked: boolean, passwordCo
8585
expires: 7, // 7 天
8686
sameSite: 'Lax',
8787
});
88-
88+
await fetchUser()
8989
// 登录成功后重定向
9090
const targetUrl = redirectUrl || '/';
9191
router.replace(targetUrl);

ui/front/src/components/header/index.tsx

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

33
import { ModelUserRole } from '@/api'
44
import { ModelForum, ModelSystemBrand } from '@/api/types'
5+
import { AuthContext } from '@/components/authProvider'
56
import { useAuthConfig } from '@/hooks/useAuthConfig'
67
import { useRouterWithForum } from '@/hooks/useRouterWithForum'
78
import SettingsIcon from '@mui/icons-material/Settings'
@@ -11,18 +12,17 @@ import Cookies from 'js-cookie'
1112
import Image from 'next/image'
1213
import Link from 'next/link'
1314
import { useParams, usePathname, useRouter } from 'next/navigation'
14-
import { useEffect, useState } from 'react'
15+
import { useContext, useEffect, useState } from 'react'
1516
import ForumSelector from '../ForumSelector'
1617
import LoggedInView from './loggedInView'
1718

1819
interface HeaderProps {
19-
initialUser?: any | null
2020
brandConfig: ModelSystemBrand
2121
initialForums?: ModelForum[]
2222
}
2323

24-
const Header = ({ initialUser = null, brandConfig, initialForums = [] }: HeaderProps) => {
25-
const [user, setUser] = useState(initialUser)
24+
const Header = ({ brandConfig, initialForums = [] }: HeaderProps) => {
25+
const { user } = useContext(AuthContext)
2626
const router = useRouterWithForum()
2727
const plainRouter = useRouter()
2828
const pathname = usePathname()
@@ -37,14 +37,6 @@ const Header = ({ initialUser = null, brandConfig, initialForums = [] }: HeaderP
3737

3838
// 从 authConfig 中获取配置
3939
const registrationEnabled = authConfig?.enable_register ?? true
40-
41-
useEffect(() => {
42-
if (!initialUser) {
43-
// 这里可以添加客户端获取用户信息的逻辑
44-
// 或者触发页面刷新以重新获取服务端数据
45-
}
46-
setUser(initialUser)
47-
}, [initialUser])
4840
// 使用状态来避免 hydration 不匹配
4941

5042
useEffect(() => {

0 commit comments

Comments
 (0)