生产管理系统 - 2种角色的登录

This commit is contained in:
2025-10-31 14:52:30 +08:00
parent 46ff61eaed
commit ad600ce059
7 changed files with 371 additions and 25 deletions

View File

@@ -28,7 +28,7 @@ import {
import { toast } from 'sonner';
import { useAuth } from '@/components/auth/AuthContext';
import { authReducer, initialAuthState, AuthState, AuthAction } from './authReducer';
import { loginApiV1AuthLoginPost } from '@/lib/api/sdk.gen';
import { getCaptchaApiV1AuthCaptchaGet, loginApiV1AuthLoginPost } from '@/lib/api/sdk.gen';
import type { CaptchaResponse } from '@/lib/api/types.gen';
interface LoginFormProps {
@@ -128,6 +128,8 @@ export function LoginForm({ onRegisterClick }: LoginFormProps) {
enterpriseId: response.data.enterprise_id || '',
enterpriseName: response.data.enterprise_name || '',
createdAt: response.data.created_at || new Date().toISOString(),
// 重要存储token到用户对象中
token: response.data.access_token || response.data.token || null,
};
// 打印登录成功日志
@@ -137,10 +139,20 @@ export function LoginForm({ onRegisterClick }: LoginFormProps) {
timestamp: new Date().toISOString()
});
// 验证token是否正确存储
if (userData.token) {
console.log('🔑 Token已存储:', userData.token.substring(0, 20) + '...');
} else {
console.warn('⚠️ 未找到token请检查API响应格式');
}
login(userData);
toast.success('登录成功!');
// 暂时不实现页面跳转
console.log('✅ 登录流程完成,等待后续页面跳转实现');
toast.success('登录成功!正在跳转...');
// 跳转到个人中心页面
setTimeout(() => {
window.location.href = '/central-config/personal-center/personal-info';
}, 1000);
} else {
dispatch({ type: 'SET_ERROR', payload: '登录失败,请检查用户名和密码' });
toast.error('登录失败,请检查用户名和密码');

View File

@@ -1,12 +1,24 @@
'use client';
import { useEffect } from 'react';
import { AuthProvider } from '@/components/auth/AuthContext';
export default function AppLayout({
children,
}: {
children: React.ReactNode
}) {
// 使用useEffect确保主题只在客户端设置避免水合问题
useEffect(() => {
// 可以在这里添加客户端特定的初始化逻辑
}, []);
return (
<html lang="en">
<body>
{children}
<html lang="en" suppressHydrationWarning>
<body suppressHydrationWarning>
<AuthProvider>
{children}
</AuthProvider>
</body>
</html>
)

View File

@@ -1,12 +1,25 @@
export default function HomePage({
children,
}: {
children: React.ReactNode
}) {
'use client';
import { useAuth } from '@/components/auth/AuthContext';
import { LoadingScreen } from '@/components/auth/LoadingScreen';
import { redirect } from 'next/navigation';
export default function HomePage() {
const { user, loading, isAuthenticated } = useAuth();
// 如果正在加载,显示加载屏幕
if (loading) {
return <LoadingScreen />;
}
console.log('isAuthenticated:',isAuthenticated)
// 如果未认证,重定向到登录页
if (!isAuthenticated) {
redirect('/login');
}
// 用户已认证,显示主页内容
return (
<div className="">
{children}
</div>
)
<div></div>
);
}