Updated the verification

This commit is contained in:
Tiago Ribeiro
2023-09-21 23:17:50 +01:00
parent 9a45f53062
commit 1e4316a57e
2 changed files with 61 additions and 39 deletions

View File

@@ -36,6 +36,7 @@ export function getServerSideProps({query, res}: {query: {oobCode: string; mode:
export default function Reset({code, mode, apiKey, continueUrl}: {code: string; mode: string; apiKey?: string; continueUrl?: string}) { export default function Reset({code, mode, apiKey, continueUrl}: {code: string; mode: string; apiKey?: string; continueUrl?: string}) {
const [password, setPassword] = useState(""); const [password, setPassword] = useState("");
const [isLoading, setIsLoading] = useState(false); const [isLoading, setIsLoading] = useState(false);
const [email, setEmail] = useState<string>();
const router = useRouter(); const router = useRouter();
@@ -44,29 +45,35 @@ export default function Reset({code, mode, apiKey, continueUrl}: {code: string;
redirectIfFound: true, redirectIfFound: true,
}); });
useEffect(() => { const verifyEmail = (e: any) => {
if (mode === "signIn") { e.preventDefault();
axios
.post<{ok: boolean}>("/api/reset/verify", {
link: `https://encoach.com/action?apiKey=${apiKey}&mode=${mode}&oobCode=${code}&continueUrl=${continueUrl}`,
})
.then((response) => {
if (response.data.ok) {
toast.success("Your account has been verified!", {toastId: "verify-successful"});
setTimeout(() => {
router.push("/");
}, 2000);
return;
}
toast.error("Something went wrong! Please make sure to click the link in your e-mail again!", {toastId: "verify-error"}); setIsLoading(true);
}) axios
.catch(() => { .post<{ok: boolean}>("/api/reset/verify", {
toast.error("Something went wrong! Please make sure to click the link in your e-mail again!", {toastId: "verify-error"}); link: `https://encoach.com/action?apiKey=${apiKey}&mode=${mode}&oobCode=${code}&continueUrl=${continueUrl}`,
}) email,
.finally(() => setIsLoading(false)); })
} .then((response) => {
}, [apiKey, code, continueUrl, mode, router]); if (response.data.ok) {
toast.success("Your account has been verified!", {toastId: "verify-successful"});
setTimeout(() => {
router.push("/");
}, 2000);
return;
}
toast.error("Something went wrong! Please make sure to click the link in your e-mail again and input the correct e-mail!", {
toastId: "verify-error",
});
})
.catch(() => {
toast.error("Something went wrong! Please make sure to click the link in your e-mail again and input the correct e-mail!", {
toastId: "verify-error",
});
})
.finally(() => setIsLoading(false));
};
const login = (e: FormEvent<HTMLFormElement>) => { const login = (e: FormEvent<HTMLFormElement>) => {
e.preventDefault(); e.preventDefault();
@@ -141,9 +148,28 @@ export default function Reset({code, mode, apiKey, continueUrl}: {code: string;
<p className="self-start text-sm lg:text-base font-normal text-mti-gray-cool">to your registered Email Address</p> <p className="self-start text-sm lg:text-base font-normal text-mti-gray-cool">to your registered Email Address</p>
</div> </div>
<Divider className="max-w-xs lg:max-w-md" /> <Divider className="max-w-xs lg:max-w-md" />
<form className="flex flex-col items-center gap-6 w-full -lg:px-8 lg:w-1/2" onSubmit={login}> <form className="flex flex-col items-center gap-6 w-full -lg:px-8 lg:w-1/2" onSubmit={verifyEmail}>
Your e-mail is currently being verified, please wait a second. <br /> <br /> {isLoading && (
Once it has been verified, you will be redirected to the home page. <span className="text-center">
Your e-mail is currently being verified, please wait a second. <br /> <br />
Once it has been verified, you will be redirected to the home page.
</span>
)}
{!isLoading && (
<>
<span>Please enter your e-mail to verify it</span>
<Input
name="email"
type="email"
defaultValue={email}
required
label="E-mail address"
placeholder="Enter your e-mail address"
onChange={setEmail}
/>
<Button className="w-full">Submit</Button>
</>
)}
</form> </form>
</section> </section>
)} )}

View File

@@ -11,21 +11,17 @@ const db = getFirestore(app);
export default withIronSessionApiRoute(verify, sessionOptions); export default withIronSessionApiRoute(verify, sessionOptions);
async function verify(req: NextApiRequest, res: NextApiResponse) { async function verify(req: NextApiRequest, res: NextApiResponse) {
const {link} = req.body as {link: string}; const {link, email} = req.body as {link: string; email: string};
console.log({user: req.session.user}); signInWithEmailLink(auth, email, link)
.then(async () => {
const userRef = doc(db, "users", req.session.user!.id);
await setDoc(userRef, {isVerified: true}, {merge: true});
if (req.session.user) { req.session.user = {...req.session.user!, isVerified: true};
signInWithEmailLink(auth, req.session.user.email, link) await req.session.save();
.then(async () => {
const userRef = doc(db, "users", req.session.user!.id);
await setDoc(userRef, {isVerified: true}, {merge: true});
req.session.user = {...req.session.user!, isVerified: true}; res.status(200).json({ok: true});
await req.session.save(); })
.catch(() => res.status(404).json({ok: false}));
res.status(200).json({ok: true});
})
.catch(() => res.status(404).json({ok: false}));
}
} }