fix(admin-web): 登录页面改用真实 API 并添加调试日志
- 移除模拟登录,改为调用 identity-service /v1/auth/login - 添加详细的 console.log 日志用于调试 - 记录 API URL、请求数据、响应和错误信息 - 移除不存在的 /register 页面链接 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
parent
b6bb772b0e
commit
f43124894d
|
|
@ -8,6 +8,8 @@ import { toast } from '@/components/common';
|
|||
import { useAppDispatch } from '@/store/redux/hooks';
|
||||
import { setCredentials } from '@/store/redux/slices/authSlice';
|
||||
import { isValidEmail, isValidPassword } from '@/utils/validators';
|
||||
import apiClient from '@/infrastructure/api/client';
|
||||
import { API_ENDPOINTS } from '@/infrastructure/api/endpoints';
|
||||
import styles from './login.module.scss';
|
||||
|
||||
/**
|
||||
|
|
@ -58,17 +60,35 @@ export default function LoginPage() {
|
|||
|
||||
setLoading(true);
|
||||
|
||||
try {
|
||||
// 模拟登录请求
|
||||
await new Promise((resolve) => setTimeout(resolve, 1500));
|
||||
const apiUrl = `${process.env.NEXT_PUBLIC_API_BASE_URL}${API_ENDPOINTS.AUTH.LOGIN}`;
|
||||
console.log('[Login] 开始登录请求');
|
||||
console.log('[Login] API URL:', apiUrl);
|
||||
console.log('[Login] 请求数据:', { email: formData.email, password: '***' });
|
||||
|
||||
try {
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
const response: any = await apiClient.post(API_ENDPOINTS.AUTH.LOGIN, {
|
||||
email: formData.email,
|
||||
password: formData.password,
|
||||
});
|
||||
|
||||
console.log('[Login] 登录响应:', response);
|
||||
|
||||
// 根据后端返回的数据结构处理
|
||||
const { user, token, accessToken, permissions } = response.data || response;
|
||||
const finalToken = token || accessToken;
|
||||
|
||||
if (!finalToken) {
|
||||
console.error('[Login] 响应中没有 token:', response);
|
||||
throw new Error('登录响应缺少 token');
|
||||
}
|
||||
|
||||
// 模拟登录成功
|
||||
dispatch(
|
||||
setCredentials({
|
||||
user: {
|
||||
user: user || {
|
||||
id: '1',
|
||||
email: formData.email,
|
||||
username: 'admin',
|
||||
username: formData.email.split('@')[0],
|
||||
nickname: '管理员',
|
||||
avatar: '',
|
||||
role: 'super_admin',
|
||||
|
|
@ -76,15 +96,32 @@ export default function LoginPage() {
|
|||
createdAt: new Date().toISOString(),
|
||||
lastLoginAt: new Date().toISOString(),
|
||||
},
|
||||
token: 'mock_token_' + Date.now(),
|
||||
permissions: ['*'],
|
||||
token: finalToken,
|
||||
permissions: permissions || ['*'],
|
||||
})
|
||||
);
|
||||
|
||||
console.log('[Login] 登录成功,跳转到 /dashboard');
|
||||
toast.success('登录成功');
|
||||
router.push('/dashboard');
|
||||
} catch {
|
||||
toast.error('登录失败,请检查账号密码');
|
||||
} catch (error: unknown) {
|
||||
console.error('[Login] 登录失败:', error);
|
||||
|
||||
// 详细记录错误信息
|
||||
if (error && typeof error === 'object' && 'response' in error) {
|
||||
const axiosError = error as { response?: { status?: number; data?: unknown }; message?: string };
|
||||
console.error('[Login] 错误状态码:', axiosError.response?.status);
|
||||
console.error('[Login] 错误响应:', axiosError.response?.data);
|
||||
console.error('[Login] 错误消息:', axiosError.message);
|
||||
|
||||
const errorMessage =
|
||||
(axiosError.response?.data as { message?: string })?.message ||
|
||||
axiosError.message ||
|
||||
'登录失败,请检查账号密码';
|
||||
toast.error(errorMessage);
|
||||
} else {
|
||||
toast.error('登录失败,请检查网络连接');
|
||||
}
|
||||
} finally {
|
||||
setLoading(false);
|
||||
}
|
||||
|
|
@ -191,13 +228,6 @@ export default function LoginPage() {
|
|||
</button>
|
||||
</form>
|
||||
|
||||
{/* 注册链接 */}
|
||||
<div className={styles.login__footer}>
|
||||
<span className={styles.login__footerText}>还没有账户?</span>
|
||||
<Link href="/register" className={styles.login__registerLink}>
|
||||
立即注册
|
||||
</Link>
|
||||
</div>
|
||||
</section>
|
||||
</div>
|
||||
);
|
||||
|
|
|
|||
Loading…
Reference in New Issue