Fix registration: use bcrypt directly and show real API error messages.
Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
@@ -1,8 +1,18 @@
|
||||
import { LockOutlined, UserOutlined } from '@ant-design/icons'
|
||||
import { Button, Card, Form, Input, Tabs, Typography, message } from 'antd'
|
||||
import axios from 'axios'
|
||||
import { Navigate, useNavigate } from 'react-router-dom'
|
||||
import { useAuth } from '../context/AuthContext'
|
||||
|
||||
function apiErrorMessage(error: unknown, fallback: string) {
|
||||
if (axios.isAxiosError(error)) {
|
||||
const detail = error.response?.data?.detail
|
||||
if (typeof detail === 'string') return detail
|
||||
if (Array.isArray(detail) && detail[0]?.msg) return detail[0].msg
|
||||
}
|
||||
return fallback
|
||||
}
|
||||
|
||||
export default function LoginPage() {
|
||||
const { user, login, register, loading } = useAuth()
|
||||
const navigate = useNavigate()
|
||||
@@ -16,8 +26,8 @@ export default function LoginPage() {
|
||||
await login(values.username, values.password)
|
||||
message.success('登录成功')
|
||||
navigate('/')
|
||||
} catch {
|
||||
message.error('用户名或密码错误')
|
||||
} catch (error) {
|
||||
message.error(apiErrorMessage(error, '用户名或密码错误'))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -30,8 +40,8 @@ export default function LoginPage() {
|
||||
await register(values.username, values.password)
|
||||
message.success('注册成功')
|
||||
navigate('/')
|
||||
} catch {
|
||||
message.error('注册失败,用户名可能已存在')
|
||||
} catch (error) {
|
||||
message.error(apiErrorMessage(error, '注册失败,请稍后重试'))
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user