diff --git a/app/controllers/loginController.js b/app/controllers/loginController.js new file mode 100644 index 0000000..0080f78 --- /dev/null +++ b/app/controllers/loginController.js @@ -0,0 +1,105 @@ +const nodemailer = require('nodemailer'); +const path = require('path') +const db = require(path.join(__dirname,"../db/dbConnect")); +require('dotenv').config(); + +let transporter = nodemailer.createTransport({ + service: 'Gmail', + auth: { + user: process.env.email, + pass: process.env.pass + } +}); + +const login = (req,res)=>{ + if (req.session.isLoggedIn) { + res.redirect("/") + return; + } + db.query('SELECT * FROM users WHERE email = \''+req.body.email+'\' AND password = \''+req.body.password+'\'', (error, results, fields) => { + if (error) { + console.error('Error executing query: ' + error); + res.send(error); + return; + } + if (results.length > 0) { + req.session.isLoggedIn = true; + res.redirect('/'); + } + else{ + res.redirect('/register'); + } + }); +} + + + +const forgotPass = (req,res)=>{ + if (req.session.isLoggedIn){ + res.redirect("/"); + return; + } + db.query('SELECT * FROM users WHERE email = \''+req.body.email+'\';', (error, results, fields) => { + if (error) { + console.error('Error executing query: ' + error); + res.send(error); + return; + } + if (results.length > 0){ + email = results[0].email; + otp = randomNumber = Math.floor(Math.random() * 9000) + 1000; + let mailOptions = { + from: process.env.email, + to: email, + subject: 'OTP for Password Reset', + text: 'Your OTP for password change is '+ otp + }; + transporter.sendMail(mailOptions, (error, info) => { + if (error) { + console.error('Error occurred:', error); + } else { + console.log('Email sent:', info.response); + } + }); + db.query('UPDATE users SET otp='+otp+' WHERE email=\''+email+'\';', (error, results, fields) => { + if (error) { + console.error('Error executing query: ' + error); + res.send(error); + return; + } + res.render('changePass',{email:email}) + }); + } + else{ + res.redirect('/register'); + } + }); +} + +const changePass = (req,res)=>{ + if (req.session.isLoggedIn){ + res.redirect("/"); + return; + } + otp = req.body.otp; + console.log(otp); + db.query('UPDATE users set password = \'' +req.body.newPassword+'\' WHERE email = \''+req.body.email+'\' AND otp = '+otp+';', (error, results, fields) => { + if (error) { + console.error('Error executing query: ' + error); + res.send(error); + return; + } + res.redirect('/login'); + }); +} + +const logout = (req,res)=>{ + if (req.session.isLoggedIn){ + req.session.isLoggedIn = false; + } + res.redirect("/login"); +} + +module.exports = { + login,logout,forgotPass,changePass +} \ No newline at end of file diff --git a/app/controllers/productController.js b/app/controllers/productController.js new file mode 100644 index 0000000..c1f545d --- /dev/null +++ b/app/controllers/productController.js @@ -0,0 +1,21 @@ +const path = require('path') +const db = require(path.join(__dirname,"../db/dbConnect")); + +const searchProducts = (req,res)=>{ + if (!req.session.isLoggedIn) { + res.redirect('/login'); + return; + } + db.query('SELECT * FROM products WHERE name LIKE \'\%'+req.body.name+'\%\';', (error, results, fields) => { + if (error) { + console.error('Error executing query: ' + error); + res.send(error); + return; + } + console.log(results) + res.render('products',{products:results, isLoggedIn:req.session.isLoggedIn}); + }); +} +module.exports = { + searchProducts +} \ No newline at end of file diff --git a/app/controllers/signupController.js b/app/controllers/signupController.js new file mode 100644 index 0000000..2bc9f9c --- /dev/null +++ b/app/controllers/signupController.js @@ -0,0 +1,30 @@ +const path = require('path') +const db = require(path.join(__dirname,"../db/dbConnect")); +require('dotenv').config(); + +const signUp = (req,res)=>{ + if (req.session.isLoggedIn){ + res.redirect("/"); + return; + } + db.query("SELECT * FROM users WHERE email = '"+req.body.email+"';", (error, results, fields) => { + if (error) { + console.error('Error executing query: ' + error); + res.send(error); + return; + } + if (results.length > 0) { + res.redirect('/login'); + } + else{ + db.query("INSERT INTO users (name,email,password) VALUES ('"+req.body.fullname+"','"+req.body.email+"','"+req.body.password+"');", (error, results, fields) => { + if (error) { + console.error('Error executing query: ' + error); + return; + } + res.redirect('/login'); + }); + } + }); +} +module.exports = {signUp} \ No newline at end of file diff --git a/app/controllers/viewControllers.js b/app/controllers/viewControllers.js new file mode 100644 index 0000000..ddd458e --- /dev/null +++ b/app/controllers/viewControllers.js @@ -0,0 +1,46 @@ +const path = require('path') +const db = require(path.join(__dirname,"../db/dbConnect")); +require('dotenv').config(); + +const loginView = (req,res)=>{ + if (req.session.isLoggedIn) { + res.redirect("/") + return; + } + res.render('login'); +} + +const signUpView = (req,res)=>{ + if (req.session.isLoggedIn){ + res.redirect("/"); + return; + } + res.render('register'); +} + +const forgotPassView = (req,res)=>{ + if (req.session.isLoggedIn){ + res.redirect("/"); + return; + } + res.render('forgotPass') +} + +const productListView = (req,res)=>{ + if (!req.session.isLoggedIn) { + res.redirect('/login'); + return; + } + db.query('SELECT * FROM products', (error, results, fields) => { + if (error) { + console.error('Error executing query: ' + error); + res.send(error); + return; + } + res.render('products',{products:results, isLoggedIn:req.session.isLoggedIn}); + }); +} + +module.exports = { + loginView,signUpView,forgotPassView,productListView +} \ No newline at end of file diff --git a/app/routes/route.js b/app/routes/route.js index 0f56bd1..33f5b06 100644 --- a/app/routes/route.js +++ b/app/routes/route.js @@ -1,196 +1,36 @@ const express = require('express') const router = express.Router() -const nodemailer = require('nodemailer'); const bodyParser = require('body-parser'); +const session = require('express-session'); +const path = require('path'); + router.use(bodyParser.urlencoded({ extended: true })); router.use(bodyParser.json()); -const path = require('path') -router.use(express.static( path.join(__dirname,"../public"))) -const db = require(path.join(__dirname,"../db/dbConnect")); -const session = require('express-session'); +router.use(express.static(path.join(__dirname, "../public"))); + +const loginController = require(path.join(__dirname,"../controllers/loginController")); +const signupController = require(path.join(__dirname,"../controllers/signupController")); +const viewController = require(path.join(__dirname,"../controllers/viewControllers")); +const productController = require(path.join(__dirname,"../controllers/productController")); require('dotenv').config(); + router.use(session({ secret: process.env.secret, resave: false, saveUninitialized: false })); -let transporter = nodemailer.createTransport({ - service: 'Gmail', - auth: { - user: process.env.email, - pass: process.env.pass - } -}); - - -router.get('/', (req,res) => { - if (!req.session.isLoggedIn) { - res.redirect('/login'); - return; - } - db.query('SELECT * FROM products', (error, results, fields) => { - if (error) { - console.error('Error executing query: ' + error); - res.send(error); - return; - } - res.render('products',{products:results, isLoggedIn:req.session.isLoggedIn}); - }); -}) - -router.post('/search', (req, res) => { - if (!req.session.isLoggedIn) { - res.redirect('/login'); - return; - } - db.query('SELECT * FROM products WHERE name LIKE \'\%'+req.body.name+'\%\';', (error, results, fields) => { - if (error) { - console.error('Error executing query: ' + error); - res.send(error); - return; - } - console.log(results) - res.render('products',{products:results, isLoggedIn:req.session.isLoggedIn}); - }); -}) - - -router.get('/login', (req, res) =>{ - if (req.session.isLoggedIn) { - res.redirect("/") - return; - } - res.render('login'); -}) - -router.post('/login', (req, res) => { - if (req.session.isLoggedIn) { - res.redirect("/") - return; - } - db.query('SELECT * FROM users WHERE email = \''+req.body.email+'\' AND password = \''+req.body.password+'\'', (error, results, fields) => { - if (error) { - console.error('Error executing query: ' + error); - res.send(error); - return; - } - if (results.length > 0) { - req.session.isLoggedIn = true; - res.redirect('/'); - } - else{ - res.redirect('/register'); - } - }); -}); - -router.get('/register', (req,res) => { - if (req.session.isLoggedIn){ - res.redirect("/"); - return; - } - res.render('register'); -}) - -router.post('/register', (req, res) => { - if (req.session.isLoggedIn){ - res.redirect("/"); - return; - } - db.query("SELECT * FROM users WHERE email = '"+req.body.email+"';", (error, results, fields) => { - if (error) { - console.error('Error executing query: ' + error); - res.send(error); - return; - } - if (results.length > 0) { - res.redirect('/login'); - } - else{ - db.query("INSERT INTO users (name,email,password) VALUES ('"+req.body.fullname+"','"+req.body.email+"','"+req.body.password+"');", (error, results, fields) => { - if (error) { - console.error('Error executing query: ' + error); - return; - } - res.redirect('/login'); - }); - } - }); -}); - -router.get('/forgotPass', (req, res) => { - if (req.session.isLoggedIn){ - res.redirect("/"); - return; - } - res.render('forgotPass') -}) -router.post('/forgotPass', (req,res) => { - if (req.session.isLoggedIn){ - res.redirect("/"); - return; - } - db.query('SELECT * FROM users WHERE email = \''+req.body.email+'\';', (error, results, fields) => { - if (error) { - console.error('Error executing query: ' + error); - res.send(error); - return; - } - if (results.length > 0){ - email = results[0].email; - otp = randomNumber = Math.floor(Math.random() * 9000) + 1000; - let mailOptions = { - from: process.env.email, - to: email, - subject: 'OTP for Password Reset', - text: 'Your OTP for password change is '+ otp - }; - transporter.sendMail(mailOptions, (error, info) => { - if (error) { - console.error('Error occurred:', error); - } else { - console.log('Email sent:', info.response); - } - }); - db.query('UPDATE users SET otp='+otp+' WHERE email=\''+email+'\';', (error, results, fields) => { - if (error) { - console.error('Error executing query: ' + error); - res.send(error); - return; - } - res.render('changePass',{email:email}) - }); - } - else{ - res.redirect('/register'); - } - }); -}) -router.post('/changePass', (req, res) => { - if (req.session.isLoggedIn){ - res.redirect("/"); - return; - } - otp = req.body.otp; - console.log(otp); - //console.log('UPDATE users set password = \'' +req.body.newPassword+'\' WHERE otp = '+otp+' AND email = \''+req.body.email+'\';') - db.query('UPDATE users set password = \'' +req.body.newPassword+'\' WHERE email = \''+req.body.email+'\' AND otp = '+otp+';', (error, results, fields) => { - if (error) { - console.error('Error executing query: ' + error); - res.send(error); - return; - } - res.redirect('/login'); - }); -}) -router.post('/logout', (req, res) => { - if (req.session.isLoggedIn){ - req.session.isLoggedIn = false; - } - res.redirect("/login"); -}) +router.get('/', viewController.productListView) +router.post('/search', productController.searchProducts) +router.get('/login', viewController.loginView) +router.post('/login', loginController.login) +router.get('/register', viewController.signUpView) +router.post('/register', signupController.signUp); +router.get('/forgotPass', viewController.forgotPassView) +router.post('/forgotPass', loginController.forgotPass) +router.post('/changePass', loginController.changePass) +router.post('/logout', loginController.logout) module.exports = router; \ No newline at end of file