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 =` + + +
+ + + +Your OTP is: ${otp}
+ +Please click the following link to complete the OTP verification process:
+ Verify OTP + +Don't reply to this email.
+