feat: email verify state
This commit is contained in:
@ -1,6 +1,11 @@
|
||||
import { create } from "zustand";
|
||||
import { useAuth } from "./auth";
|
||||
import type { UserProfile } from "@/types";
|
||||
import {
|
||||
confirmEmailApi,
|
||||
requestEmailOtpApi,
|
||||
type ConfirmEmailRequest,
|
||||
} from "@/api/verify";
|
||||
import { useAuth } from "./auth";
|
||||
|
||||
export type VerifyStep = "email" | "avatar" | "review";
|
||||
|
||||
@ -8,11 +13,22 @@ export interface IVerifyState {
|
||||
step: VerifyStep | null;
|
||||
|
||||
loadStep: (profile: UserProfile) => void;
|
||||
|
||||
requesting: boolean;
|
||||
requested: boolean;
|
||||
requestOTP: () => Promise<void>;
|
||||
|
||||
confirming: boolean;
|
||||
confirmOTP: (req: ConfirmEmailRequest) => Promise<void>;
|
||||
}
|
||||
|
||||
export const useVerify = create<IVerifyState>((set) => ({
|
||||
step: null,
|
||||
|
||||
requesting: false,
|
||||
requested: false,
|
||||
confirming: false,
|
||||
|
||||
loadStep: (profile) => {
|
||||
if (!profile.email_verified) {
|
||||
set({ step: "email" });
|
||||
@ -31,4 +47,30 @@ export const useVerify = create<IVerifyState>((set) => ({
|
||||
|
||||
set({ step: null });
|
||||
},
|
||||
|
||||
requestOTP: async () => {
|
||||
set({ requesting: true, requested: false });
|
||||
|
||||
try {
|
||||
await requestEmailOtpApi();
|
||||
set({ requested: true });
|
||||
} catch (err) {
|
||||
console.log("ERR: Failed to request OTP:", err);
|
||||
} finally {
|
||||
set({ requesting: false });
|
||||
}
|
||||
},
|
||||
|
||||
confirmOTP: async (req: ConfirmEmailRequest) => {
|
||||
set({ confirming: true });
|
||||
|
||||
try {
|
||||
await confirmEmailApi(req);
|
||||
await useAuth.getState().authenticate();
|
||||
} catch (err) {
|
||||
console.log("ERR: Failed to request OTP:", err);
|
||||
} finally {
|
||||
set({ confirming: false });
|
||||
}
|
||||
},
|
||||
}));
|
||||
|
Reference in New Issue
Block a user