diff --git a/models/user.ts b/models/user.ts index 55eb41f..b292be2 100644 --- a/models/user.ts +++ b/models/user.ts @@ -8,6 +8,7 @@ export default interface IUser { email: string, password: string, isVerified: boolean, + otp: number, userType: 'ADMIN' | 'MODERATOR' | 'APPLICANT', skillSet?: Array, progress?: Array<{ diff --git a/package-lock.json b/package-lock.json index c43f4e4..4d782cc 100644 --- a/package-lock.json +++ b/package-lock.json @@ -64,6 +64,32 @@ } } }, + "@sendgrid/client": { + "version": "7.4.7", + "resolved": "https://registry.npmjs.org/@sendgrid/client/-/client-7.4.7.tgz", + "integrity": "sha512-Zh3H/nCyI2+MaAZW9KJpyk6JXfxE27W+mR636Hju/GX95SR7+uymeuLLJ3GtLvtapLTl8l6/0YoHjzlg65xtqg==", + "requires": { + "@sendgrid/helpers": "^7.4.6", + "axios": "^0.21.4" + } + }, + "@sendgrid/helpers": { + "version": "7.4.6", + "resolved": "https://registry.npmjs.org/@sendgrid/helpers/-/helpers-7.4.6.tgz", + "integrity": "sha512-Vvt4d60fkU/DPSwMyxXtlnbw4/B+5Y9eeYnygTxhmw8TNzUhdPphr7SaRSperWJ8P1VeQZzobvQNyMj5E7A3UA==", + "requires": { + "deepmerge": "^4.2.2" + } + }, + "@sendgrid/mail": { + "version": "7.4.7", + "resolved": "https://registry.npmjs.org/@sendgrid/mail/-/mail-7.4.7.tgz", + "integrity": "sha512-lGfXJBEx7PMQje/NsVsebF6MdP2ptHWjmuI4YANjReAQlIGq3Cqm4JLP5Fb4n5Bbr1LXLCM7R0gJo+/PT6ENKw==", + "requires": { + "@sendgrid/client": "^7.4.7", + "@sendgrid/helpers": "^7.4.6" + } + }, "@sindresorhus/is": { "version": "0.14.0", "resolved": "https://registry.npmjs.org/@sindresorhus/is/-/is-0.14.0.tgz", @@ -682,6 +708,11 @@ "resolved": "https://registry.npmjs.org/deep-extend/-/deep-extend-0.6.0.tgz", "integrity": "sha512-LOHxIOaPYdHlJRtCQfDIVZtfw/ufM8+rVj649RIHzcm/vGwQRXFt6OPqIFWsm2XEMrNIEtWR64sY1LEKD2vAOA==" }, + "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", diff --git a/package.json b/package.json index da2583e..0b8e549 100644 --- a/package.json +++ b/package.json @@ -14,6 +14,7 @@ "author": "", "license": "ISC", "dependencies": { + "@sendgrid/mail": "^7.4.7", "axios": "^0.21.4", "bcrypt": "^5.0.1", "body-parser": "^1.19.0", diff --git a/src/authentication/routes.ts b/src/authentication/routes.ts index 6162ac0..64c659b 100644 --- a/src/authentication/routes.ts +++ b/src/authentication/routes.ts @@ -1,7 +1,9 @@ -import express from 'express'; +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'; const authRoutes = express.Router(); @@ -11,11 +13,11 @@ authRoutes.get('/users/', async (request, response) => { }); authRoutes.post('/register-applicant/', async (request, response) => { - const name = request.body.name; - const email = request.body.email; - const password = request.body.password; - const userType = 'APPLICANT'; - const isVerified = false; + const name: string = request.body.name; + const email: string = request.body.email; + const password: string = request.body.password; + const userType: string = 'APPLICANT'; + const isVerified: boolean = false; const userCollection = getDatabaseClient().db(DB_NAME).collection('users'); @@ -66,10 +68,58 @@ authRoutes.post('/register-applicant/', async (request, response) => { }); + +authRoutes.post('/verify-user/', async (request, response) => { + const email: string = request.body.email; + + if (!email) { + response.status(400); + response.send("Missing Email ID"); + } + + 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; + } + + const otp = 1000 + Math.ceil(Math.random() * 8999); + + const otpMail: MailDataRequired = { + to: email, + from: 'kj@webtrigon.in', + subject: 'Webtrigon Job-portal Sign Up Verification', + text: `Please use the OTP ${otp} to verify your Email ID`, + }; + + sendGridMail.send(otpMail).then(data=> { + console.log(data); + response.send('Verification OTP sent'); + }, (err) => { + console.log(err); + response.sendStatus(500); + response.send("SMTP system failure"); + }); + + return; +}); + authRoutes.post('/api-auth/', async (request, response) => { - const email = request.body.email; - const password = request.body.password; - const userType = request.body.userType; + const email: string = request.body.email; + const password: string = request.body.password; + const userType: string = request.body.userType; if (!email || !password || !userType) { response.status(400); @@ -117,12 +167,6 @@ authRoutes.post('/api-auth/', async (request, response) => { } return; - - // let passwordComparisonResult: boolean; - - // try { - // passwordComparisonResult = await bcrypt.compare(password, user.password); - // } }); export default authRoutes; \ No newline at end of file diff --git a/src/index.ts b/src/index.ts index 80edfc7..005a692 100644 --- a/src/index.ts +++ b/src/index.ts @@ -3,6 +3,9 @@ import cors from 'cors'; import passport from 'passport'; import authRoutes from './authentication/routes'; import { connectToDatabaseServer } from './db-utils'; +import sendGridMail from '@sendgrid/mail'; + +const SENDGRID_API_KEY = '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'; const app = express(); app.use(cors()); @@ -17,6 +20,6 @@ app.get('/', (request, response) => { const server = app.listen(app.get('port'), () => { connectToDatabaseServer(); - // sendGridMail.setApiKey(SENDGRID_API_KEY); + sendGridMail.setApiKey(SENDGRID_API_KEY); console.log('App is running on http://localhost:%d', app.get('port')); }); \ No newline at end of file