重构认证逻辑,优化登录和用户信息获取流程,新增全局缓存支持
This commit is contained in:
@@ -21,12 +21,13 @@ import zod from 'zod'
|
||||
import Captcha from './captcha'
|
||||
import verify from '@/actions/auth/verify'
|
||||
import {login} from '@/actions/auth/auth'
|
||||
import {useRouter} from 'next/navigation'
|
||||
import {useRouter, useSearchParams} from 'next/navigation'
|
||||
import {toast} from 'sonner'
|
||||
import {ApiResponse} from '@/lib/api'
|
||||
import {Label} from '@/components/ui/label'
|
||||
import logo from '@/assets/logo.webp'
|
||||
import bg from './_assets/bg.webp'
|
||||
import {useProfileStore} from '@/components/providers/StoreProvider'
|
||||
|
||||
export type LoginPageProps = {}
|
||||
|
||||
@@ -157,30 +158,27 @@ export default function LoginPage(props: LoginPageProps) {
|
||||
// 调用登录函数
|
||||
const result = await login({
|
||||
username: values.username,
|
||||
password: values.password, // 使用验证码作为密码
|
||||
password: values.password,
|
||||
remember: values.remember,
|
||||
})
|
||||
|
||||
if (result.success) {
|
||||
// 登录成功
|
||||
toast.success('登录成功', {
|
||||
description: '欢迎回来!',
|
||||
})
|
||||
|
||||
// 跳转到首页或用户仪表板
|
||||
router.push('/')
|
||||
router.refresh() // 刷新页面状态
|
||||
}
|
||||
else {
|
||||
// 登录失败
|
||||
toast.error(result.message, {
|
||||
// 登录失败
|
||||
if (!result.success) {
|
||||
return toast.error(result.message, {
|
||||
description: '请检查您的手机号码和验证码',
|
||||
})
|
||||
}
|
||||
|
||||
// 登录成功
|
||||
await refreshProfile()
|
||||
router.push(redirect || '/')
|
||||
toast.success('登录成功', {
|
||||
description: '欢迎回来!',
|
||||
})
|
||||
}
|
||||
catch (e) {
|
||||
toast.error('服务器错误', {
|
||||
description: '请稍后再试',
|
||||
toast.error('登录错误', {
|
||||
description: (e as Error).message,
|
||||
})
|
||||
}
|
||||
finally {
|
||||
@@ -188,6 +186,19 @@ export default function LoginPage(props: LoginPageProps) {
|
||||
}
|
||||
}
|
||||
|
||||
// ======================
|
||||
// 重定向
|
||||
// ======================
|
||||
|
||||
const params = useSearchParams()
|
||||
const redirect = params.get('redirect')
|
||||
|
||||
const refreshProfile = useProfileStore(store=>store.refreshProfile)
|
||||
|
||||
// ======================
|
||||
// render
|
||||
// ======================
|
||||
|
||||
return (
|
||||
<main className={merge(
|
||||
`relative`,
|
||||
|
||||
Reference in New Issue
Block a user