import { useState } from "react"; import { useActionData } from "@remix-run/react"; import { type MetaFunction, type ActionFunctionArgs, redirect, type LoaderFunctionArgs } from "@remix-run/node"; import styles from "~/styles/pages/login.css?url"; import { getUserSession, getSession, type UserRole, sessionStorage } from "~/root"; export const links = () => [ { rel: "stylesheet", href: styles } ]; export const meta: MetaFunction = () => { return [ { title: "中国烟草AI合同及卷宗审核系统 - 登录" }, { name: "description", content: "中国烟草AI合同及卷宗审核系统登录页面" }, ]; }; // 处理表单提交的action export async function action({ request }: ActionFunctionArgs) { const formData = await request.formData(); const username = formData.get("username") as string; const password = formData.get("password") as string; const userRole = formData.get("userRole") as UserRole || 'common'; console.log("userRole-----", userRole); // 简单的登录验证,实际应用中应该进行真正的身份验证 if (!username || !password) { return Response.json({ error: "用户名和密码不能为空" }); } if (userRole === 'developer') { if (username !== 'admin' || password !== 'admin') { return Response.json({ error: "管理员用户名或密码错误" }); } } // 获取session中存储的重定向URL,如果没有则默认到/ const session = await getSession(request); // 查看session中存储的redirectTo值 const redirectTo = session.get("redirectTo") || "/"; console.log("登录后重定向到:", redirectTo); // 创建会话cookie const newSession = await sessionStorage.getSession(); newSession.set("isAuthenticated", true); newSession.set("userRole", userRole); const cookie = await sessionStorage.commitSession(newSession); console.log("设置cookie:", !!cookie); // 使用新方法进行重定向 return redirect(redirectTo, { headers: { "Set-Cookie": cookie } }); } // 加载器,获取当前会话状态 export async function loader({ request }: LoaderFunctionArgs) { const { isAuthenticated } = await getUserSession(request); // 如果已登录,重定向到首页 if (isAuthenticated) { return redirect("/"); } return Response.json({ isAuthenticated }); } export default function Login() { const [username, setUsername] = useState(""); const [password, setPassword] = useState(""); const [userRole, setUserRole] = useState("common"); const actionData = useActionData(); return (
{/* 中国烟草 */}

中国烟草AI合同及卷宗审核系统

用户登录

{actionData?.error && (
{actionData.error}
)}
setUsername(e.target.value)} className="form-input" placeholder="请输入用户名" />
setPassword(e.target.value)} className="form-input" placeholder="请输入密码" />

© 2024 中国烟草 版权所有

); }