Skip to content

Commit e30989c

Browse files
authored
Lookup for email in username field to match docs if email is undefined (#2732)
* Lookup for email in username field to match docs if email is undefined * Adds support for sendMail option to when email is selected * Proper does not exists clause
1 parent 7e037ff commit e30989c

File tree

3 files changed

+53
-3
lines changed

3 files changed

+53
-3
lines changed

spec/MockEmailAdapterWithOptions.js

+12-1
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,20 @@ module.exports = options => {
22
if (!options) {
33
throw "Options were not provided"
44
}
5-
return {
5+
let adapter = {
66
sendVerificationEmail: () => Promise.resolve(),
77
sendPasswordResetEmail: () => Promise.resolve(),
88
sendMail: () => Promise.resolve()
9+
};
10+
if (options.sendMail) {
11+
adapter.sendMail = options.sendMail
912
}
13+
if (options.sendPasswordResetEmail) {
14+
adapter.sendPasswordResetEmail = options.sendPasswordResetEmail
15+
}
16+
if (options.sendVerificationEmail) {
17+
adapter.sendVerificationEmail = options.sendVerificationEmail;
18+
}
19+
20+
return adapter;
1021
}

spec/ValidationAndPasswordsReset.spec.js

+39
Original file line numberDiff line numberDiff line change
@@ -479,6 +479,45 @@ describe("Custom Pages, Email Verification, Password Reset", () => {
479479
});
480480
});
481481

482+
it('succeeds sending a password reset username if appName, publicServerURL, and email adapter are prodvided', done => {
483+
let adapter = MockEmailAdapterWithOptions({
484+
fromAddress: '[email protected]',
485+
apiKey: 'k',
486+
domain: 'd',
487+
sendMail: function(options) {
488+
expect(options.to).toEqual('[email protected]');
489+
return Promise.resolve();
490+
}
491+
});
492+
493+
// delete that handler to force using the default
494+
delete adapter.sendPasswordResetEmail;
495+
496+
spyOn(adapter, 'sendMail').and.callThrough();
497+
reconfigureServer({
498+
appName: 'coolapp',
499+
publicServerURL: 'http://localhost:1337/1',
500+
emailAdapter: adapter
501+
})
502+
.then(() => {
503+
let user = new Parse.User();
504+
user.setPassword("asdf");
505+
user.setUsername("[email protected]");
506+
user.signUp(null)
507+
.then(user => Parse.User.requestPasswordReset("[email protected]"))
508+
.then(result => {
509+
expect(adapter.sendMail).toHaveBeenCalled();
510+
done();
511+
}, error => {
512+
done(error);
513+
});
514+
})
515+
.catch(error => {
516+
fail(JSON.stringify(error));
517+
done();
518+
});
519+
});
520+
482521
it('does not send verification email if email verification is disabled', done => {
483522
var emailAdapter = {
484523
sendVerificationEmail: () => Promise.resolve(),

src/Controllers/UserController.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ export class UserController extends AdaptableController {
125125
}
126126

127127
setPasswordResetToken(email) {
128-
return this.config.database.update('_User', { email }, { _perishable_token: randomString(25) }, {}, true)
128+
return this.config.database.update('_User', { $or: [{email}, {username: email, email: {$exists: false}}] }, { _perishable_token: randomString(25) }, {}, true)
129129
}
130130

131131
sendPasswordResetEmail(email) {
@@ -181,7 +181,7 @@ export class UserController extends AdaptableController {
181181
"You requested to reset your password for " + appName + ".\n\n" +
182182
"" +
183183
"Click here to reset it:\n" + link;
184-
let to = user.get("email");
184+
let to = user.get("email") || user.get('username');
185185
let subject = 'Password Reset for ' + appName;
186186
return { text, to, subject };
187187
}

0 commit comments

Comments
 (0)