Ver a proveniência

user verification module complete along with sendgrid email integration

master
kj1352 há 4 anos
ascendente
cometimento
186302a777
5 ficheiros alterados com 65 adições e 7 eliminações
  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 Ver ficheiro


BIN
sendgrid.env Ver ficheiro


+ 0
- 1
src/authentication/auth.ts Ver ficheiro

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


+ 62
- 5
src/authentication/routes.ts Ver ficheiro

@@ -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;
});


+ 3
- 1
src/index.ts Ver ficheiro

@@ -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());


Carregando…
Cancelar
Guardar