Guest User

Untitled

a guest
Feb 1st, 2018
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.26 KB | None | 0 0
  1. const auth = require('@feathersjs/authentication');
  2. const errors = require('@feathersjs/errors');
  3. const bcrypt = require('bcryptjs');
  4.  
  5. const comparePasswords = (oldPassword, password) => new Promise((resolve, reject) => {
  6. bcrypt.compare(oldPassword, password, (err, data1) => {
  7. if(err || !data1) return reject();
  8. return resolve();
  9. });
  10. });
  11.  
  12. module.exports = function() {
  13. const app = this;
  14.  
  15. // Add authentication/changePassword service
  16. const changePasswordService = app.use('authentication/changePassword', {
  17. async create(data, params) {
  18. // const user = await app.service('users').get(params.payload.userId);
  19. const user = params.user;
  20. if(!data.password) throw new errors.BadRequest(`Missing password`);
  21. if(!data.oldPassword) throw new errors.BadRequest(`Missing oldPassword`);
  22. try {
  23. await comparePasswords(data.oldPassword, user.password);
  24. }
  25. catch(e) {
  26. throw new errors.BadRequest('Current password wrong');
  27. }
  28. const newUser = await app.service('users').patch(user._id, {password: data.password});
  29. delete newUser.password; // never send pwd to client
  30. return newUser;
  31. }
  32. });
  33.  
  34. // Add jwt authentication
  35. changePasswordService.hooks({
  36. before: auth.hooks.authenticate('jwt')
  37. });
  38. };
Add Comment
Please, Sign In to add comment