From 819ec3c736ee8be34c32ab1e01771dad42b14f23 Mon Sep 17 00:00:00 2001 From: haseebzaki-07 Date: Sun, 27 Oct 2024 23:25:44 +0530 Subject: [PATCH 1/8] Add otp verification for registration --- backend/config/nodemailer.js | 31 +++- backend/controller/customer.controller.js | 73 ++++++++-- backend/models/customer.model.js | 12 +- backend/routes/customerRouter.js | 2 + frontend/src/components/Pages/Signup.jsx | 14 +- frontend/src/components/Pages/VerifyOtp.tsx | 2 + .../components/Pages/verifyRegisterOtp.jsx | 132 ++++++++++++++++++ frontend/src/router/index.jsx | 2 + 8 files changed, 254 insertions(+), 14 deletions(-) create mode 100644 frontend/src/components/Pages/verifyRegisterOtp.jsx diff --git a/backend/config/nodemailer.js b/backend/config/nodemailer.js index eb354cbc..923bc45c 100644 --- a/backend/config/nodemailer.js +++ b/backend/config/nodemailer.js @@ -142,4 +142,33 @@ exports.sendVerificationMail = async (email, verificationCode) => { } } -} \ No newline at end of file +} + + +exports.sendRegisterVerificationMail = async (email, verificationCode) => { + const emailText = ` + Dear Customer, + + Please use this verification code for verifying your account. Here's your code': + + RCode: ${verificationCode} + + Thank you for choosing our service. We are happy to help you. + + Best regards, + PlayCafe +`; + + try { + await transporter.sendMail({ + from: process.env.EMAIL_USER, + to: email, + subject: 'Your OTP Verification Code', + text: emailText, + }); + } catch (error) { + console.error("Error sending OTP email:", error); + throw new Error("Failed to send OTP email"); + } +} + diff --git a/backend/controller/customer.controller.js b/backend/controller/customer.controller.js index 4937e2af..73c25ceb 100644 --- a/backend/controller/customer.controller.js +++ b/backend/controller/customer.controller.js @@ -1,8 +1,10 @@ /* eslint-disable no-unused-vars */ const bcrypt = require("bcrypt"); +const crypto = require('crypto'); const { z } = require("zod"); const Customer = require("../models/customer.model"); const jwt = require("jsonwebtoken"); +const { sendRegisterVerificationMail } = require("../config/nodemailer"); // Define the schema const customerSchema = z.object({ @@ -12,9 +14,7 @@ const customerSchema = z.object({ }); async function createCustomer(req, res) { - // Validate the request body const validation = customerSchema.safeParse(req.body); - if (!validation.success) { return res.status(400).json({ error: validation.error.errors }); } @@ -25,25 +25,71 @@ async function createCustomer(req, res) { } try { + + const otp = crypto.randomInt(100000, 999999).toString(); + const otpExpiry = new Date(Date.now() + 5 * 60 * 1000); // 5 mins from now + const hashedPassword = await bcrypt.hash(req.body.password, 10); const customer = new Customer({ name: req.body.name, email: req.body.email, password: hashedPassword, + otp, + otpExpiry, + isVerified: false, }); await customer.save(); - res.status(201).json({ message: "Customer created successfully" }); + + await sendRegisterVerificationMail(req.body.email, otp); + + res.status(201).json({ message: "OTP sent to your email. Verify to complete registration." }); } catch (error) { + console.error("Error creating customer:", error); res.status(500).json({ error: "Internal server error" }); } } + +async function verifyOtp(req, res) { + const { email, otp } = req.body; + + try { + const customer = await Customer.findOne({ email }); + + + if (!customer || customer.isVerified) { + return res.status(400).json({ error: "Invalid request or already verified" }); + } + + + if (customer.otp !== otp) { + return res.status(400).json({ error: "Invalid OTP" }); + } + if (new Date() > customer.otpExpiry) { + return res.status(400).json({ error: "OTP expired. Please register again." }); + } + + + customer.isVerified = true; + customer.otp = undefined; + customer.otpExpiry = undefined; + await customer.save(); + + res.status(200).json({ message: "Registration successful!" }); + } catch (error) { + console.error("Error verifying OTP:", error); + res.status(500).json({ error: "Internal server error" }); + } +} + + async function loginCustomer(req, res) { const customerLoginSchema = z.object({ email: z.string().email("Invalid email address"), password: z.string().min(6, "Password must be at least 6 characters long"), }); - // Validate the request body + + const validation = customerLoginSchema.safeParse(req.body); if (!validation.success) { return res.status(400).json({ error: validation.error.errors }); @@ -54,24 +100,30 @@ async function loginCustomer(req, res) { if (!customer) { return res.status(401).json({ error: "Invalid email or password" }); } - const validPassword = await bcrypt.compare( - req.body.password, - customer.password - ); + + // Check if the customer is verified + if (!customer.isVerified) { + return res.status(403).json({ error: "Account not verified. Please verify your email." }); + } + + const validPassword = await bcrypt.compare(req.body.password, customer.password); if (!validPassword) { return res.status(401).json({ error: "Invalid email or password" }); } + const payload = { - sub: customer._id, // User ID + sub: customer._id, name: customer.name, // Optional role: "customer", // Optional email: customer.email, // Optional }; + const token = jwt.sign( payload, process.env.JWT_SECRET, { expiresIn: "1h" } // Expires in 1 hour ); + res.json({ message: "Login successful", token, @@ -83,10 +135,12 @@ async function loginCustomer(req, res) { }, }); } catch (error) { + console.error("Error during login:", error); res.status(500).json({ error: "Internal server error" }); } } + async function resetPassword(req, res) { const customerResetPasswordSchema = z.object({ email: z.string().email("Invalid email address"), @@ -116,4 +170,5 @@ module.exports = { createCustomer, loginCustomer, resetPassword, + verifyOtp }; diff --git a/backend/models/customer.model.js b/backend/models/customer.model.js index 1622fc0b..8b3dba44 100644 --- a/backend/models/customer.model.js +++ b/backend/models/customer.model.js @@ -1,5 +1,3 @@ -// models/Customer.js - const mongoose = require("mongoose"); const Schema = mongoose.Schema; @@ -22,6 +20,16 @@ const customerSchema = new Schema( type: String, default: "", }, + otp: { + type: String, + }, + otpExpiry: { + type: Date, + }, + isVerified: { + type: Boolean, + default: false, + }, role: { type: String, default: "customer", diff --git a/backend/routes/customerRouter.js b/backend/routes/customerRouter.js index 5a57513c..14802eef 100644 --- a/backend/routes/customerRouter.js +++ b/backend/routes/customerRouter.js @@ -3,6 +3,7 @@ const { loginCustomer, createCustomer, resetPassword, + verifyOtp, } = require("../controller/customer.controller"); const authenticateCustomer = require("../middlewares/authCustomer"); const passport = require("../config/passport.config"); @@ -27,6 +28,7 @@ router.get( ); router.post("/register", createCustomer); +router.post("/verify", verifyOtp); router.get( "/auth/google", passport.authenticate("google", { scope: ["email"] }) diff --git a/frontend/src/components/Pages/Signup.jsx b/frontend/src/components/Pages/Signup.jsx index b10ce1c5..342d4a26 100644 --- a/frontend/src/components/Pages/Signup.jsx +++ b/frontend/src/components/Pages/Signup.jsx @@ -25,6 +25,8 @@ const Signup = () => { const handleSubmit = async (e) => { e.preventDefault(); setIsLoading(true); + + // Input validation if (!data.email || !data.password || !data.name) { setError('Please fill in all fields'); setIsLoading(false); @@ -45,6 +47,7 @@ const Signup = () => { setIsLoading(false); return; } + try { const response = await fetch(`${API_URL}/api/user/register`, { method: 'POST', @@ -52,18 +55,25 @@ const Signup = () => { body: JSON.stringify(data), }); const result = await response.json(); + if (!response.ok) { setIsLoading(false); setError(result.error); return; } - alert('Registered successfully! Please log in.'); - navigate('/'); + + + alert('OTP sent to your email. Verify to complete registration.'); + navigate('/otp-verify'); + } catch (error) { setError(error.message); console.error('Error:', error); + } finally { + setIsLoading(false); // Ensure loading state is reset after request } }; + useEffect(() => { window.scrollTo(0, 0); diff --git a/frontend/src/components/Pages/VerifyOtp.tsx b/frontend/src/components/Pages/VerifyOtp.tsx index d633d00b..135bca55 100644 --- a/frontend/src/components/Pages/VerifyOtp.tsx +++ b/frontend/src/components/Pages/VerifyOtp.tsx @@ -79,6 +79,8 @@ const VerifyOtp = () => { onChange={(e) => handleChange(e)} /> + + + + + ); +}; + +export default OtpRegisterVerify; diff --git a/frontend/src/router/index.jsx b/frontend/src/router/index.jsx index a14a06db..83ca6a13 100644 --- a/frontend/src/router/index.jsx +++ b/frontend/src/router/index.jsx @@ -21,6 +21,7 @@ import Admin from '../components/Pages/Admin'; import VerifyOtp from '../components/Pages/VerifyOtp'; import EmailVerify from '../components/Pages/EmailVerify'; import Membership from '../components/Membership'; +import OtpRegisterVerify from '../components/Pages/verifyRegisterOtp'; const router = createBrowserRouter( createRoutesFromElements( }> @@ -38,6 +39,7 @@ const router = createBrowserRouter( } /> } /> } /> + } /> } /> From e4e10219555e9740eda45baab4e0e16d76cd15f9 Mon Sep 17 00:00:00 2001 From: "github-actions[bot]" <41898282+github-actions[bot]@users.noreply.github.com> Date: Sun, 27 Oct 2024 18:02:22 +0000 Subject: [PATCH 2/8] docs(contributor): contrib-readme-action has updated readme --- README.md | 120 +++++++++++++++++------------------------------------- 1 file changed, 38 insertions(+), 82 deletions(-) diff --git a/README.md b/README.md index cf249873..665d77d7 100644 --- a/README.md +++ b/README.md @@ -259,20 +259,6 @@ We extend our heartfelt gratitude to all the amazing contributors who have made Arindam - - - jainaryan04 -
- Aryan Ramesh Jain -
- - - - haseebzaki-07 -
- Haseeb Zaki -
- alo7lika @@ -280,8 +266,6 @@ We extend our heartfelt gratitude to all the amazing contributors who have made alolika bhowmik - - Ashwinib26 @@ -296,6 +280,8 @@ We extend our heartfelt gratitude to all the amazing contributors who have made Mahera Nayan + + tejasbenibagde @@ -310,13 +296,6 @@ We extend our heartfelt gratitude to all the amazing contributors who have made Tyarla Shirisha - - - VinayLodhi1712 -
- Vinay Anand Lodhi -
- Amnyadav @@ -324,8 +303,6 @@ We extend our heartfelt gratitude to all the amazing contributors who have made Aman Yadav - - NilanchalaPanda @@ -334,17 +311,10 @@ We extend our heartfelt gratitude to all the amazing contributors who have made - - Suhas-Koheda -
- Suhas Koheda -
- - - - Sumanbhadra + + haseebzaki-07
- Suman Bhadra + Haseeb Zaki
@@ -354,22 +324,22 @@ We extend our heartfelt gratitude to all the amazing contributors who have made Sawan kushwah + + - - PavanTeja2005 + + Suhas-Koheda
- PavanTeja2005 + Suhas Koheda
- - sajalbatra + + Jay-1409
- Sajal Batra + Jay shah
- - vishnuprasad2004 @@ -378,10 +348,17 @@ We extend our heartfelt gratitude to all the amazing contributors who have made - - Jay-1409 + + sajalbatra
- Jay shah + Sajal Batra +
+ + + + PavanTeja2005 +
+ PavanTeja2005
@@ -391,6 +368,8 @@ We extend our heartfelt gratitude to all the amazing contributors who have made Abhijit Motekar + + Navneetdadhich @@ -398,6 +377,13 @@ We extend our heartfelt gratitude to all the amazing contributors who have made Navneet Dadhich + + + VinayLodhi1712 +
+ Vinay Anand Lodhi +
+ lade6501 @@ -412,8 +398,6 @@ We extend our heartfelt gratitude to all the amazing contributors who have made MD REHAN - - T-Rahul-prabhu-38 @@ -428,13 +412,8 @@ We extend our heartfelt gratitude to all the amazing contributors who have made Aditya Bakshi - - - vaishnavipal1869 -
- vaishnavipal1869 -
- + + tanishirai @@ -456,8 +435,6 @@ We extend our heartfelt gratitude to all the amazing contributors who have made Sourabh Singh Rawat - - Shiva-Bajpai @@ -465,13 +442,6 @@ We extend our heartfelt gratitude to all the amazing contributors who have made Shiva Bajpai - - - Pushpa472 -
- Pushpa Vishwakarma -
- devxMani @@ -486,6 +456,8 @@ We extend our heartfelt gratitude to all the amazing contributors who have made Ayush Yadav + + smog-root @@ -500,8 +472,6 @@ We extend our heartfelt gratitude to all the amazing contributors who have made Vaibhav._Y - - Vaibhav-Kumar-K-R @@ -530,13 +500,8 @@ We extend our heartfelt gratitude to all the amazing contributors who have made Sapna Kul - - - Nikhil0-3 -
- Nikhil More -
- + + MutiatBash @@ -544,8 +509,6 @@ We extend our heartfelt gratitude to all the amazing contributors who have made Bashua Mutiat - - Mohitranag18 @@ -560,13 +523,6 @@ We extend our heartfelt gratitude to all the amazing contributors who have made Jai Dhingra - - - harjasae2001 -
- Harjas Singh -
- mishradev1 From 563b8a728cf0dc05cfc06a673676e57b83c6451e Mon Sep 17 00:00:00 2001 From: "github-actions[bot]" <41898282+github-actions[bot]@users.noreply.github.com> Date: Mon, 28 Oct 2024 05:45:08 +0000 Subject: [PATCH 3/8] docs(contributor): contrib-readme-action has updated readme --- README.md | 76 ++++++------------------------------------------------- 1 file changed, 8 insertions(+), 68 deletions(-) diff --git a/README.md b/README.md index 22677e42..665d77d7 100644 --- a/README.md +++ b/README.md @@ -260,18 +260,10 @@ We extend our heartfelt gratitude to all the amazing contributors who have made - - - jainaryan04 -
- Aryan Ramesh Jain -
- - - - haseebzaki-07 + + alo7lika
- Haseeb Zaki + alolika bhowmik
@@ -281,16 +273,15 @@ We extend our heartfelt gratitude to all the amazing contributors who have made Ashwini_ab - - - - - alo7lika + + itznayan
- alolika bhowmik + Mahera Nayan
+ + tejasbenibagde @@ -298,15 +289,6 @@ We extend our heartfelt gratitude to all the amazing contributors who have made Tejas Benibagde - - - itznayan -
- Mahera Nayan -
- - - Shirisha-16 @@ -315,24 +297,6 @@ We extend our heartfelt gratitude to all the amazing contributors who have made - - meghanakn22 -
- meghanakn22 -
- - - - - VinayLodhi1712 -
- Vinay Anand Lodhi -
- - - - - Amnyadav
@@ -369,8 +333,6 @@ We extend our heartfelt gratitude to all the amazing contributors who have made Suhas Koheda
- - Jay-1409 @@ -429,8 +391,6 @@ We extend our heartfelt gratitude to all the amazing contributors who have made Vishal Lade - - REHAN-18 @@ -468,8 +428,6 @@ We extend our heartfelt gratitude to all the amazing contributors who have made Sushree Manaswini Biswal - - Sourabh782 @@ -491,13 +449,6 @@ We extend our heartfelt gratitude to all the amazing contributors who have made MANI - - - meghanakn473 -
- K N Meghana -
- Ayush215mb @@ -551,15 +502,6 @@ We extend our heartfelt gratitude to all the amazing contributors who have made - - - - Nikhil0-3 -
- Nikhil More -
- - MutiatBash @@ -588,8 +530,6 @@ We extend our heartfelt gratitude to all the amazing contributors who have made Dev Mishra - - chikatlarakesh From c1c20c5cc079007530ce8cb9d32b3662bad83257 Mon Sep 17 00:00:00 2001 From: Anushka Chouhan <157525924+AnushkaChouhan25@users.noreply.github.com> Date: Mon, 28 Oct 2024 17:12:21 +0530 Subject: [PATCH 4/8] Update README.md --- README.md | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/README.md b/README.md index aa0dbe72..f8884746 100644 --- a/README.md +++ b/README.md @@ -618,6 +618,20 @@ We extend our heartfelt gratitude to all the amazing contributors who have made +--- +## Stargazers ❤️ + +
+ +[![Stargazers repo roster for RamakrushnaBiswal/PlayCafe](https://reporoster.com/stars/RamakrushnaBiswal/PlayCafe)](https://github.com/RamakrushnaBiswal/PlayCafe/stargazers) + +
+ +## Forkers ❤️ + +[![Forkers repo roster for RamakrushnaBiswal/PlayCafe](https://reporoster.com/forks/RamakrushnaBiswal/PlayCafe)](https://github.com/RamakrushnaBiswal/PlayCafe/network/members) + +--- ## ⭐ Support From 4f42779acd3318d05b663e93fe29937776a83077 Mon Sep 17 00:00:00 2001 From: Abhi-hertz <93651229+AE-Hertz@users.noreply.github.com> Date: Mon, 28 Oct 2024 17:36:12 +0530 Subject: [PATCH 5/8] fix: navbar algn --- frontend/src/components/Shared/Navbar.jsx | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/frontend/src/components/Shared/Navbar.jsx b/frontend/src/components/Shared/Navbar.jsx index 1c6e0a2a..b09f7b27 100644 --- a/frontend/src/components/Shared/Navbar.jsx +++ b/frontend/src/components/Shared/Navbar.jsx @@ -80,13 +80,13 @@ const Navbar = () => { }`} > -
+
logo Date: Mon, 28 Oct 2024 15:58:59 +0000 Subject: [PATCH 6/8] docs(contributor): contrib-readme-action has updated readme --- README.md | 128 +++++++++++++++++++++++++++++++++++++++--------------- 1 file changed, 94 insertions(+), 34 deletions(-) diff --git a/README.md b/README.md index 665d77d7..de6a54bb 100644 --- a/README.md +++ b/README.md @@ -259,6 +259,20 @@ We extend our heartfelt gratitude to all the amazing contributors who have made Arindam + + + jainaryan04 +
+ Aryan Ramesh Jain +
+ + + + haseebzaki-07 +
+ Haseeb Zaki +
+ alo7lika @@ -266,6 +280,8 @@ We extend our heartfelt gratitude to all the amazing contributors who have made alolika bhowmik + + Ashwinib26 @@ -280,8 +296,6 @@ We extend our heartfelt gratitude to all the amazing contributors who have made Mahera Nayan - - tejasbenibagde @@ -296,6 +310,22 @@ We extend our heartfelt gratitude to all the amazing contributors who have made Tyarla Shirisha + + + meghanakn22 +
+ meghanakn22 +
+ + + + VinayLodhi1712 +
+ Vinay Anand Lodhi +
+ + + Amnyadav @@ -311,10 +341,17 @@ We extend our heartfelt gratitude to all the amazing contributors who have made - - haseebzaki-07 + + Suhas-Koheda
- Haseeb Zaki + Suhas Koheda +
+ + + + Sumanbhadra +
+ Suman Bhadra
@@ -324,20 +361,20 @@ We extend our heartfelt gratitude to all the amazing contributors who have made Sawan kushwah - - - - Suhas-Koheda + + PavanTeja2005
- Suhas Koheda + PavanTeja2005
+ + - - Jay-1409 + + sajalbatra
- Jay shah + Sajal Batra
@@ -348,17 +385,10 @@ We extend our heartfelt gratitude to all the amazing contributors who have made - - sajalbatra -
- Sajal Batra -
- - - - PavanTeja2005 + + Jay-1409
- PavanTeja2005 + Jay shah
@@ -368,8 +398,6 @@ We extend our heartfelt gratitude to all the amazing contributors who have made Abhijit Motekar - - Navneetdadhich @@ -377,13 +405,6 @@ We extend our heartfelt gratitude to all the amazing contributors who have made Navneet Dadhich - - - VinayLodhi1712 -
- Vinay Anand Lodhi -
- lade6501 @@ -391,6 +412,8 @@ We extend our heartfelt gratitude to all the amazing contributors who have made Vishal Lade + + REHAN-18 @@ -412,8 +435,13 @@ We extend our heartfelt gratitude to all the amazing contributors who have made Aditya Bakshi - - + + + vaishnavipal1869 +
+ vaishnavipal1869 +
+ tanishirai @@ -428,6 +456,8 @@ We extend our heartfelt gratitude to all the amazing contributors who have made Sushree Manaswini Biswal + + Sourabh782 @@ -442,6 +472,13 @@ We extend our heartfelt gratitude to all the amazing contributors who have made Shiva Bajpai + + + Pushpa472 +
+ Pushpa Vishwakarma +
+ devxMani @@ -449,6 +486,13 @@ We extend our heartfelt gratitude to all the amazing contributors who have made MANI + + + meghanakn473 +
+ K N Meghana +
+ Ayush215mb @@ -502,6 +546,13 @@ We extend our heartfelt gratitude to all the amazing contributors who have made + + + Nikhil0-3 +
+ Nikhil More +
+ MutiatBash @@ -523,6 +574,13 @@ We extend our heartfelt gratitude to all the amazing contributors who have made Jai Dhingra + + + harjasae2001 +
+ Harjas Singh +
+ mishradev1 @@ -530,6 +588,8 @@ We extend our heartfelt gratitude to all the amazing contributors who have made Dev Mishra + + chikatlarakesh From 1a8d6cac5e108cef133bd62c48d63982f3c05557 Mon Sep 17 00:00:00 2001 From: "github-actions[bot]" <41898282+github-actions[bot]@users.noreply.github.com> Date: Mon, 28 Oct 2024 16:06:01 +0000 Subject: [PATCH 7/8] docs(contributor): contrib-readme-action has updated readme --- README.md | 31 +++++++++++++++++++------------ 1 file changed, 19 insertions(+), 12 deletions(-) diff --git a/README.md b/README.md index 3e140cee..e6218556 100644 --- a/README.md +++ b/README.md @@ -260,17 +260,17 @@ We extend our heartfelt gratitude to all the amazing contributors who have made - - jainaryan04 + + haseebzaki-07
- Aryan Ramesh Jain + Haseeb Zaki
- - haseebzaki-07 + + jainaryan04
- Haseeb Zaki + Aryan Ramesh Jain
@@ -290,17 +290,17 @@ We extend our heartfelt gratitude to all the amazing contributors who have made - - itznayan + + tejasbenibagde
- Mahera Nayan + Tejas Benibagde
- - tejasbenibagde + + itznayan
- Tejas Benibagde + Mahera Nayan
@@ -597,6 +597,13 @@ We extend our heartfelt gratitude to all the amazing contributors who have made CHIKATLA RAKESH + + + AnushkaChouhan25 +
+ Anushka Chouhan +
+ AliGates915 From 6d21841136a28e25ea6af79a38da286104bc9d23 Mon Sep 17 00:00:00 2001 From: "github-actions[bot]" <41898282+github-actions[bot]@users.noreply.github.com> Date: Mon, 28 Oct 2024 16:10:23 +0000 Subject: [PATCH 8/8] docs(contributor): contrib-readme-action has updated readme --- README.md | 31 ++++++++++++------------------- 1 file changed, 12 insertions(+), 19 deletions(-) diff --git a/README.md b/README.md index e6218556..3e140cee 100644 --- a/README.md +++ b/README.md @@ -260,17 +260,17 @@ We extend our heartfelt gratitude to all the amazing contributors who have made - - haseebzaki-07 + + jainaryan04
- Haseeb Zaki + Aryan Ramesh Jain
- - jainaryan04 + + haseebzaki-07
- Aryan Ramesh Jain + Haseeb Zaki
@@ -290,17 +290,17 @@ We extend our heartfelt gratitude to all the amazing contributors who have made - - tejasbenibagde + + itznayan
- Tejas Benibagde + Mahera Nayan
- - itznayan + + tejasbenibagde
- Mahera Nayan + Tejas Benibagde
@@ -597,13 +597,6 @@ We extend our heartfelt gratitude to all the amazing contributors who have made CHIKATLA RAKESH - - - AnushkaChouhan25 -
- Anushka Chouhan -
- AliGates915