Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: create separate folder for controllers for a neat filestruc… #6

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading