Skip to content

Commit

Permalink
Fixed Email Verification
Browse files Browse the repository at this point in the history
  • Loading branch information
swetankkk committed Oct 29, 2023
1 parent c6dec2e commit 1c3be4e
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 17 deletions.
21 changes: 14 additions & 7 deletions src/controllers/auth.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down Expand Up @@ -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,
Expand All @@ -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,
Expand Down
8 changes: 5 additions & 3 deletions src/services/auth.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
4 changes: 2 additions & 2 deletions src/services/email.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = `<p>Click <a href="${config.appUrl}/verify-email?token=${token}">here</a> to verify your email</p>`;
const template = `Click <a href="${config.appUrl}/verify-email?token=${token}">here</a> to verify your email`;
await sendEmail(to, subject, template);
};

Expand Down
11 changes: 8 additions & 3 deletions src/services/token.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down Expand Up @@ -63,16 +64,20 @@ 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,
user: payload.sub,
blacklisted: false,
});
if (!tokenDoc) {
throw new Error('token not found');
res.status(httpStatus.BAD_REQUEST).send({
success: false,
message: 'BAD Request',
});
}
return tokenDoc;
};
Expand Down
1 change: 0 additions & 1 deletion src/services/user.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down
22 changes: 21 additions & 1 deletion src/utils/ApiError.js
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -12,3 +31,4 @@ class ApiError extends Error {
}
module.exports = ApiError;
*/

0 comments on commit 1c3be4e

Please sign in to comment.