File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 11// 用户偏好设置页(Server Component)
2- // 未登录时重定向到 /login?redirect=/settings
3- import { cookies } from "next/headers" ;
2+ // 登录态由客户端 SettingsForm 内部的 useAuth 处理:token 存在 localStorage,服务端无法读取,
3+ // 所以这里不做服务端鉴权,仅负责渲染页面壳。未登录 → 客户端 router.replace 到 /login?redirect=/settings。
44import { Header } from "@/app/components/Header" ;
55import { Footer } from "@/app/components/Footer" ;
66import { SettingsForm } from "./SettingsForm" ;
77
8- async function getServerUser ( ) {
9- const cookieStore = await cookies ( ) ;
10- const token = cookieStore . get ( "satoken" ) ?. value ;
11- if ( ! token || ! process . env . BACKEND_URL ) return null ;
12- try {
13- const res = await fetch ( `${ process . env . BACKEND_URL } /auth/me` , {
14- headers : { satoken : token } ,
15- cache : "no-store" ,
16- } ) ;
17- if ( ! res . ok ) return null ;
18- const body = await res . json ( ) ;
19- return body ?. data ?? null ;
20- } catch {
21- return null ;
22- }
23- }
24-
25- export default async function SettingsPage ( ) {
26- const user = await getServerUser ( ) ;
27-
28- // satoken 存在于 localStorage 而非 cookie,服务端无法读取
29- // 因此此处 user 可能为 null;实际登录态由客户端 SettingsForm 内部处理
30- // 仅当能从服务端确认已登出时才重定向,避免误跳转
31- // (大多数情况下 user 为 null 是正常的,由客户端 useAuth 判断)
32- void user ;
33-
8+ export default function SettingsPage ( ) {
349 return (
3510 < >
3611 < Header />
You can’t perform that action at this time.
0 commit comments