2
0
Files
2024-08-09 00:39:27 +02:00

75 lines
1.9 KiB
TypeScript

import { useState } from "react";
import { useLocale } from "@calcom/lib/hooks/useLocale";
import { trpc } from "@calcom/trpc/react";
export type UseVerifyCodeReturnType = ReturnType<typeof useVerifyCode>;
type UseVerifyCodeProps = {
onSuccess: (isVerified: boolean) => void;
};
export const useVerifyCode = ({ onSuccess }: UseVerifyCodeProps) => {
const { t } = useLocale();
const [isPending, setIsPending] = useState(false);
const [error, setError] = useState("");
const [value, setValue] = useState("");
const [hasVerified, setHasVerified] = useState(false);
const verifyCodeMutationUserSessionRequired = trpc.viewer.organizations.verifyCode.useMutation({
onSuccess: (data) => {
setIsPending(false);
onSuccess(data);
},
onError: (err) => {
setIsPending(false);
setHasVerified(false);
if (err.message === "invalid_code") {
setError(t("code_provided_invalid"));
}
},
});
const verifyCodeMutationUserSessionNotRequired = trpc.viewer.auth.verifyCodeUnAuthenticated.useMutation({
onSuccess: (data) => {
setIsPending(false);
onSuccess(data);
},
onError: (err) => {
setIsPending(false);
setHasVerified(false);
if (err.message === "invalid_code") {
setError(t("code_provided_invalid"));
}
},
});
const verifyCodeWithSessionRequired = (code: string, email: string) => {
verifyCodeMutationUserSessionRequired.mutate({
code,
email,
});
};
const verifyCodeWithSessionNotRequired = (code: string, email: string) => {
verifyCodeMutationUserSessionNotRequired.mutate({
code,
email,
});
};
return {
verifyCodeWithSessionRequired,
verifyCodeWithSessionNotRequired,
isPending,
setIsPending,
error,
value,
hasVerified,
setValue,
setHasVerified,
resetErrors: () => setError(""),
};
};