Skip to content

Improve reset password API #6830

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 9 commits into
base: alpha
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion spec/PasswordPolicy.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,7 @@ describe('Password Policy: ', () => {
done();
})
.catch(error => {
// ToDO: 142 error is too generic, a more specific error code should be thrown like Parse.Error.PASSWORD_POLICY_USERNAME
expect(error.code).toEqual(142);
done();
});
Expand Down Expand Up @@ -946,8 +947,9 @@ describe('Password Policy: ', () => {
});
} catch (error) {
expect(error.status).not.toBe(302);
// ToDO: 142 error is too generic, a more specific error code should be thrown like Parse.Error.PASSWORD_POLICY_USERNAME
expect(error.text).toEqual(
'{"code":-1,"error":"Password cannot contain your username."}'
'{"code":142,"error":"Password cannot contain your username."}'
);
}
await Parse.User.logIn('user1', 'r@nd0m');
Expand Down
7 changes: 5 additions & 2 deletions spec/PublicAPI.spec.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
const req = require('../lib/request');

const request = function(url, callback) {
const request = function (url, callback) {
return req({
url,
}).then(response => callback(null, response), err => callback(err, err));
}).then(
response => callback(null, response),
err => callback(err, err)
);
};

describe('public API', () => {
Expand Down
15 changes: 9 additions & 6 deletions src/Controllers/UserController.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,10 @@ export class UserController extends AdaptableController {
)
.then(results => {
if (results.length != 1) {
throw 'Failed to reset password: username / email / token is invalid';
throw new Parse.Error(
Parse.Error.RESET_PASSWORD_ERROR,
'Failed to reset password: username / email / token is invalid'
);
}

if (
Expand All @@ -102,7 +105,10 @@ export class UserController extends AdaptableController {
expiresDate = new Date(expiresDate.iso);
}
if (expiresDate < new Date())
throw 'The password reset link has expired';
throw new Parse.Error(
Parse.Error.RESET_LINK_EXPIRED,
'The password reset link has expired'
);
}

return results[0];
Expand Down Expand Up @@ -246,10 +252,7 @@ export class UserController extends AdaptableController {
return this.checkResetTokenValidity(username, token)
.then(user => updateUserPassword(user.objectId, password, this.config))
.catch(error => {
if (error && error.message) {
// in case of Parse.Error, fail with the error message only
return Promise.reject(error.message);
} else {
if (error) {
return Promise.reject(error);
}
});
Expand Down
4 changes: 2 additions & 2 deletions src/Routers/PublicAPIRouter.js
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ export class PublicAPIRouter extends PromiseRouter {
username: username,
token: token,
id: config.applicationId,
error: result.err,
error: result.err ? result.err.message : result.err,
app: config.appName,
});

Expand All @@ -215,7 +215,7 @@ export class PublicAPIRouter extends PromiseRouter {
});
}
if (result.err) {
throw new Parse.Error(Parse.Error.OTHER_CAUSE, `${result.err}`);
throw result.err;
}
}

Expand Down