From 0ce4534156f8d50f0d13d6d0505ba7c4bc9ccf7d Mon Sep 17 00:00:00 2001 From: Luxshan2000 Date: Sat, 7 Oct 2023 13:02:18 +0530 Subject: [PATCH] otp send added --- backend/package-lock.json | 11 +++- backend/package.json | 3 +- backend/server.js | 3 ++ backend/src/controllers/authController.js | 21 +++++++- backend/src/utils/email.js | 66 +++++++++++++++++++++++ webapp/src/routes/AllRoutes.js | 2 +- webapp/src/routes/PrivateRoutes.js | 13 +++-- webapp/src/views/SignUp.js | 6 +-- 8 files changed, 115 insertions(+), 10 deletions(-) create mode 100644 backend/src/utils/email.js diff --git a/backend/package-lock.json b/backend/package-lock.json index ee39327..c0e7e80 100644 --- a/backend/package-lock.json +++ b/backend/package-lock.json @@ -17,7 +17,8 @@ "express": "^4.18.2", "google-auth-library": "^9.0.0", "jsonwebtoken": "^9.0.2", - "mongoose": "^7.5.0" + "mongoose": "^7.5.0", + "nodemailer": "^6.9.5" }, "devDependencies": { "nodemon": "^3.0.1" @@ -1497,6 +1498,14 @@ "webidl-conversions": "^3.0.0" } }, + "node_modules/nodemailer": { + "version": "6.9.5", + "resolved": "https://registry.npmjs.org/nodemailer/-/nodemailer-6.9.5.tgz", + "integrity": "sha512-/dmdWo62XjumuLc5+AYQZeiRj+PRR8y8qKtFCOyuOl1k/hckZd8durUUHs/ucKx6/8kN+wFxqKJlQ/LK/qR5FA==", + "engines": { + "node": ">=6.0.0" + } + }, "node_modules/nodemon": { "version": "3.0.1", "resolved": "https://registry.npmjs.org/nodemon/-/nodemon-3.0.1.tgz", diff --git a/backend/package.json b/backend/package.json index 872653b..2cd802a 100644 --- a/backend/package.json +++ b/backend/package.json @@ -18,7 +18,8 @@ "express": "^4.18.2", "google-auth-library": "^9.0.0", "jsonwebtoken": "^9.0.2", - "mongoose": "^7.5.0" + "mongoose": "^7.5.0", + "nodemailer": "^6.9.5" }, "devDependencies": { "nodemon": "^3.0.1" diff --git a/backend/server.js b/backend/server.js index 3bfc922..1f36be7 100644 --- a/backend/server.js +++ b/backend/server.js @@ -6,6 +6,7 @@ const cookieParser = require('cookie-parser') const connectMongoDb = require("./config/database") const authRoutes = require('./src/routes/authRoutes'); + const app = express() app.use(cookieParser()) const PORT = process.env.PORT || 5000 @@ -26,6 +27,8 @@ app.use('/api/auth', authRoutes); + + app.listen(5000, () => { console.log(`Server is running on port ${PORT}`); }); diff --git a/backend/src/controllers/authController.js b/backend/src/controllers/authController.js index 50c21a8..7005c2a 100644 --- a/backend/src/controllers/authController.js +++ b/backend/src/controllers/authController.js @@ -2,11 +2,14 @@ const User = require('../models/user') const bcrypt = require('bcrypt'); const jwt = require("jsonwebtoken"); const { GenerateRandomPassword } = require('../utils/string'); +const emailModule = require("../utils/email") + require('dotenv').config(); exports.signup = async (req, res) => { try { // Extract user information + const { email, password, name } = req.body; const saltRounds = 10; // Adjust const hashedPassword = await bcrypt.hash(password, saltRounds); @@ -14,13 +17,29 @@ exports.signup = async (req, res) => { const user = new User({ email, hashedPassword, name }); await user.save(); + const otp = 2327 + console.log("Start"); + + await emailModule.sendOTP(email, otp) + + + // Send the response + + const token = jwt.sign({ name: user.name, isVerified: user.isVerified }, process.env.SECURITY_KEY, { expiresIn: '7day' }); + + + const oneWeekInSeconds = 7 * 24 * 60 * 60; // 7 days * 24 hours * 60 minutes * 60 seconds + const expirationDate = new Date(Date.now() + oneWeekInSeconds * 1000); // Convert seconds to milliseconds + res.cookie('token', token, { + expires: expirationDate, + + }); res.json({ message: 'Signup successful' }); } catch (error) { - console.error(error); res.status(500).json({ error: 'Signup failed' }); } }; diff --git a/backend/src/utils/email.js b/backend/src/utils/email.js new file mode 100644 index 0000000..2c535a3 --- /dev/null +++ b/backend/src/utils/email.js @@ -0,0 +1,66 @@ +const nodemailer = require('nodemailer'); + +const transporter = nodemailer.createTransport({ + service: 'gmail', // e.g., 'gmail' + auth: { + user: 'luxluxshan2000@gmail.com', // Your email address + pass: 'covl uxof lefn keih' // Your email password + }, + }); + + + + + + +async function sendOTP(to, otp) { + const html_email =` + + + + + + + OTP Verification Email + + + +
+

DriveSmart

+

OTP Verification

+ +

Your OTP is: ${otp}

+ +

Please click the following link to complete the OTP verification process:

+ Verify OTP + +

Don't reply to this email.

+
+ + +` + + try{ + + const mailOptions = { + from: 'luxluxshan2000@gmail.com', + to: to, + subject: 'OTP Verification', + html: html_email + }; + + + return await transporter.sendMail(mailOptions) + }catch(err){ + console.log("Error while sending the email!") + } + + + + + +} + +module.exports = { + sendOTP, +}; \ No newline at end of file diff --git a/webapp/src/routes/AllRoutes.js b/webapp/src/routes/AllRoutes.js index f07f2eb..f027db8 100644 --- a/webapp/src/routes/AllRoutes.js +++ b/webapp/src/routes/AllRoutes.js @@ -30,9 +30,9 @@ function AllRoutes() { }/> }/> }/> + }/> } > - }/> }/> }/> } /> diff --git a/webapp/src/routes/PrivateRoutes.js b/webapp/src/routes/PrivateRoutes.js index 546d5cf..4495a8e 100644 --- a/webapp/src/routes/PrivateRoutes.js +++ b/webapp/src/routes/PrivateRoutes.js @@ -3,19 +3,26 @@ import { Outlet, Navigate} from "react-router-dom"; import React from 'react' import { useAuthContext } from "../context/AuthContext"; import { getSessionCookie } from "../utils/cookie"; - +import jwt_decode from 'jwt-decode'; function PrivateRoutes() { let allow = false + let isVerified = false allow = getSessionCookie("token") - + + if(allow){ + isVerified = jwt_decode(allow).isVerified + } + + + return ( - allow ? : + allow ? ( isVerified ? : ) : ) } diff --git a/webapp/src/views/SignUp.js b/webapp/src/views/SignUp.js index 538d9ba..70caa6d 100644 --- a/webapp/src/views/SignUp.js +++ b/webapp/src/views/SignUp.js @@ -21,13 +21,13 @@ export default function SignUp() { if(password != conpassword){ return } - - axios.post('http://18.61.20.118:5000/api/auth/signup', {email:email, password:password, name:userName}) + //18.61.20.118 + axios.post('http://localhost:5000/api/auth/signup', {email:email, password:password, name:userName}) .then(response => { // Handle the successful response here console.log('Registration successful!', response.data); - // navigate("/passwordverify", { replace: true }); + navigate("/passwordverify", { replace: true });