Skip to content

Commit

Permalink
ft(password reset):user should reset password
Browse files Browse the repository at this point in the history
- reset password via email

[finishes #167727456]

ft(password reset):reset password via email
  • Loading branch information
danndav committed Sep 5, 2019
1 parent e0e33f4 commit 74c8e9b
Show file tree
Hide file tree
Showing 6 changed files with 142 additions and 2 deletions.
51 changes: 51 additions & 0 deletions src/controllers/user.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -131,4 +131,55 @@ export default class UserController {
Response.UnauthorizedError(res, { message: 'Unable to sign in' });
}
}

/**
*
*
* @static
* @param {object} req
* @param {object} res
* @returns {json} - json
* @memberof UserController
*/
static async resetpasswordEmail(req, res) {
let userToken;
let payload;
const userData = UserUtils.getUserSignupData(req.body);

await User.findOne({ attributes: ['first_name', 'email', 'id'], where: { email: userData.email } })
.then((data) => {
payload = data;
userToken = JWTService.generatePasswordToken(data);
})
.catch(() => res.status(404).json({
status: 'error',
message: 'user email not found',
}));


await User.update({
password_reset_token: userToken
}, { where: { email: userData.email } })
.then(() => {
payload.dataValues.token = userToken;
const { first_name: firstName, email } = payload.dataValues;
sender.sendEmail(process.env.SENDER_EMAIL, payload.dataValues.email, 'passord_reset', { firstName, email });
})
.then(() => {
res.status(200).json({
status: 'success',
data: {
message: 'you will receive a link in your mail shortly to proceed'
}
});
})
.catch((err) => {
res.status(500).json({
status: 'error',
message: err.message,
info: 'password reset failed',
err
});
});
}
}
2 changes: 2 additions & 0 deletions src/routes/api/auth.router.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import UserController from '../../controllers/user.controller';
import UserMiddleware from '../../middlewares/user.middleware';
import socialMockMiddleWare from '../../middlewares/social-mock.middleware';


const router = express.Router();

const authBase = '/auth';
Expand All @@ -14,6 +15,7 @@ const authBase = '/auth';
router.post(`${authBase}/signup`, UserController.signup);
router.post('/auth/signin', ...UserMiddleware.validateSigninFields(),
UserController.signin);
router.post('/password-reset', UserController.resetpasswordEmail);

/* google */
router.get(`${authBase}/google`, passport.authenticate('google', {
Expand Down
4 changes: 3 additions & 1 deletion src/services/email.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ sgMail.setApiKey(process.env.SEND_GRID_API);
const templates = {
travel_request_notification: 'd-963a476c77a34f318895713712b4d6bb',
signup_template: 'd-1ae0bd2e62c742e9a78009512bd1b5b8',
request_rejected: 'd-ccd25aa2dd9f47cb9d746d909787db59'
request_rejected: 'd-ccd25aa2dd9f47cb9d746d909787db59',
passord_reset: 'd-0e43d73f3e3048bba2d124ff5f384107'

};

/**
Expand Down
10 changes: 10 additions & 0 deletions src/services/jwt.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,14 @@ export default class JWTService {
return jwt.sign({ id, role },
process.env.JWT_SECRET, { expiresIn: '7d' });
}

/**
* Generates a new token for a particular user
* @param {string} data
* @returns {string} token
*/
static generatePasswordToken(data) {
return jwt.sign({ id: data.id, role: data.role },
process.env.JWT_SECRET, { expiresIn: '1h' });
}
}
35 changes: 35 additions & 0 deletions src/test/signup.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import chaiHttp from 'chai-http';
import app from '../index';

chai.use(chaiHttp);
const { expect } = chai;
chai.should();

const user = {
Expand Down Expand Up @@ -51,4 +52,38 @@ describe('Users', () => {
})
});
});

describe('/Post Requests', () => {

it('It should fail to reset password with an invalid mail', (done) => {
chai
.request(app)
.post('/api/v1/password-reset')
.send({email:'d@ff'})
.end((err, res) => {
res.should.have.status(404);
res.body.should.have.property('status').to.equals('error');
res.body.should.have
.property('message')
.to.equals('user email not found');

done();
});
});

it('It should receive a mail ', (done) => {
chai
.request(app)
.post('/api/v1/password-reset')
.send({email:'john_doe@email.com'})
.end((err, res) => {
res.should.have.status(200);
res.body.should.have.property('status').to.equals('success');
res.body.should.have.property('data').to.be.an('object')

done();
});
});

});
});
42 changes: 41 additions & 1 deletion swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,47 @@
}
}
}
}
},
"/auth/passwordreset": {
"get": {
"tags": [
"Auth"
],
"summary": "Enables a user to request a password reset",
"operationId": "passwordReset",
"consumes": [
"application/json"
],
"produces": [
"application/json"
],
"parameters": [
{
"in": "body",
"name": "body",
"description": "registered user email ",
"required": true,
"schema": {
"$ref": "#/definitions/UserMail"
}
}
],
"responses": {
"200": {
"description": "you will receive a link in your mail shortly to proceed",
"schema": {
"$ref": "#/definitions/PasswordResetSucess"
}
},
"400": {
"description": "password reset failed",
"schema": {
"$ref": "#/definitions/PasswordResetFail"
}
}
}
}
},
"/auth/facebook": {
"get": {
Expand Down Expand Up @@ -199,7 +240,6 @@
}
}
}
}
},

"requestBody": {
Expand Down

0 comments on commit 74c8e9b

Please sign in to comment.