This commit is contained in:
D Shrat 2021-11-18 12:41:15 +07:00
commit fa800b19c7
2 changed files with 374 additions and 303 deletions

View file

@ -1,196 +1,246 @@
const express = require('express'); const express = require("express");
const bcrypt = require("bcrypt"); const bcrypt = require("bcrypt");
const randtoken = require("rand-token"); const randtoken = require("rand-token");
const router = express.Router(); const router = express.Router();
const db = require('../modules/db'); const db = require("../modules/db");
const mail = require('../modules/mail'); const mail = require("../modules/mail");
const saltRounds = 10; const saltRounds = 10;
let config; let config;
try { try {
config = require('../config'); config = require("../config");
} catch (e) { } catch (e) {
console.log('No config file found'); console.log("No config file found");
process.exit(0); process.exit(0);
} }
router.get('/getproducts', (req, res) => { router.get("/getproducts", (req, res) => {
const conn = db.connect(); const conn = db.connect();
conn.execute('SELECT * FROM `products`', [], function (err, results) { conn.execute("SELECT * FROM `products`", [], function (err, results) {
res.json(results); res.json(results);
}); });
}) });
router.get('/getproduct', (req, res) => { router.get("/getproduct", (req, res) => {
const conn = db.connect(); const conn = db.connect();
conn.execute('SELECT * FROM `products` WHERE `ID` = ?', [req.query.id], function (err, results) { conn.execute(
"SELECT * FROM `products` WHERE `ID` = ?",
[req.query.id],
function (err, results) {
res.json(results); res.json(results);
}
);
}); });
})
router.post('/register', async (req, res) => { router.post("/register", async (req, res) => {
if (!req.body.email || !req.body.password || !req.body.first_name || !req.body.last_name) { if (
!req.body.email ||
!req.body.password ||
!req.body.first_name ||
!req.body.last_name
) {
res.status(400); res.status(400);
res.json({ res.json({
'message': 'Bad Request' message: "Bad Request",
})
} else {
const conn = db.connect();
conn.query("SELECT * FROM users WHERE email = ?", [req.body.email], async function (error, response, fields) {
if (error) {
res.status(400);
res.json({
'message': 'Bad Request'
})
} else {
if (response.length > 0) {
res.status(400);
res.json({
'message': 'Bad Request'
})
} else {
const encryptedPassword = await bcrypt.hash(req.body.password, saltRounds);
var users = {
first_name: req.body.first_name,
last_name: req.body.last_name,
email: req.body.email,
password: encryptedPassword,
session: '[]',
};
conn.query("INSERT INTO users SET ?", users, function (error, response, fields) {
if (error) {
res.status(400);
res.json({
'message': 'Bad Request'
})
} else {
conn.query('SELECT * FROM users WHERE email ="' + req.body.email + '"', function (err, result) {
if (err) {
res.status(400);
res.json({
'message': 'Bad Request'
})
}
if (result.length > 0) {
var token = randtoken.generate(20);
if (result[0].verify == 0) {
let subject = "Account Verification - Nekoya";
let content = `<p>Hello!!! Please click this link <a href="${config.host}/verify-mail?token=${token}">link</a> to verify your account!!! Thanks!!!</p>`;
var sent = mail.send(req.body.email, subject, content);
if (sent != "0") {
var data = {
token: token,
};
conn.query('UPDATE users SET ? WHERE email ="' + req.body.email + '"', data, function (err, result) {
if (err) {
res.status(400);
res.json({
'message': 'Bad Request'
})
} else {
res.status(200);
res.json({
'message': 'Register Verification Sent ~'
})
}
}); });
} else {
res.status(400);
res.json({
'message': 'Bad Request'
})
}
}
} else {
res.status(400);
res.json({
'message': 'Bad Request'
})
}
});
}
});
}
}
});
}
})
router.post('/login', async (req, res) => {
if (!req.body.email || !req.body.password) {
res.status(400);
res.json({
'message': 'Bad Request'
})
} else { } else {
const conn = db.connect(); const conn = db.connect();
conn.query( conn.query(
"SELECT * FROM users WHERE email = ?", "SELECT * FROM users WHERE email = ?",
[req.body.email], [req.body.email],
async function (error, response, fields) { async function (error, response, fields) {
const passCheck = await bcrypt.compare(req.body.password, response[0].password);
if (error) { if (error) {
res.status(400); res.status(400);
res.json({ res.json({
'message': 'Bad Request' message: "Bad Request",
}) });
} else {
if (response.length > 0) {
res.status(400);
res.json({
message: "Bad Request",
});
} else {
const encryptedPassword = await bcrypt.hash(
req.body.password,
saltRounds
);
var users = {
first_name: req.body.first_name,
last_name: req.body.last_name,
email: req.body.email,
password: encryptedPassword,
session: "[]",
};
conn.query(
"INSERT INTO users SET ?",
users,
function (error, response, fields) {
if (error) {
res.status(400);
res.json({
message: "Bad Request",
});
} else {
conn.query(
'SELECT * FROM users WHERE email ="' + req.body.email + '"',
function (err, result) {
if (err) {
res.status(400);
res.json({
message: "Bad Request",
});
}
if (result.length > 0) {
var token = randtoken.generate(20);
if (result[0].verify == 0) {
let subject = "Account Verification - Nekoya";
let content = `<p>Hello!!! Please click this link <a href="${config.host}/verify-mail?token=${token}">link</a> to verify your account!!! Thanks!!!</p>`;
var sent = mail.send(
req.body.email,
subject,
content
);
if (sent != "0") {
var data = {
token: token,
};
conn.query(
'UPDATE users SET ? WHERE email ="' +
req.body.email +
'"',
data,
function (err, result) {
if (err) {
res.status(400);
res.json({
message: "Bad Request",
});
} else {
res.status(200);
res.json({
message: "Register Verification Sent ~",
});
}
}
);
} else {
res.status(400);
res.json({
message: "Bad Request",
});
}
}
} else {
res.status(400);
res.json({
message: "Bad Request",
});
}
}
);
}
}
);
}
}
}
);
}
});
router.post("/login", async (req, res) => {
if (!req.body.email || !req.body.password) {
res.status(400);
res.json({
message: "Bad Request",
});
} else {
const conn = db.connect();
conn.query(
"SELECT * FROM users WHERE email = ?",
[req.body.email],
async function (error, response, fields) {
if (!response[0]) {
res.status(205);
res.json({
message: "Sorry Your email is not registered in our system",
});
} else {
const passCheck = await bcrypt.compare(
req.body.password,
response[0].password
);
if (error) {
res.status(400);
res.json({
message: "Bad Request",
});
} else { } else {
if (response.length > 0) { if (response.length > 0) {
if (passCheck) { if (passCheck) {
if (response[0].verify == 0) { if (response[0].verify == 0) {
res.status(204); res.status(204);
res.json({ res.json({
'message': 'Sorry You havent verified your email' message: "Sorry You havent verified your email",
}) });
} else { } else {
let token = randtoken.generate(256); let token = randtoken.generate(256);
let session = JSON.parse(response[0].session); let session = JSON.parse(response[0].session);
session.push({ session.push({
user_agent: req.body.ua || req.headers['user-agent'], user_agent: req.body.ua || req.headers["user-agent"],
ip: req.body.ip || req.headers['x-forwarded-for'] || req.socket.remoteAddress, ip:
req.body.ip ||
req.headers["x-forwarded-for"] ||
req.socket.remoteAddress,
session: token, session: token,
}) });
conn.query('UPDATE users SET ? WHERE email ="' + req.body.email + '"', { conn.query(
session: JSON.stringify(session) 'UPDATE users SET ? WHERE email ="' + req.body.email + '"',
}, function (err, result) { {
session: JSON.stringify(session),
},
function (err, result) {
if (err) { if (err) {
res.status(400); res.status(400);
res.json({ res.json({
'message': 'Bad Request' message: "Bad Request",
}) });
} else { } else {
res.status(200); res.status(200);
res.json({ res.json({
'id': response[0].id, id: response[0].id,
'first_name': response[0].first_name, first_name: response[0].first_name,
'last_name': response[0].last_name, last_name: response[0].last_name,
'email': response[0].email, email: response[0].email,
'verify': (response[0].verify == 1) ? true : false, verify: response[0].verify == 1 ? true : false,
'session_token': token, session_token: token,
})
}
}); });
} }
}
);
}
} else { } else {
res.status(401); res.status(401);
res.json({ res.json({
'message': 'Unauthorized' message: "Unauthorized",
}) });
} }
} else { } else {
res.status(400); res.status(400);
res.json({ res.json({
'message': 'Bad Request' message: "Bad Request",
}) });
}
} }
} }
} }
); );
} }
}) });
router.get('/verify-mail', async (req, res) => { router.get("/verify-mail", async (req, res) => {
const conn = db.connect(); const conn = db.connect();
conn.query( conn.query(
'SELECT * FROM users WHERE token ="' + req.query.token + '"', 'SELECT * FROM users WHERE token ="' + req.query.token + '"',
@ -198,8 +248,8 @@ router.get('/verify-mail', async (req, res) => {
if (err) { if (err) {
res.status(400); res.status(400);
res.json({ res.json({
'message': 'Bad Request' message: "Bad Request",
}) });
} }
if (result.length > 0) { if (result.length > 0) {
if (result[0].verify == 0) { if (result[0].verify == 0) {
@ -213,30 +263,30 @@ router.get('/verify-mail', async (req, res) => {
if (err) { if (err) {
res.status(400); res.status(400);
res.json({ res.json({
'message': 'Bad Request' message: "Bad Request",
}) });
} else { } else {
res.status(200); res.status(200);
res.json({ res.json({
'message': 'Verified ~' message: "Verified ~",
}) });
} }
} }
); );
} else { } else {
res.status(403); res.status(403);
res.json({ res.json({
'message': 'Forbidden' message: "Forbidden",
}) });
} }
} else { } else {
res.status(400); res.status(400);
res.json({ res.json({
'message': 'Bad Request' message: "Bad Request",
}) });
} }
} }
); );
}) });
module.exports = router; module.exports = router;

