fix: replay pending action after login
This commit is contained in:
@@ -32,7 +32,7 @@ export function AuthGuard({ children }: AuthGuardProps) {
|
|||||||
return (
|
return (
|
||||||
<div className="flex min-h-[50vh] flex-col items-center justify-center gap-4 text-center">
|
<div className="flex min-h-[50vh] flex-col items-center justify-center gap-4 text-center">
|
||||||
<p className="text-sm text-muted-foreground">请先登录后继续访问此页面</p>
|
<p className="text-sm text-muted-foreground">请先登录后继续访问此页面</p>
|
||||||
<Button onClick={openLoginDialog}>登录</Button>
|
<Button onClick={() => openLoginDialog()}>登录</Button>
|
||||||
</div>
|
</div>
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -16,6 +16,7 @@ import { Input } from "@/components/ui/input"
|
|||||||
import { Label } from "@/components/ui/label"
|
import { Label } from "@/components/ui/label"
|
||||||
import { currentUser } from "@/lib/mock"
|
import { currentUser } from "@/lib/mock"
|
||||||
import { useAuthStore } from "@/store/auth"
|
import { useAuthStore } from "@/store/auth"
|
||||||
|
import { useLoginDialogStore } from "@/store/login-dialog"
|
||||||
|
|
||||||
const loginSchema = z.object({
|
const loginSchema = z.object({
|
||||||
phone: z.string().min(11, "请输入正确的手机号"),
|
phone: z.string().min(11, "请输入正确的手机号"),
|
||||||
@@ -30,6 +31,7 @@ interface LoginDialogProps {
|
|||||||
export function LoginDialog({ open, onOpenChange }: LoginDialogProps) {
|
export function LoginDialog({ open, onOpenChange }: LoginDialogProps) {
|
||||||
const router = useRouter()
|
const router = useRouter()
|
||||||
const { login } = useAuthStore()
|
const { login } = useAuthStore()
|
||||||
|
const consumePendingAction = useLoginDialogStore((state) => state.consumePendingAction)
|
||||||
const {
|
const {
|
||||||
register,
|
register,
|
||||||
handleSubmit,
|
handleSubmit,
|
||||||
@@ -41,6 +43,7 @@ export function LoginDialog({ open, onOpenChange }: LoginDialogProps) {
|
|||||||
const onSubmit = async (_data: z.infer<typeof loginSchema>) => {
|
const onSubmit = async (_data: z.infer<typeof loginSchema>) => {
|
||||||
await new Promise((r) => setTimeout(r, 500))
|
await new Promise((r) => setTimeout(r, 500))
|
||||||
login(currentUser, ["consumer", "player", "owner"])
|
login(currentUser, ["consumer", "player", "owner"])
|
||||||
|
consumePendingAction()
|
||||||
onOpenChange(false)
|
onOpenChange(false)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -13,7 +13,7 @@ export function useRequireAuth() {
|
|||||||
if (isAuthenticated) {
|
if (isAuthenticated) {
|
||||||
action()
|
action()
|
||||||
} else {
|
} else {
|
||||||
openLoginDialog()
|
openLoginDialog(action)
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
[isAuthenticated, openLoginDialog],
|
[isAuthenticated, openLoginDialog],
|
||||||
|
|||||||
+12
-4
@@ -2,12 +2,20 @@ import { create } from "zustand"
|
|||||||
|
|
||||||
interface LoginDialogState {
|
interface LoginDialogState {
|
||||||
open: boolean
|
open: boolean
|
||||||
openLoginDialog: () => void
|
pendingAction: (() => void) | null
|
||||||
|
openLoginDialog: (action?: () => void) => void
|
||||||
closeLoginDialog: () => void
|
closeLoginDialog: () => void
|
||||||
|
consumePendingAction: () => void
|
||||||
}
|
}
|
||||||
|
|
||||||
export const useLoginDialogStore = create<LoginDialogState>((set) => ({
|
export const useLoginDialogStore = create<LoginDialogState>((set, get) => ({
|
||||||
open: false,
|
open: false,
|
||||||
openLoginDialog: () => set({ open: true }),
|
pendingAction: null,
|
||||||
closeLoginDialog: () => set({ open: false }),
|
openLoginDialog: (action) => set({ open: true, pendingAction: action ?? null }),
|
||||||
|
closeLoginDialog: () => set({ open: false, pendingAction: null }),
|
||||||
|
consumePendingAction: () => {
|
||||||
|
const action = get().pendingAction
|
||||||
|
set({ pendingAction: null })
|
||||||
|
action?.()
|
||||||
|
},
|
||||||
}))
|
}))
|
||||||
|
|||||||
Reference in New Issue
Block a user