Browse Source

user verification module complete along with sendgrid email integration

master
kj1352 4 years ago
parent
commit
186302a777
5 changed files with 65 additions and 7 deletions
  1. BIN
      .gitignore
  2. BIN
      sendgrid.env
  3. +0
    -1
      src/authentication/auth.ts
  4. +62
    -5
      src/authentication/routes.ts
  5. +3
    -1
      src/index.ts

BIN
.gitignore View File


BIN
sendgrid.env View File


+ 0
- 1
src/authentication/auth.ts View File

@@ -1,4 +1,3 @@
import bcrypt from 'bcrypt';
import passport from 'passport'; import passport from 'passport';
import passportJWT, { StrategyOptions } from 'passport-jwt'; import passportJWT, { StrategyOptions } from 'passport-jwt';
import jwt from 'jsonwebtoken'; import jwt from 'jsonwebtoken';


+ 62
- 5
src/authentication/routes.ts View File

@@ -2,8 +2,7 @@ import express, { response } from 'express';
import { DB_NAME, getDatabaseClient } from '../db-utils'; import { DB_NAME, getDatabaseClient } from '../db-utils';
import bcrypt from 'bcrypt'; import bcrypt from 'bcrypt';
import { generateJWT, SALT_ROUNDS } from './auth'; 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(); 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; const email: string = request.body.email;


if (!email) { if (!email) {
@@ -99,19 +98,77 @@ authRoutes.post('/verify-user/', async (request, response) => {


const otpMail: MailDataRequired = { const otpMail: MailDataRequired = {
to: email, to: email,
from: 'kj@webtrigon.in',
from: 'nikhilkj24@gmail.com',
subject: 'Webtrigon Job-portal Sign Up Verification', subject: 'Webtrigon Job-portal Sign Up Verification',
text: `Please use the OTP ${otp} to verify your Email ID`, 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=> { sendGridMail.send(otpMail).then(data=> {
console.log(data); console.log(data);
response.send('Verification OTP sent'); response.send('Verification OTP sent');
return;
}, (err) => { }, (err) => {
console.log(err); console.log(err);
response.sendStatus(500); response.sendStatus(500);
response.send("SMTP system failure"); 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; return;
}); });


+ 3
- 1
src/index.ts View File

@@ -5,7 +5,9 @@ import authRoutes from './authentication/routes';
import { connectToDatabaseServer } from './db-utils'; import { connectToDatabaseServer } from './db-utils';
import sendGridMail from '@sendgrid/mail'; 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(); const app = express();
app.use(cors()); app.use(cors());


Loading…
Cancel
Save