feat(auth): complete verification state machine with resubmit flow
This commit is contained in:
@@ -6,9 +6,12 @@ interface AuthState {
|
||||
currentRole: UserRole
|
||||
verifiedRoles: UserRole[]
|
||||
verificationStatus: Partial<Record<UserRole, VerificationStatus>>
|
||||
verificationReasons: Partial<Record<UserRole, string>>
|
||||
user: User | null
|
||||
switchRole: (role: UserRole) => void
|
||||
submitVerification: (role: UserRole) => void
|
||||
approveVerification: (role: UserRole) => void
|
||||
rejectVerification: (role: UserRole, reason: string) => void
|
||||
login: (user: User, verifiedRoles?: UserRole[]) => void
|
||||
logout: () => void
|
||||
}
|
||||
@@ -18,6 +21,7 @@ export const useAuthStore = create<AuthState>((set, get) => ({
|
||||
currentRole: "consumer",
|
||||
verifiedRoles: ["consumer"],
|
||||
verificationStatus: { consumer: "approved" },
|
||||
verificationReasons: {},
|
||||
user: null,
|
||||
switchRole: (role) => {
|
||||
const { verifiedRoles } = get()
|
||||
@@ -31,11 +35,52 @@ export const useAuthStore = create<AuthState>((set, get) => ({
|
||||
return state
|
||||
}
|
||||
|
||||
const nextReasons = { ...state.verificationReasons }
|
||||
delete nextReasons[role]
|
||||
|
||||
return {
|
||||
verificationStatus: {
|
||||
...state.verificationStatus,
|
||||
[role]: "pending",
|
||||
},
|
||||
verificationReasons: nextReasons,
|
||||
}
|
||||
}),
|
||||
approveVerification: (role) =>
|
||||
set((state) => {
|
||||
if (state.verifiedRoles.includes(role) && state.verificationStatus[role] === "approved") {
|
||||
return state
|
||||
}
|
||||
|
||||
const nextReasons = { ...state.verificationReasons }
|
||||
delete nextReasons[role]
|
||||
|
||||
return {
|
||||
verifiedRoles: state.verifiedRoles.includes(role)
|
||||
? state.verifiedRoles
|
||||
: [...state.verifiedRoles, role],
|
||||
verificationStatus: {
|
||||
...state.verificationStatus,
|
||||
[role]: "approved",
|
||||
},
|
||||
verificationReasons: nextReasons,
|
||||
}
|
||||
}),
|
||||
rejectVerification: (role, reason) =>
|
||||
set((state) => {
|
||||
if (state.verifiedRoles.includes(role)) {
|
||||
return state
|
||||
}
|
||||
|
||||
return {
|
||||
verificationStatus: {
|
||||
...state.verificationStatus,
|
||||
[role]: "rejected",
|
||||
},
|
||||
verificationReasons: {
|
||||
...state.verificationReasons,
|
||||
[role]: reason.trim() || "认证资料不完整,请补充后重试",
|
||||
},
|
||||
}
|
||||
}),
|
||||
login: (user, verifiedRoles = ["consumer"]) =>
|
||||
@@ -51,6 +96,7 @@ export const useAuthStore = create<AuthState>((set, get) => ({
|
||||
},
|
||||
{},
|
||||
),
|
||||
verificationReasons: {},
|
||||
}),
|
||||
logout: () =>
|
||||
set({
|
||||
@@ -58,6 +104,7 @@ export const useAuthStore = create<AuthState>((set, get) => ({
|
||||
currentRole: "consumer",
|
||||
verifiedRoles: ["consumer"],
|
||||
verificationStatus: { consumer: "approved" },
|
||||
verificationReasons: {},
|
||||
user: null,
|
||||
}),
|
||||
}))
|
||||
|
||||
Reference in New Issue
Block a user