View file

@ -1,104 +1,125 @@
const express = require('express') const express = require("express");
const router = express.Router() const router = express.Router();
let controller = require("../controllers/controllers"); let controller = require("../controllers/controllers");
router.get('/', (_req, res) => { router.get("/", (_req, res) => {
res.render('pages/index'); res.render("pages/index");
}) });
router.route('/register') router
.route("/register")
.get((_req, res) => { .get((_req, res) => {
res.render('pages/register'); res.render("pages/register");
}) })
.post((req, res) => { .post((req, res) => {
controller.register(req.body.email, req.body.password, req.body.first_name, req.body.last_name).then(data => { controller
.register(
req.body.email,
req.body.password,
req.body.first_name,
req.body.last_name
)
.then((data) => {
if (data == 200) { if (data == 200) {
res.render("pages/register-verification-sent"); res.render("pages/register-verification-sent");
} else { } else {
res.render("pages/register-error"); res.render("pages/register-error");
} }
}); });
}) });
router.route('/login') router
.route("/login")
.get((_req, res) => { .get((_req, res) => {
res.render('pages/login'); res.render("pages/login");
}) })
.post((req, res) => { .post((req, res) => {
controller.login(req.body.email, req.body.password, req.headers['user-agent'], req.headers['x-forwarded-for'] || req.socket.remoteAddress).then(data => { controller
.login(
req.body.email,
req.body.password,
req.headers["user-agent"],
req.headers["x-forwarded-for"] || req.socket.remoteAddress
)
.then((data) => {
if (data[0] == 200) { if (data[0] == 200) {
res.render("pages/index"); res.render("pages/index");
} else if (data[0] == 204) { } else if (data[0] == 204) {
res.render('pages/login', { res.render("pages/login", {
companyName : 'SALAH', companyName: "SALAH",
error : 'Please confirm your email' error: "Please confirm your email",
});
} else if (data[0] == 205) {
res.render("pages/login", {
companyName: "SALAH",
error: "Sorry Your email is not registered in our system",
}); });
} else { } else {
res.render('pages/login', { res.render("pages/login", {
companyName : 'SALAH', companyName: "SALAH",
error : 'Invalid Email or Password' error: "Invalid Email or Password",
}); });
} }
}); });
}) });
router.get('/verify-mail', (req, res) => { router.get("/verify-mail", (req, res) => {
controller.verify_mail(req.params.token).then(data => { controller.verify_mail(req.params.token).then((data) => {
if (data == 200) { if (data == 200) {
res.render("pages/register-verification-success"); res.render("pages/register-verification-success");
} else { } else {
res.render("pages/index"); res.render("pages/index");
} }
}) });
}) });
router.get('/forgot-password', (_req, res) => { router.get("/forgot-password", (_req, res) => {
res.render('pages/forgot-password'); res.render("pages/forgot-password");
}) });
router.get('/otp', (_req, res) => { router.get("/otp", (_req, res) => {
res.render('pages/otp'); res.render("pages/otp");
}) });
router.get('/change-password', (_req, res) => { router.get("/change-password", (_req, res) => {
res.render('pages/change-password'); res.render("pages/change-password");
}) });
router.get('/products', (_req, res) => { router.get("/products", (_req, res) => {
controller.getProducts().then(data => { controller.getProducts().then((data) => {
res.render('pages/products', {data: data}); res.render("pages/products", { data: data });
}) });
}) });
router.get('/product/:id', (req, res) => { router.get("/product/:id", (req, res) => {
controller.getProduct(req.params.id).then(data => { controller.getProduct(req.params.id).then((data) => {
res.render('pages/product', {data: data}); res.render("pages/product", { data: data });
}) });
}) });
router.get(('/successadd'), (_req, res) => { router.get("/successadd", (_req, res) => {
res.render('pages/success-add') res.render("pages/success-add");
}) });
router.get('/bag', (_req, res) => { router.get("/bag", (_req, res) => {
res.render('pages/bag') res.render("pages/bag");
}) });
router.get('/checkout', (_req, res) => { router.get("/checkout", (_req, res) => {
res.render('pages/checkout') res.render("pages/checkout");
}) });
router.get('/notifpay', (_req, res) => { router.get("/notifpay", (_req, res) => {
res.render('pages/notif-pay') res.render("pages/notif-pay");
}) });
router.get('/payment', (_req, res) => { router.get("/payment", (_req, res) => {
res.render('pages/payment') res.render("pages/payment");
}) });
router.get('/about-us', (_req, res) => { router.get("/about-us", (_req, res) => {
res.render('pages/about-us') res.render("pages/about-us");
}) });
module.exports = router; module.exports = router;