优化首页的ui交互,解决打包生产环境下写入sessionStorage错误,优化权限路由的跳转问题
This commit is contained in:
+30
-11
@@ -38,19 +38,21 @@ export const developerOnlyPaths = [
|
||||
'/settings',
|
||||
'/config-lists',
|
||||
'/document-types',
|
||||
'/prompts'
|
||||
'/prompts',
|
||||
];
|
||||
|
||||
// 创建基于Cookie的会话存储
|
||||
// 在实际应用中,应该使用环境变量来设置密钥
|
||||
const sessionStorage = createCookieSessionStorage({
|
||||
export const sessionStorage = createCookieSessionStorage({
|
||||
cookie: {
|
||||
name: "__session",
|
||||
name: "__lgsession",
|
||||
httpOnly: true,
|
||||
path: "/",
|
||||
sameSite: "lax",
|
||||
secrets: ["s3cr3t"], // 应该从环境变量读取
|
||||
secure: process.env.NODE_ENV === "production",
|
||||
// secure: process.env.NODE_ENV === "production",
|
||||
maxAge: 60 * 60 * 24 * 1, // 1天
|
||||
secure: false, // 开发环境中禁用secure
|
||||
},
|
||||
});
|
||||
|
||||
@@ -63,9 +65,18 @@ export async function getSession(request: Request) {
|
||||
// 获取用户登录状态
|
||||
export async function getUserSession(request: Request) {
|
||||
const session = await getSession(request);
|
||||
const isAuthenticated = session.get("isAuthenticated") === true;
|
||||
const userRole = session.get("userRole") || 'common' as UserRole;
|
||||
|
||||
console.log("获取会话状态:",
|
||||
// "Cookie:", request.headers.get("Cookie"),
|
||||
"是否认证:", isAuthenticated,
|
||||
"用户角色:", userRole
|
||||
);
|
||||
|
||||
return {
|
||||
isAuthenticated: session.get("isAuthenticated") === true,
|
||||
userRole: session.get("userRole") || 'common' as UserRole
|
||||
isAuthenticated,
|
||||
userRole
|
||||
};
|
||||
}
|
||||
|
||||
@@ -74,10 +85,15 @@ export async function createUserSession(isAuthenticated: boolean, userRole: User
|
||||
const session = await sessionStorage.getSession();
|
||||
session.set("isAuthenticated", isAuthenticated);
|
||||
session.set("userRole", userRole);
|
||||
console.log("session-----", session.get("userRole"));
|
||||
|
||||
const cookie = await sessionStorage.commitSession(session);
|
||||
console.log("创建会话 - 设置Cookie:", !!cookie);
|
||||
console.log("创建会话 - 用户角色:", userRole);
|
||||
console.log("创建会话 - 重定向到:", redirectTo);
|
||||
|
||||
return redirect(redirectTo, {
|
||||
headers: {
|
||||
"Set-Cookie": await sessionStorage.commitSession(session),
|
||||
"Set-Cookie": cookie,
|
||||
},
|
||||
});
|
||||
}
|
||||
@@ -117,15 +133,17 @@ export async function loader({ request }: LoaderFunctionArgs) {
|
||||
|
||||
// 获取用户会话
|
||||
const { isAuthenticated, userRole } = await getUserSession(request);
|
||||
// console.log("Auth status:", { isAuthenticated, userRole, pathname });
|
||||
console.log("是否公开路径:", isPublicPath, "是否已认证:", isAuthenticated);
|
||||
|
||||
// 如果访问需要认证的路径但未登录,重定向到登录页
|
||||
if (!isPublicPath && !isAuthenticated) {
|
||||
console.log("未认证,需要重定向到登录页");
|
||||
// 保存请求的URL,以便登录后重定向回来
|
||||
const session = await getSession(request);
|
||||
|
||||
// 如果路径是/home,则将重定向目标设置为/
|
||||
const redirectTarget = pathname === "/home" ? "/" : pathname;
|
||||
const redirectTarget = pathname !== "/" ? "/" : pathname;
|
||||
// const redirectTarget = pathname === "home" ? "/" : pathname;
|
||||
// 保存重定向目标
|
||||
session.set("redirectTo", redirectTarget);
|
||||
|
||||
@@ -138,12 +156,13 @@ export async function loader({ request }: LoaderFunctionArgs) {
|
||||
|
||||
// 如果已登录且访问登录页,重定向到首页
|
||||
if (pathname === "/login" && isAuthenticated) {
|
||||
// console.log("Already authenticated, redirecting from login to /");
|
||||
console.log("已认证,重定向到首页");
|
||||
return redirect("/");
|
||||
}
|
||||
|
||||
// 检查访问权限 - 如果是common用户访问了开发者专属页面,重定向到首页
|
||||
if (userRole === 'common' && developerOnlyPaths.some(path => pathname.startsWith(path))) {
|
||||
console.log("用户没有访问权限,重定向到首页");
|
||||
return redirect("/");
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user