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