feathers-authentication-management
Version:
Adds sign up verification, forgotten password reset, and other capabilities to local feathers-authentication
41 lines (31 loc) • 1.6 kB
JavaScript
const errors = require('@feathersjs/errors');
const makeDebug = require('debug');
const comparePasswords = require('./helpers/compare-passwords');
const ensureObjPropsValid = require('./helpers/ensure-obj-props-valid');
const ensureValuesAreStrings = require('./helpers/ensure-values-are-strings');
const getUserData = require('./helpers/get-user-data');
const hashPassword = require('./helpers/hash-password');
const notifier = require('./helpers/notifier');
const debug = makeDebug('authLocalMgnt:passwordChange');
module.exports = passwordChange;
async function passwordChange (options, identifyUser, oldPassword, password, field, notifierOptions = {}) {
debug('passwordChange', oldPassword, password);
const usersService = options.app.service(options.service);
const usersServiceIdName = usersService.id;
ensureValuesAreStrings(oldPassword, password);
ensureObjPropsValid(identifyUser, options.identifyUserProps);
const users = await usersService.find({ query: identifyUser });
const user1 = getUserData(users);
try {
await comparePasswords(oldPassword, user1.password, () => { });
} catch (err) {
throw new errors.BadRequest('Current password is incorrect.', {
errors: { oldPassword: 'Current password is incorrect.' }
});
}
const user2 = await usersService.patch(user1[usersServiceIdName], {
password: await hashPassword(options.app, password, field)
});
const user3 = await notifier(options.notifier, 'passwordChange', user2, notifierOptions);
return options.sanitizeUserForClient(user3);
}