Skip to content

Commit

Permalink
feature(user should be able to reset password):user should be able to…
Browse files Browse the repository at this point in the history
… reset password

user should be able to reset password
[finishes #166841011]
  • Loading branch information
Cavdy committed Jul 10, 2019
1 parent 28ae1af commit 12d432d
Show file tree
Hide file tree
Showing 15 changed files with 1,098 additions and 2 deletions.
80 changes: 80 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 5 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,14 @@
"author": "Andela Simulations Programme",
"license": "MIT",
"dependencies": {
"@babel/polyfill": "^7.4.3",
"bcryptjs": "^2.4.3",
"body-parser": "^1.18.3",
"@babel/cli": "^7.4.3",
"@babel/core": "^7.4.3",
"@babel/node": "^7.2.2",
"@babel/polyfill": "^7.4.3",
"@babel/preset-env": "^7.4.3",
"@babel/register": "^7.4.0",
"body-parser": "^1.18.3",
"chai": "^4.2.0",
"chai-http": "^4.3.0",
"cloudinary": "^1.14.0",
Expand All @@ -52,8 +52,11 @@
"passport-twitter": "^1.0.4",
"multer": "^1.4.1",
"multer-storage-cloudinary": "^2.2.1",
"nodemailer": "^6.2.1",
"nodemailer-sendgrid": "^1.0.3",
"passport": "^0.4.0",
"passport-local": "^1.0.0",
"password-hash": "^1.2.2",
"pg": "^7.11.0",
"pg-hstore": "^2.3.3",
"request": "^2.87.0",
Expand Down
190 changes: 190 additions & 0 deletions src/controllers/Auth.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
import bcrypt from 'bcryptjs';
import auth from '../middleware/Auth';
import { Blacklist, User } from '../db/models';
import sendEmail from '../helpers/mail/mailer';
import resetTemplate from '../helpers/mail/mailTemplate/passwordResetTemplate';

/**
* @description Authentication Controller
* @class AuthController
*/
class AuthController {
Expand Down Expand Up @@ -72,6 +77,191 @@ class AuthController {
});
}
}

/**
*
* @description User should recieve a reset token email
* @constructor
* @static
* @param {object} req
* @param {object} res
* @returns {object} res
* @memberof AuthController
*/
static async sendResetToken(req, res) {
try {
const {
email
} = req.body;
const errors = {};

// CHECK IF EMAIL EXIST
const findUser = await User.findOne({
where: {
email
}
});

if (findUser) {
const token = req.generate;
await User.update({
verificationToken: token
}, {
where: {
email
}
});

const url = `${req.protocol}://${req.get('host')}/api/v1/auth/resetPassword?resetToken=${token}`;
const html = resetTemplate(findUser.userName, url);

await sendEmail
.sendEmail(
'do_not_reply@authorhaven.com',
email,
'Password Reset',
html
);

// RETURN SUCCESS IF SUCCESSFUL
return res.status(200).json({
status: 200,
message: 'reset code successfully sent to email',
});
}

// SET ERROR IF EMAIL DOES NOT EXIST
errors.email = 'email does not exist';
return res.status(404).json({
status: 404,
message: errors,
});
} catch (err) {
return res.status(500).json({
status: 500,
message: 'Internal server error',
});
}
}

/**
*
* @description User should recieve a reset token email
* @constructor
* @static
* @param {object} req
* @param {object} res
* @returns {object} res
* @memberof AuthController
*/
static async resendToken(req, res) {
try {
const {
email
} = req.body;
const errors = {};

// CHECK IF EMAIL EXIST
const findUser = await User.findOne({
where: {
email
}
});

if (findUser) {
if (findUser.verificationToken !== '') {
const url = `${req.protocol}://${req.get('host')}/api/v1/auth/resetPassword?resetToken=${findUser.verificationToken}`;
const html = resetTemplate(findUser.userName, url);

await sendEmail
.sendEmail(
'do_not_reply@authorhaven.com',
email,
'Resend Password Reset',
html
);

// RETURN SUCCESS IF SUCCESS
return res.status(200).json({
status: 200,
message: 'reset code resent to your email',
});
}

// SET ERROR IF ERROR
errors.token = 'invalid request';
return res.status(400).json({
status: 400,
message: errors,
});
}

// SET ERROR IF ERROR
errors.email = 'email does not exist';
return res.status(404).json({
status: 404,
message: errors,
});
} catch (err) {
return res.status(500).json({
status: 500,
message: 'Internal server error',
});
}
}

/**
*
* @description User should be able to reset their password with the token
* @constructor
* @static
* @param {object} req
* @param {object} res
* @returns {object} res
* @memberof AuthController
*/
static async resetPassword(req, res) {
try {
const {
resetToken
} = req.query;
const {
password
} = req.body;

// find the token if it exist
const findToken = await User.findOne({
where: {
verificationToken: resetToken
}
});

if (findToken) {
// Hashing Password with bcryptjs
const salt = bcrypt.genSaltSync(10);
const hashPassword = bcrypt.hashSync(password, salt);
await User.update({
password: hashPassword,
verificationToken: ''
}, {
where: {
verificationToken: resetToken
}
});

// RETURN SUCCESS IF SUCCESS
return res.status(200).json({
status: 200,
message: 'password reset successful',
});
}
} catch (err) {
return res.status(500).json({
status: 500,
message: 'Internal server error',
});
}
}
}

export default AuthController;
2 changes: 2 additions & 0 deletions src/db/config/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,4 +58,6 @@ module.exports = {
isProduction: process.env.NODE_ENV === 'production',
port: process.env.PORT || 3000,
secret: process.env.SECRET,
GENERATE_SECRET: process.env.GENERATE_SECRET,
sendgrid: process.env.SENDGRID_API_KEY
};
Loading

0 comments on commit 12d432d

Please sign in to comment.