Skip to content

Commit

Permalink
Merge pull request #6 from devraushan/rectify_filestructure
Browse files Browse the repository at this point in the history
refactor: create separate folder for controllers for a neat filestruc…
  • Loading branch information
AdityaKumar5155 authored Mar 23, 2024
2 parents 512c61c + 50f31a3 commit 85a01b7
Show file tree
Hide file tree
Showing 5 changed files with 222 additions and 180 deletions.
105 changes: 105 additions & 0 deletions app/controllers/loginController.js
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
}
21 changes: 21 additions & 0 deletions app/controllers/productController.js
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
}
30 changes: 30 additions & 0 deletions app/controllers/signupController.js
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}
46 changes: 46 additions & 0 deletions app/controllers/viewControllers.js
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
}
Loading

0 comments on commit 85a01b7

Please sign in to comment.