1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071 |
- module.exports = {
- friendlyName: 'Update password and login',
- description: 'Finish the password recovery flow by setting the new password and '+
- 'logging in the requesting user, based on the authenticity of their token.',
- inputs: {
- password: {
- description: 'The new, unencrypted password.',
- example: 'abc123v2',
- required: true
- },
- token: {
- description: 'The password token that was generated by the `sendPasswordRecoveryEmail` endpoint.',
- example: 'gwa8gs8hgw9h2g9hg29hgwh9asdgh9q34$$$$$asdgasdggds',
- required: true
- }
- },
- exits: {
- invalidToken: {
- description: 'The provided password token is invalid, expired, or has already been used.',
- responseType: 'expired'
- }
- },
- fn: async function (inputs, exits) {
- if(!inputs.token) {
- throw 'invalidToken';
- }
- // Look up the user with this reset token.
- var userRecord = await User.findOne({ passwordResetToken: inputs.token });
- // If no such user exists, or their token is expired, bail.
- if (!userRecord || userRecord.passwordResetTokenExpiresAt <= Date.now()) {
- throw 'invalidToken';
- }
- // Hash the new password.
- var hashed = await sails.helpers.passwords.hashPassword(inputs.password);
- // Store the user's new password and clear their reset token so it can't be used again.
- await User.update({ id: userRecord.id }).set({
- password: hashed,
- passwordResetToken: '',
- passwordResetTokenExpiresAt: 0
- });
- // Log the user in.
- this.req.session.userId = userRecord.id;
- return exits.success();
- }
- };
|