diff --git a/apps/web/pages/api/auth/[...nextauth].tsx b/apps/web/pages/api/auth/[...nextauth].tsx index be4861284f..28e81dfb15 100644 --- a/apps/web/pages/api/auth/[...nextauth].tsx +++ b/apps/web/pages/api/auth/[...nextauth].tsx @@ -162,6 +162,7 @@ if (true) { const emailsDir = path.resolve(process.cwd(), "..", "..", "packages/emails", "templates"); providers.push( EmailProvider({ + type: "email", maxAge: 10 * 60 * 60, // Magic links are valid for 10 min only // Here we setup the sendVerificationRequest that calls the email template with the identifier (email) and token to verify. sendVerificationRequest: ({ identifier, url }) => { @@ -201,7 +202,7 @@ export default NextAuth({ signIn: "/auth/login", signOut: "/auth/logout", error: "/auth/error", // Error code passed in query string as ?error= - // verifyRequest: "/auth/verify-request", + verifyRequest: "/auth/verify", // newUser: "/auth/new", // New users will be directed here on first sign in (leave the property out if not of interest) }, providers, diff --git a/apps/web/pages/auth/verify.tsx b/apps/web/pages/auth/verify.tsx new file mode 100644 index 0000000000..69056e0e5e --- /dev/null +++ b/apps/web/pages/auth/verify.tsx @@ -0,0 +1,161 @@ +import { CheckIcon, ExclamationIcon, MailOpenIcon } from "@heroicons/react/outline"; +import { getSession, signIn } from "next-auth/react"; +import Head from "next/head"; +import { useRouter } from "next/router"; +import React, { useEffect, useRef, useState } from "react"; + +import { WEBAPP_URL } from "@calcom/lib/constants"; +import showToast from "@calcom/lib/notification"; +import Button from "@calcom/ui/Button"; + +async function sendVerificationLogin(email: string, username: string) { + await signIn("email", { + email: email.toLowerCase(), + username: username.toLowerCase(), + redirect: false, + callbackUrl: WEBAPP_URL || "https://app.cal.com", + }) + .then(() => { + showToast("Verification email sent", "success"); + }) + .catch((err) => { + showToast(err, "error"); + }); +} + +function useSendFirstVerificationLogin() { + const router = useRouter(); + const { email, username } = router.query; + const sent = useRef(false); + useEffect(() => { + if (router.isReady && !sent.current) { + (async () => { + await sendVerificationLogin(`${email}`, `${username}`); + sent.current = true; + })(); + } + }, [email, router.isReady, username]); +} + +export default function Verify() { + const router = useRouter(); + const { email, username, t, session_id, cancel } = router.query; + const [secondsLeft, setSecondsLeft] = useState(30); + + // @note: check for t=timestamp and apply disabled state and secondsLeft accordingly + // to avoid refresh to skip waiting 30 seconds to re-send email + useEffect(() => { + const lastSent = new Date(parseInt(`${t}`)); + // @note: This double round() looks ugly but it's the only way I came up to get the time difference in seconds + const difference = Math.round(Math.round(new Date().getTime() - lastSent.getTime()) / 1000); + if (difference < 30) { + // If less than 30 seconds, set the seconds left to 30 - difference + setSecondsLeft(30 - difference); + } else { + // else set the seconds left to 0 and disabled false + setSecondsLeft(0); + } + }, [t]); + // @note: here we make sure each second is decremented if disabled up to 0. + useEffect(() => { + if (secondsLeft > 0) { + const interval = setInterval(() => { + if (secondsLeft > 0) { + setSecondsLeft(secondsLeft - 1); + } + }, 1000); + return () => clearInterval(interval); + } + }, [secondsLeft]); + + // @note: check for session, redirect to webapp if session found + useEffect(() => { + let intervalId: NodeJS.Timer, redirecting: boolean; + // eslint-disable-next-line prefer-const + intervalId = setInterval(async () => { + const session = await getSession(); + if (session && !redirecting) { + // User connected using the magic link -> redirect him/her + redirecting = true; + // @note: redirect to webapp /getting-started, user will end up with two tabs open with the onboarding 'getting-started' wizard. + router.push(WEBAPP_URL + "/getting-started"); + } + }, 1000); + return () => { + intervalId && clearInterval(intervalId); + }; + }, [router]); + + useSendFirstVerificationLogin(); + + return ( +
+ + + {/* @note: Ternary can look ugly ant his might be extracted later but I think at 3 it's not yet worth + it or too hard to read. */} + {cancel + ? "Your payment failed" + : session_id + ? "Payment successful!" + : "Verify your email" + " | Cal.com"} + + +
+
+
+ {cancel ? ( + + ) : session_id ? ( + + ) : ( + + )} +
+

+ {cancel ? "Your payment failed" : session_id ? "Payment successful!" : "Check your Inbox"} +

+ {cancel && ( +

Your account has been created, but your premium has not been reserved.

+ )} +

+ We have sent an email to {email} with a link to activate your account.{" "} + {cancel && + "Once you activate your account you will be able to try purchase your premium username again or select a different one."} +

+

+ Don't see an email? Click the button below to send another email. +

+ +
+ + +
+
+
+
+ ); +} diff --git a/yarn.lock b/yarn.lock index 34f8af8b79..cd9e3deeb1 100644 --- a/yarn.lock +++ b/yarn.lock @@ -6222,16 +6222,11 @@ "@types/node" "*" form-data "^3.0.0" -"@types/node@*", "@types/node@16.9.1", "@types/node@>=12.0.0", "@types/node@>=8.1.0", "@types/node@^14.0.10 || ^16.0.0", "@types/node@^14.14.20 || ^16.0.0": +"@types/node@*", "@types/node@16.9.1", "@types/node@>=12.0.0", "@types/node@>=8.1.0", "@types/node@^12.12.6", "@types/node@^14.0.10 || ^16.0.0", "@types/node@^14.14.20 || ^16.0.0": version "16.9.1" resolved "https://registry.yarnpkg.com/@types/node/-/node-16.9.1.tgz#0611b37db4246c937feef529ddcc018cf8e35708" integrity sha512-QpLcX9ZSsq3YYUUnD3nFDY8H7wctAhQj/TFKL8Ya8v5fMm3CFXxo8zStsLAl780ltoYoo1WvKUVGBQK+1ifr7g== -"@types/node@^12.12.6": - version "12.20.55" - resolved "https://registry.yarnpkg.com/@types/node/-/node-12.20.55.tgz#c329cbd434c42164f846b909bd6f85b5537f6240" - integrity sha512-J8xLz7q2OFulZ2cyGTLE1TbbZcjpno7FaN6zdJNrgAdrJ+DZzh/uFR6YrTb4C+nXakvud8Q4+rbhoIWlYQbUFQ== - "@types/nodemailer@^6.4.4": version "6.4.4" resolved "https://registry.yarnpkg.com/@types/nodemailer/-/nodemailer-6.4.4.tgz#c265f7e7a51df587597b3a49a023acaf0c741f4b"