From 0dc2289008cf7662c38f098313953219e6390ec3 Mon Sep 17 00:00:00 2001 From: devmuhnnad Date: Tue, 1 Aug 2023 12:56:46 +0300 Subject: [PATCH] optional email confirmation feature --- client/pages/login.tsx | 11 +++++------ server/handlers/auth.ts | 9 ++++++++- server/queries/user.ts | 3 ++- 3 files changed, 15 insertions(+), 8 deletions(-) diff --git a/client/pages/login.tsx b/client/pages/login.tsx index 2323fb89..ca91730b 100644 --- a/client/pages/login.tsx +++ b/client/pages/login.tsx @@ -34,7 +34,7 @@ const LoginPage = () => { const { isAuthenticated } = useStoreState((s) => s.auth); const login = useStoreActions((s) => s.auth.login); const [error, setError] = useState(""); - const [verifying, setVerifying] = useState(false); + const [message, setMessage] = useState(""); const [loading, setLoading] = useState({ login: false, signup: false }); const [formState, { email, password, label }] = useFormState<{ email: string; @@ -79,8 +79,8 @@ const LoginPage = () => { if (type === "signup" && !DISALLOW_REGISTRATION) { setLoading((s) => ({ ...s, signup: true })); try { - await axios.post(APIv2.AuthSignup, { email, password }); - setVerifying(true); + const res = await axios.post(APIv2.AuthSignup, { email, password }); + setMessage(res.data.message); } catch (error) { setError(error.response.data.error); } @@ -97,10 +97,9 @@ const LoginPage = () => { return ( - {verifying ? ( + {message ? (

- A verification email has been sent to{" "} - {formState.values.email}. + {message}

) : ( diff --git a/server/handlers/auth.ts b/server/handlers/auth.ts index 7c9b2549..2f0f4e44 100644 --- a/server/handlers/auth.ts +++ b/server/handlers/auth.ts @@ -120,9 +120,16 @@ export const signup: Handler = async (req, res) => { req.user ); + if (!process.env.MAIL_HOST) { + return res + .status(201) + .send({ message: "Your account has been created successfully." }); + } await mail.verification(user); - return res.status(201).send({ message: "Verification email has been sent." }); + return res + .status(201) + .send({ message: `Verification email has been sent to ${user.email}.` }); }; export const token: Handler = async (req, res) => { diff --git a/server/queries/user.ts b/server/queries/user.ts index 12d84a3b..742556b7 100644 --- a/server/queries/user.ts +++ b/server/queries/user.ts @@ -36,7 +36,8 @@ export const add = async (params: Add, user?: User) => { email: params.email, password: params.password, verification_token: uuid(), - verification_expires: addMinutes(new Date(), 60).toISOString() + verification_expires: addMinutes(new Date(), 60).toISOString(), + verified: process.env.MAIL_HOST ? false : true }; if (user) {