generated from OPCODE-Open-Spring-Fest/template
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #6 from devraushan/rectify_filestructure
refactor: create separate folder for controllers for a neat filestruc…
- Loading branch information
Showing
5 changed files
with
222 additions
and
180 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
} |
Oops, something went wrong.