From 186302a7776154b61a284cb0adeb301ec920cbca Mon Sep 17 00:00:00 2001 From: kj1352 Date: Wed, 29 Sep 2021 17:41:03 +0530 Subject: [PATCH] user verification module complete along with sendgrid email integration --- .gitignore | Bin 21 -> 50 bytes sendgrid.env | Bin 0 -> 196 bytes src/authentication/auth.ts | 1 - src/authentication/routes.ts | 67 ++++++++++++++++++++++++++++++++--- src/index.ts | 4 ++- 5 files changed, 65 insertions(+), 7 deletions(-) create mode 100644 sendgrid.env diff --git a/.gitignore b/.gitignore index 8f9d79913c322b5af65056255e6fc7c30bb1abdc..2c9fcff3f6654dfcd21ef5bc1f5efd7e76458420 100644 GIT binary patch literal 50 zcmd1HPf3l>%}*)KNiEjb;^j)oEH2U4DrQJ!$YV%hNM|Tw$OO`QV15|`F9R0?0D@Qy AS^xk5 literal 21 ccmd1HPf3l>%}*)KNiEjb;!4RZF45Nl09cI&ZvX%Q diff --git a/sendgrid.env b/sendgrid.env new file mode 100644 index 0000000000000000000000000000000000000000..0f30c7cdb779f8d2c7be04c7392a2ae961f9a537 GIT binary patch literal 196 zcmW-bJr4n45QLvv;y>tgqCuq~+~GuU=d-!ONn8jbKL0!(ig|Z-cV_4Pyth0#bK=64 z5;0vybO@Q!x2Cih%TgjYE1Hzmgj7VLXULrmGv7pns$$vF8ab_z*-um-oOR@xX&H0& rEU5DMXXgYu+hRhGfr(dge-v^w@wW?Tn11uhg|(OKyx8e3Q>4Hb$sHlR literal 0 HcmV?d00001 diff --git a/src/authentication/auth.ts b/src/authentication/auth.ts index bae3a30..14c4ade 100644 --- a/src/authentication/auth.ts +++ b/src/authentication/auth.ts @@ -1,4 +1,3 @@ -import bcrypt from 'bcrypt'; import passport from 'passport'; import passportJWT, { StrategyOptions } from 'passport-jwt'; import jwt from 'jsonwebtoken'; diff --git a/src/authentication/routes.ts b/src/authentication/routes.ts index 64c659b..21a2203 100644 --- a/src/authentication/routes.ts +++ b/src/authentication/routes.ts @@ -2,8 +2,7 @@ import express, { response } from 'express'; import { DB_NAME, getDatabaseClient } from '../db-utils'; import bcrypt from 'bcrypt'; import { generateJWT, SALT_ROUNDS } from './auth'; -import sendGridMail from '@sendgrid/mail'; -import { MailDataRequired } from '@sendgrid/helpers/classes/mail'; +import sendGridMail, { MailDataRequired } from '@sendgrid/mail'; const authRoutes = express.Router(); @@ -69,7 +68,7 @@ authRoutes.post('/register-applicant/', async (request, response) => { }); -authRoutes.post('/verify-user/', async (request, response) => { +authRoutes.post('/request-verification/', async (request, response) => { const email: string = request.body.email; if (!email) { @@ -99,19 +98,77 @@ authRoutes.post('/verify-user/', async (request, response) => { const otpMail: MailDataRequired = { to: email, - from: 'kj@webtrigon.in', + from: 'nikhilkj24@gmail.com', subject: 'Webtrigon Job-portal Sign Up Verification', text: `Please use the OTP ${otp} to verify your Email ID`, }; + await userCollection.updateOne({ + _id: matchedUser._id, + }, { + $set: { + otp: otp, + } + }); + sendGridMail.send(otpMail).then(data=> { console.log(data); response.send('Verification OTP sent'); + return; }, (err) => { console.log(err); response.sendStatus(500); response.send("SMTP system failure"); - }); + return; + }); + + return; +}); + + +authRoutes.post('/verify-user/', async (request, response) => { + const email: string = request.body.email; + const otp: number = Number(request.body.otp); + + if (!email || !otp) { + response.status(400); + response.send("Please fill in the required fields"); + } + + const userCollection = getDatabaseClient().db(DB_NAME).collection('users'); + + let users = await userCollection.find({ + email, + }).toArray(); + + const matchedUser = users[0]; + + if (!matchedUser) { + response.status(400); + response.send('No user under this EmailID'); + return; + } else if (matchedUser.isVerified) { + response.status(400); + response.send('The user is already verified'); + return; + } + + if (matchedUser.otp === otp) { + response.status(200); + response.send("User Verified"); + } else { + response.status(400); + response.send('Verification failed, please try again.'); + return; + } + + await userCollection.updateOne({ + email: matchedUser.email, + }, { + $set: { + isVerified: true, + } + }); return; }); diff --git a/src/index.ts b/src/index.ts index 005a692..0d06b92 100644 --- a/src/index.ts +++ b/src/index.ts @@ -5,7 +5,9 @@ import authRoutes from './authentication/routes'; import { connectToDatabaseServer } from './db-utils'; import sendGridMail from '@sendgrid/mail'; -const SENDGRID_API_KEY = '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'; +const SENDGRID_API_KEY = process.env.SENDGRID_API_KEY || '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'; + +sendGridMail.setApiKey(SENDGRID_API_KEY); const app = express(); app.use(cors());