From 1c3be4e8984a4d1818485d95196e700bb22fff08 Mon Sep 17 00:00:00 2001 From: Swetank Date: Sun, 29 Oct 2023 20:02:44 +0530 Subject: [PATCH] Fixed Email Verification --- src/controllers/auth.controller.js | 21 ++++++++++++++------- src/services/auth.service.js | 8 +++++--- src/services/email.service.js | 4 ++-- src/services/token.service.js | 11 ++++++++--- src/services/user.service.js | 1 - src/utils/ApiError.js | 22 +++++++++++++++++++++- 6 files changed, 50 insertions(+), 17 deletions(-) diff --git a/src/controllers/auth.controller.js b/src/controllers/auth.controller.js index 14d4f29..af27fc9 100644 --- a/src/controllers/auth.controller.js +++ b/src/controllers/auth.controller.js @@ -19,7 +19,6 @@ const register = catchAsync(async (req, res) => { .send(); } const tokens = await tokenService.generateAuthTokens(user); - //await emailService.sendVerificationEmail(user, tokens.verificationToken); res.status(httpStatus.CREATED).send({ success: true, message: 'Registration Successful', @@ -73,6 +72,7 @@ const sendVerificationEmail = catchAsync(async (req, res) => { const verifyEmailToken = await tokenService.generateVerifyEmailToken( req.user ); + console.log('verifyEmailToken :', verifyEmailToken); await emailService.sendVerificationEmail(req.user.email, verifyEmailToken); res.status(httpStatus.ACCEPTED).send({ success: true, @@ -82,16 +82,23 @@ const sendVerificationEmail = catchAsync(async (req, res) => { }); const verifyEmail = catchAsync(async (req, res) => { - await authService.verifyEmail(req.query.token); - res.status(httpStatus.OK).send({ - success: true, - message: 'Email Verification Successful', - }); + try { + const response = await authService.verifyEmail(req, res); + + res.status(httpStatus.OK).send({ + success: true, + message: 'Email Verification Successful', + }); + } catch (error) { + res.status(httpStatus.BAD_REQUEST).send({ + success: false, + message: 'BAD Request', + }); + } }); const getSwots = catchAsync(async (req, res) => { const swots = await userService.getSwots(req, res); - console.log('Swots : ', swots); if (swots) { res.status(httpStatus.OK).send({ success: true, diff --git a/src/services/auth.service.js b/src/services/auth.service.js index 6e36fff..bf322e3 100644 --- a/src/services/auth.service.js +++ b/src/services/auth.service.js @@ -61,12 +61,14 @@ const resetPassword = async (resetPasswordToken, newPassword) => { } }; -const verifyEmail = async (verifyEmailToken) => { +const verifyEmail = async (req, res) => { try { const verifyEmailTokenDoc = await tokenService.verifyToken( - verifyEmailToken, - tokenTypes.VERIFY_EMAIL + req, + tokenTypes.VERIFY_EMAIL, + res ); + const user = await userService.getUserById(verifyEmailTokenDoc.user); if (!user) { throw new Error(); diff --git a/src/services/email.service.js b/src/services/email.service.js index 3f558ee..5181ff3 100644 --- a/src/services/email.service.js +++ b/src/services/email.service.js @@ -12,13 +12,13 @@ if (config.env === 'test') { } const sendEmail = async (to, subject, text) => { - const msg = { from: config.email.from, to, subject, text }; + const msg = { from: config.email.from, to, subject, html: text }; await transport.sendMail(msg); }; const sendVerificationEmail = async (to, token) => { const subject = 'Verify your email'; - const template = `

Click here to verify your email

`; + const template = `Click here to verify your email`; await sendEmail(to, subject, template); }; diff --git a/src/services/token.service.js b/src/services/token.service.js index ac8eba7..53a2921 100644 --- a/src/services/token.service.js +++ b/src/services/token.service.js @@ -4,6 +4,7 @@ const config = require('../config/config'); const { tokenTypes } = require('../config/tokens'); const { Token } = require('../models'); const logger = require('../config/logger'); +const httpStatus = require('http-status'); const generateToken = (userId, expires, type, secret = config.jwt.secret) => { const payload = { @@ -63,8 +64,9 @@ const saveToken = async (token, userId, expires, type, blacklisted = false) => { return tokenDoc; }; -const verifyToken = async (token, type) => { - const payload = await jwt.verify(token, config.jwt.secret); +const verifyToken = async (req, type, res) => { + const payload = await jwt.verify(req.query.token, config.jwt.secret); + const token = req.query.token; const tokenDoc = await Token.findOne({ token, type, @@ -72,7 +74,10 @@ const verifyToken = async (token, type) => { blacklisted: false, }); if (!tokenDoc) { - throw new Error('token not found'); + res.status(httpStatus.BAD_REQUEST).send({ + success: false, + message: 'BAD Request', + }); } return tokenDoc; }; diff --git a/src/services/user.service.js b/src/services/user.service.js index 2013ede..424507a 100644 --- a/src/services/user.service.js +++ b/src/services/user.service.js @@ -57,7 +57,6 @@ const getSwot = async (req, res) => { //console.log('Req.user :', req.user); const swots = await User.findById(req.user._id); return swots.swot; - //console.log('Swots : ', swots.swot); }; const modifySwot = async (req, res) => { diff --git a/src/utils/ApiError.js b/src/utils/ApiError.js index ea4ad6d..91c1a24 100644 --- a/src/utils/ApiError.js +++ b/src/utils/ApiError.js @@ -1,8 +1,27 @@ -class ApiError extends Error { +function ApiError(statusCode, message, isOperational = true, stack = '') { + this.statusCode = statusCode; + this.message = message; + this.isOperational = isOperational; + this.success = false; + if (stack) { + this.stack = stack; + } else { + Error.captureStackTrace(this, this.constructor); + } + return this; +} + +ApiError.prototype = Object.create(Error.prototype); +ApiError.prototype.constructor = ApiError; + +module.exports = ApiError; + +/*class ApiError extends Error { constructor(statusCode, message, isOperational = true, stack = '') { super(message); this.statusCode = statusCode; this.isOperational = isOperational; + this.success = false; if (stack) { this.stack = stack; } else { @@ -12,3 +31,4 @@ class ApiError extends Error { } module.exports = ApiError; +*/