From 99e3455f862da076db9b9f4291a8fec863e5b23c Mon Sep 17 00:00:00 2001 From: OMKAR AGRAWAL Date: Thu, 15 Apr 2021 12:43:27 +0000 Subject: [PATCH 1/2] tem1 --- app.js | 2 +- bin/www | 1 - package-lock.json | 70 +- package.json | 2 + routes/index.js | 19 +- routes/student.js | 2 +- routes/users.js | 16 + dbconnection.js => tools/dbconnection.js | 0 tools/email-apsit/beefree-v9kikn5bte.html | 466 +++++++++++++ tools/email.html | 609 +++++++++++++++++ tools/sendEmails.js | 647 ++++++++++++++++++ dbtest.js => trials/dbtest.js | 25 +- .../emailVerifier.test.js | 0 ip.test.js => trials/ip.test.js | 0 procedure.test.js => trials/procedure.test.js | 2 +- 15 files changed, 1842 insertions(+), 19 deletions(-) rename dbconnection.js => tools/dbconnection.js (100%) create mode 100644 tools/email-apsit/beefree-v9kikn5bte.html create mode 100644 tools/email.html create mode 100644 tools/sendEmails.js rename dbtest.js => trials/dbtest.js (88%) rename emailVerifier.test.js => trials/emailVerifier.test.js (100%) rename ip.test.js => trials/ip.test.js (100%) rename procedure.test.js => trials/procedure.test.js (83%) diff --git a/app.js b/app.js index 0c26519..712273b 100644 --- a/app.js +++ b/app.js @@ -130,7 +130,7 @@ try { },async function (email, givenPassword, done) { debug("LINE 20"); debug(email, givenPassword); - return pool.query('Select id, password, name, uType, isActive, isVerified, verificationLink from user where email = ?;', [email]) + return pool.query('Select id, password, name, uType, isActive, isVerified, verificationCode from user where email = ?;', [email]) .then(results => { if (results[0].length > 0) { const { diff --git a/bin/www b/bin/www index 65e9607..59a440a 100755 --- a/bin/www +++ b/bin/www @@ -1,4 +1,3 @@ -#!/usr/bin/env node /** * Module dependencies. diff --git a/package-lock.json b/package-lock.json index 6f3a978..65d0025 100644 --- a/package-lock.json +++ b/package-lock.json @@ -65,6 +65,32 @@ } } }, + "@sendgrid/client": { + "version": "7.4.0", + "resolved": "https://registry.npmjs.org/@sendgrid/client/-/client-7.4.0.tgz", + "integrity": "sha512-KAZlEb1P8sATgBN+7hXgzaRF94nF9KQgDxQ6zUT1BV0kEsNtJQ2cs35sCtWt6AKKJrL0xPI/MsfcAJqom4YQBg==", + "requires": { + "@sendgrid/helpers": "^7.4.0", + "axios": "^0.19.2" + } + }, + "@sendgrid/helpers": { + "version": "7.4.0", + "resolved": "https://registry.npmjs.org/@sendgrid/helpers/-/helpers-7.4.0.tgz", + "integrity": "sha512-IQI2vemiJB0+X6bEp4HRG+0/wrzR2RDGnB5rwfq1CsPDrUFdJfxbE2zbGx//1GnlNwAtbHyc93ejU1m0KZr86w==", + "requires": { + "deepmerge": "^4.2.2" + } + }, + "@sendgrid/mail": { + "version": "7.4.0", + "resolved": "https://registry.npmjs.org/@sendgrid/mail/-/mail-7.4.0.tgz", + "integrity": "sha512-SAARsfbl50OEJ99LYGKfgrYiV5O6+23aeGJuEBTHHSwRZ6KhD3n1BjPeIejbqgbqYLZJfNLxyU3o5xRdJPp3zg==", + "requires": { + "@sendgrid/client": "^7.4.0", + "@sendgrid/helpers": "^7.4.0" + } + }, "@sindresorhus/is": { "version": "0.14.0", "resolved": "https://registry.npmjs.org/@sindresorhus/is/-/is-0.14.0.tgz", @@ -235,6 +261,14 @@ "resolved": "https://registry.npmjs.org/async/-/async-0.9.2.tgz", "integrity": "sha1-rqdNXmHB+JlhO/ZL2mbUx48v0X0=" }, + "axios": { + "version": "0.19.2", + "resolved": "https://registry.npmjs.org/axios/-/axios-0.19.2.tgz", + "integrity": "sha512-fjgm5MvRHLhx+osE2xoekY70AhARk3a6hkN+3Io1jc00jtquGvxYlKlsFUhmUET0V5te6CcZI7lcv2Ym61mjHA==", + "requires": { + "follow-redirects": "1.5.10" + } + }, "balanced-match": { "version": "1.0.0", "resolved": "https://registry.npmjs.org/balanced-match/-/balanced-match-1.0.0.tgz", @@ -790,6 +824,11 @@ "integrity": "sha1-s2nW+128E+7PUk+RsHD+7cNXzzQ=", "dev": true }, + "deepmerge": { + "version": "4.2.2", + "resolved": "https://registry.npmjs.org/deepmerge/-/deepmerge-4.2.2.tgz", + "integrity": "sha512-FJ3UgI4gIl+PHZm53knsuSFpE+nESMr7M4v9QcgB7S63Kj/6WqMiFQJpBBYz1Pt+66bZpP3Q7Lye0Oo9MPKEdg==" + }, "defer-to-connect": { "version": "1.1.3", "resolved": "https://registry.npmjs.org/defer-to-connect/-/defer-to-connect-1.1.3.tgz", @@ -1424,6 +1463,24 @@ "integrity": "sha512-tW+UkmtNg/jv9CSofAKvgVcO7c2URjhTdW1ZTkcAritblu8tajiYy7YisnIflEwtKssCtOxpnBRoCB7iap0/TA==", "dev": true }, + "follow-redirects": { + "version": "1.5.10", + "resolved": "https://registry.npmjs.org/follow-redirects/-/follow-redirects-1.5.10.tgz", + "integrity": "sha512-0V5l4Cizzvqt5D44aTXbFZz+FtyXV1vrDN6qrelxtfYQKW0KO0W2T/hkE8xvGa/540LkZlkaUjO4ailYTFtHVQ==", + "requires": { + "debug": "=3.1.0" + }, + "dependencies": { + "debug": { + "version": "3.1.0", + "resolved": "https://registry.npmjs.org/debug/-/debug-3.1.0.tgz", + "integrity": "sha512-OX8XqP7/1a9cqkxYw2yXss15f26NKWBpDXQd0/uK/KPqdQhxbPa994hnzjcE2VqQpDslf55723cKPUOGSmMY3g==", + "requires": { + "ms": "2.0.0" + } + } + } + }, "formidable": { "version": "1.2.2", "resolved": "https://registry.npmjs.org/formidable/-/formidable-1.2.2.tgz", @@ -2210,6 +2267,12 @@ "integrity": "sha512-sGkPx+VjMtmA6MX27oA4FBFELFCZZ4S4XqeGOXCv68tT+jb3vk/RyaKWP0PTKyWtmLSM0b+adUTEvbs1PEaH2w==", "dev": true }, + "nanoid": { + "version": "3.1.12", + "resolved": "https://registry.npmjs.org/nanoid/-/nanoid-3.1.12.tgz", + "integrity": "sha512-1qstj9z5+x491jfiC4Nelk+f8XBad7LN20PmyWINJEMRSf3wcAjAWysw1qaA8z6NSKe2sjq1hRSDpBH5paCb6A==", + "dev": true + }, "supports-color": { "version": "7.2.0", "resolved": "https://registry.npmjs.org/supports-color/-/supports-color-7.2.0.tgz", @@ -2313,10 +2376,9 @@ } }, "nanoid": { - "version": "3.1.12", - "resolved": "https://registry.npmjs.org/nanoid/-/nanoid-3.1.12.tgz", - "integrity": "sha512-1qstj9z5+x491jfiC4Nelk+f8XBad7LN20PmyWINJEMRSf3wcAjAWysw1qaA8z6NSKe2sjq1hRSDpBH5paCb6A==", - "dev": true + "version": "3.1.20", + "resolved": "https://registry.npmjs.org/nanoid/-/nanoid-3.1.20.tgz", + "integrity": "sha512-a1cQNyczgKbLX9jwbS/+d7W8fX/RfgYR7lVWwWOGIPNgK2m0MWvrGF6/m4kk6U3QcFMnZf3RIhL0v2Jgh/0Uxw==" }, "natural-compare": { "version": "1.4.0", diff --git a/package.json b/package.json index 9bdaae1..663ed05 100644 --- a/package.json +++ b/package.json @@ -12,6 +12,7 @@ "ip": "node ip.test.js" }, "dependencies": { + "@sendgrid/mail": "^7.4.0", "bcrypt": "^5.0.0", "body-parser": "^1.19.0", "compression": "^1.7.4", @@ -27,6 +28,7 @@ "helmet": "^4.2.0", "morgan": "^1.9.1", "mysql2": "^2.2.5", + "nanoid": "^3.1.20", "passport": "^0.4.1", "passport-local": "^1.0.0", "validator-pizza-node": "^1.0.3" diff --git a/routes/index.js b/routes/index.js index 5194fc2..e32e85c 100644 --- a/routes/index.js +++ b/routes/index.js @@ -1,5 +1,9 @@ const express = require('express'); const path = require('path'); +const { nanoid } = require('nanoid'); +const bcrypt = require('bcrypt'); +const { sendVerificationMail } = require('../tools/sendEmails'); + const router = express.Router(); const debug = require('debug')('backend:server:index.js'); const { @@ -256,12 +260,13 @@ router.post('/register', errors: results.array() }); } else { + const verificationID = nanoid(); // console.table([{ // ...req.body // }]); // debug("Received at /register"); - req.db.query("CALL Reg(?, ?, ?, ?, ?, ?, ?, ?, ?, ?);", [req.body.email, req.body.password, req.body.fullname, req.body.mobile, req.body.address, req.body.city, req.body.country, req.body.postcode, req.body.institute_name, req.body.photo]) + req.db.query("CALL Reg(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?);", [req.body.email, req.body.password, req.body.fullname, req.body.mobile, req.body.address, req.body.city, req.body.country, req.body.postcode, req.body.institute_name, req.body.photo, verificationID]) .then((...results) => { const data = results[0][0][0]; console.log(...results); @@ -273,8 +278,16 @@ router.post('/register', }]); // res.json(req.body); // res.redirect("/registrationSuccess") - debug("Response Sent successfully"); - res.send("Registration Successful"); + // debug("Response Sent successfully"); + // res.send("Registration Successful"); + + return sendVerificationMail(req.body.email, verificationID); + }) + .then(results => { + if(results[0].statusCode == 202) { + debug("sent code: " + verificationID); + res.send("

SUCCESSFULLY REGISTERED Please verify from your mail

"); + } else throw results; }) .catch((err) => { debug(err); diff --git a/routes/student.js b/routes/student.js index 597ffe5..c014ce7 100644 --- a/routes/student.js +++ b/routes/student.js @@ -35,7 +35,7 @@ router.get('/fetchQuiz/:id/:quizId', (req, res) => { }); // res.send({id, quizId}); -}) +}); router.use(express.static(path.join(__dirname, "..", "public", "student"))); module.exports = router; diff --git a/routes/users.js b/routes/users.js index 79dcad1..140a52a 100644 --- a/routes/users.js +++ b/routes/users.js @@ -1,6 +1,7 @@ const express = require('express'); const path = require('path'); const mysql = require('mysql2'); +const bcrypt = require('bcrypt'); const router = express.Router(); const debug = require('debug')('backend:server:index.js'); @@ -16,6 +17,21 @@ router.all('/test', function (_req, res) { debug("into /test"); res.send("TEST SUCCESS"); }); +router.get('/verify', (req, res) => { + const verificationID = req.query.code; + const email = req.query.refs; + req.db.query('update user set isVerified = TRUE, verificationCode = NULL where verificationCode = ?;', [verificationID]) + .then((results) => { + if(results[0].changedRows===1) { + res.send("

SUCCESSFULLY Verified

") + } else { + res.send("

INVALID Verification

"); + } + }) + .catch((err) => { + console.log(err); + }); +}) router.use("/assets", express.static(path.join(__dirname, "..", "public", "assets"))); router.use("/admin", express.static(path.join(__dirname, "..", "public", "admin"))) diff --git a/dbconnection.js b/tools/dbconnection.js similarity index 100% rename from dbconnection.js rename to tools/dbconnection.js diff --git a/tools/email-apsit/beefree-v9kikn5bte.html b/tools/email-apsit/beefree-v9kikn5bte.html new file mode 100644 index 0000000..bbc48b5 --- /dev/null +++ b/tools/email-apsit/beefree-v9kikn5bte.html @@ -0,0 +1,466 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/tools/email.html b/tools/email.html new file mode 100644 index 0000000..3c867d4 --- /dev/null +++ b/tools/email.html @@ -0,0 +1,609 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/tools/sendEmails.js b/tools/sendEmails.js new file mode 100644 index 0000000..7a7ede8 --- /dev/null +++ b/tools/sendEmails.js @@ -0,0 +1,647 @@ +const mail = require("@sendgrid/mail"); +if(! process.env.NODE_ENV || process.env.NODE_ENV !== "production") { + const dotenv = require("dotenv").config({path: "../.env"}); + } +mail.setApiKey(process.env.SENDGRID_API_KEY); + +// mail.send({ +// to: "omkar3654@gmail.com", +// from: "omkar@omkaragrawal.dev", +// subject: "TRIAL MAIL", +// html: "Hello there

If you are seeing this email then the test email is successful

", +// bcc: "contact@omkaragrawal.dev" +// }).then((...response) => { +// console.log("RESOLVED"); +// console.log(response); +// }) .catch((...reason) => { +// console.log("ERROR"); +// console.log(reason); +// }); +const sendVerificationEmail = (email, verificationCode) => { + const emailHTML = ` + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +`; + return mail.send({ + to: email, + from: "verify-noreply@apsit.edu.in", + subject: "TRIAL MAIL", + html: emailHTML + }); +}; + +// sendVerificationEmail("omkar3654@gmail.com") +// .then(results => console.log(results[0].statusCode)) +// .catch(err => console.log(err)); +module.exports = { + sendMail: mail.send, + sendMultiple: mail.sendMultiple, + sendVerificationMail: sendVerificationEmail, +}; \ No newline at end of file diff --git a/dbtest.js b/trials/dbtest.js similarity index 88% rename from dbtest.js rename to trials/dbtest.js index c1bb55c..0366b27 100644 --- a/dbtest.js +++ b/trials/dbtest.js @@ -2,7 +2,7 @@ var fs = require('fs'); var mysql = require('mysql2'); // // var myconfig = require('./myconfig.json'); if (!process.env.NODE_ENV || process.env.NODE_ENV !== "production") { - const dotenv = require("dotenv").config() + const dotenv = require("dotenv").config({path: '../.env'}); } // function db_test() { @@ -179,15 +179,24 @@ function db_listDatabases(con) { // con.end(); // }); - con.query('select q.id, q.quiz_id, q.quiz_time from quiz_list q join company c on q.company_id = c.id where q.company_id = ? and c.active = true and q.isActive = true;', [3]) + // con.query('select q.id, q.quiz_id, q.quiz_time from quiz_list q join company c on q.company_id = c.id where q.company_id = ? and c.active = true and q.isActive = true;', [3]) + // .then((results) => { + // // console.log(results[0]); + // if (results[0].length >= 1) { + // console.log(results[0]); + // } else if (results[0].length === 0) { + // console.log("No Quiz"); + // } else { + // console.log(results); + // } + // con.end(); + // }) + con.query('update user set isVerified = TRUE, verificationCode = NULL where verificationCode = ?;', ["123456789012345678901"]) .then((results) => { - // console.log(results[0]); - if (results[0].length >= 1) { - console.log(results[0]); - } else if (results[0].length === 0) { - console.log("No Quiz"); - } else { + if(results[0].changedRows===1) { console.log(results); + } else { + console.log("Not Affected"); } con.end(); }) diff --git a/emailVerifier.test.js b/trials/emailVerifier.test.js similarity index 100% rename from emailVerifier.test.js rename to trials/emailVerifier.test.js diff --git a/ip.test.js b/trials/ip.test.js similarity index 100% rename from ip.test.js rename to trials/ip.test.js diff --git a/procedure.test.js b/trials/procedure.test.js similarity index 83% rename from procedure.test.js rename to trials/procedure.test.js index b6b9f80..720ea77 100644 --- a/procedure.test.js +++ b/trials/procedure.test.js @@ -22,7 +22,7 @@ pool = pool.promise(); (async () => { try { // let id, email, temp, isVerified; - let reply = await pool.query("CALL Reg(?, ?, ?, ?, ?, ?, ?, ?, ?, ?);", ["omkar3654@gmail.com", "sjfbsdhfbsudhfbuhsdb", "omkaragrawal", 1234567890, "sfsdv", "Mumbai", "India", 401107, "sgdvsg", "hsdvghs"]); + let reply = await pool.query("CALL Reg(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?);", ["omkar3654@gmail.com", "sjfbsdhfbsudhfbuhsdb", "omkaragrawal", 1234567890, "sfsdv", "Mumbai", "India", 401107, "sgdvsg", "hsdvghs", "123456789012345678901"]); const data = reply[0][0][0]; console.table([{id: data.id, email: data.email, status: data["@status"], isVerified: data.isVerified}]); // SELECT id, email , @status, isVerified FROM user WHERE email = UEmail; From 48dd29143e44fe68fd2480054b5627cfbc4e3991 Mon Sep 17 00:00:00 2001 From: OMKAR AGRAWAL Date: Wed, 21 Apr 2021 19:18:15 +0000 Subject: [PATCH 2/2] changes --- routes/index.js | 40 +++++++++++++++++++++++----------------- 1 file changed, 23 insertions(+), 17 deletions(-) diff --git a/routes/index.js b/routes/index.js index e32e85c..7c61e66 100644 --- a/routes/index.js +++ b/routes/index.js @@ -1,8 +1,12 @@ const express = require('express'); const path = require('path'); -const { nanoid } = require('nanoid'); -const bcrypt = require('bcrypt'); -const { sendVerificationMail } = require('../tools/sendEmails'); +const { + nanoid +} = require('nanoid'); +// const bcrypt = require('bcrypt'); +const { + sendVerificationMail +} = require('../tools/sendEmails'); const router = express.Router(); const debug = require('debug')('backend:server:index.js'); @@ -58,7 +62,7 @@ router.get("/login", function (req, res, next) { }); router.post("/login", (req, res, next) => { - if(req.isAuthenticated()) { + if (req.isAuthenticated()) { debug("Is Authenticated"); res.redirect(301, "/users/student"); return; @@ -284,7 +288,7 @@ router.post('/register', return sendVerificationMail(req.body.email, verificationID); }) .then(results => { - if(results[0].statusCode == 202) { + if (results[0].statusCode == 202) { debug("sent code: " + verificationID); res.send("

SUCCESSFULLY REGISTERED Please verify from your mail

"); } else throw results; @@ -340,20 +344,22 @@ router.post("/uploadImage", (req, res) => { }) router.get('/companyQuizs', (req, res) => { - const { companyId } = req.query; + const { + companyId + } = req.query; // res.send(companyId); req.db.query('select q.id, q.quiz_id, q.quiz_time from quiz_list q join company c on q.company_id = c.id where q.company_id = ? and c.active = true and q.isActive = true;', [companyId]) - .then((results) => { - if (results[0].length >= 1) { - res.send(results[0]); - } else if (results[0].length === 0) { - res.send("No Quiz"); - } - }) - .catch((err) => { - console.log(err); - res.status(500).send(err); - }); + .then((results) => { + if (results[0].length >= 1) { + res.send(results[0]); + } else if (results[0].length === 0) { + res.send("No Quiz"); + } + }) + .catch((err) => { + console.log(err); + res.status(500).send(err); + }); }); router.use("/images", express.static(path.join(__dirname, "..", "custom-images")));