Skip to content

Commit

Permalink
otp send added
Browse files Browse the repository at this point in the history
  • Loading branch information
Luxshan2000 committed Oct 7, 2023
1 parent 8c466d9 commit 0ce4534
Show file tree
Hide file tree
Showing 8 changed files with 115 additions and 10 deletions.
11 changes: 10 additions & 1 deletion backend/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
3 changes: 3 additions & 0 deletions backend/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -26,6 +27,8 @@ app.use('/api/auth', authRoutes);





app.listen(5000, () => {
console.log(`Server is running on port ${PORT}`);
});
21 changes: 20 additions & 1 deletion backend/src/controllers/authController.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,44 @@ 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);
// Create a new user
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' });
}
};
Expand Down
66 changes: 66 additions & 0 deletions backend/src/utils/email.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
const nodemailer = require('nodemailer');

const transporter = nodemailer.createTransport({
service: 'gmail', // e.g., 'gmail'
auth: {
user: '[email protected]', // Your email address
pass: 'covl uxof lefn keih' // Your email password
},
});






async function sendOTP(to, otp) {
const html_email =`
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>OTP Verification Email</title>
</head>
<body style="font-family: Arial, sans-serif; background-color: #f5f5f5; padding: 20px;">
<div style="background-color: #ffffff; padding: 20px; border-radius: 10px; box-shadow: 0px 0px 10px rgba(0,0,0,0.2);">
<h1 style="font-size: 24px; color: #333; text-align:center; text-decoration: underline;">DriveSmart</h1>
<h2 style="font-size: 18px; color: #555; text-align:center">OTP Verification</h2>
<p style="font-size: 20px;">Your OTP is: <strong style="font-size: 20px; color: #007bff;">${otp}</strong></p>
<p>Please click the following link to complete the OTP verification process:</p>
<a href="http://localhost:3000/passwordverify" style="display: inline-block; padding: 10px 20px; background-color: #007bff; color: #fff; text-decoration: none; border-radius: 5px; font-size: 16px;">Verify OTP</a>
<p style="font-size: 14px; color: #777;">Don't reply to this email.</p>
</div>
</body>
</html>`

try{

const mailOptions = {
from: '[email protected]',
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,
};
2 changes: 1 addition & 1 deletion webapp/src/routes/AllRoutes.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@ function AllRoutes() {
<Route path='/resetpassword' element={<ResetPassword/>}/>
<Route path='/passwordupdated' element={<PasswordUpdated/>}/>
<Route path='/terms' element={<TermsAndConditionsView/>}/>
<Route path='/passwordverify' element={<PasswordVerify/>}/>

<Route element={<PrivateRoutes/>} >
<Route path='/passwordverify' element={<PasswordVerify/>}/>
<Route path='/dashboard' element={<DashboardView/>}/>
<Route path='/dashboard/course' element={<CourseView />}/>
<Route path='/dashboard/premium' element={<PremiumView/>} />
Expand Down
13 changes: 10 additions & 3 deletions webapp/src/routes/PrivateRoutes.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 ? <Outlet/> : <Navigate to='/' replace={true}/>
allow ? ( isVerified ? <Outlet/> : <Navigate to='/passwordverify' replace={true}/> ) : <Navigate to='/login' replace={true}/>
)
}

Expand Down
6 changes: 3 additions & 3 deletions webapp/src/views/SignUp.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 });



Expand Down

0 comments on commit 0ce4534

Please sign in to comment.