Skip to content

Commit

Permalink
Merge pull request #1133 from carmenlau/reset-password-fix
Browse files Browse the repository at this point in the history
Reset password fix
  • Loading branch information
drew-gross committed Mar 24, 2016
2 parents ee8f85b + 603bf97 commit 82ebba4
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 10 deletions.
10 changes: 9 additions & 1 deletion spec/ValidationAndPasswordsReset.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -573,7 +573,15 @@ describe("Password Reset", () => {
expect(response.body).toEqual('Found. Redirecting to http://localhost:8378/1/apps/password_reset_success.html');

Parse.User.logIn("zxcv", "hello").then(function(user){
done();
let config = new Config('test');
config.database.adaptiveCollection('_User')
.then(coll => coll.find({ 'username': 'zxcv' }, { limit: 1 }))
.then((results) => {
// _perishable_token should be unset after reset password
expect(results.length).toEqual(1);
expect(results[0]['_perishable_token']).toEqual(undefined);
done();
});
}, (err) => {
console.error(err);
fail("should login with new password");
Expand Down
25 changes: 16 additions & 9 deletions src/Controllers/UserController.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { randomString } from '../cryptoUtils';
import { inflate } from '../triggers';
import AdaptableController from './AdaptableController';
import MailAdapter from '../Adapters/Email/MailAdapter';
import rest from '../rest';

var DatabaseAdapter = require('../DatabaseAdapter');
var RestWrite = require('../RestWrite');
Expand Down Expand Up @@ -165,9 +166,17 @@ export class UserController extends AdaptableController {
}

updatePassword(username, token, password, config) {
return this.checkResetTokenValidity(username, token).then(() => {
return updateUserPassword(username, token, password, this.config);
});
return this.checkResetTokenValidity(username, token).then((user) => {
return updateUserPassword(user._id, password, this.config);
}).then(() => {
// clear reset password token
return this.config.database.adaptiveCollection('_User').then(function (collection) {
// Need direct database access because verification token is not a parse field
return collection.findOneAndUpdate({ username: username },// query
{ $unset: { _perishable_token: null } } // update
);
});
});
}

defaultVerificationEmail({link, user, appName, }) {
Expand All @@ -192,12 +201,10 @@ export class UserController extends AdaptableController {
}

// Mark this private
function updateUserPassword(username, token, password, config) {
var write = new RestWrite(config, Auth.master(config), '_User', {
username: username,
_perishable_token: token
}, {password: password, _perishable_token: null }, undefined);
return write.execute();
function updateUserPassword(userId, password, config) {
return rest.update(config, Auth.master(config), '_User', userId, {
password: password
});
}

export default UserController;

0 comments on commit 82ebba4

Please sign in to comment.