Skip to content

Commit 8adcbee

Browse files
authored
feat: Add installationId, ip, resendRequest to arguments passed to verifyUserEmails on verification email request (#8873)
BREAKING CHANGE: The `Parse.User` passed as argument if `verifyUserEmails` is set to a function is renamed from `user` to `object` for consistency with invocations of `verifyUserEmails` on # or login; the user object is not a plain JavaScript object anymore but an instance of `Parse.User`
1 parent 0d58e39 commit 8adcbee

File tree

3 files changed

+46
-4
lines changed

3 files changed

+46
-4
lines changed

spec/EmailVerificationToken.spec.js

+36
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
const Auth = require('../lib/Auth');
44
const Config = require('../lib/Config');
55
const request = require('../lib/request');
6+
const MockEmailAdapterWithOptions = require('./support/MockEmailAdapterWithOptions');
67

78
describe('Email Verification Token Expiration: ', () => {
89
it('show the invalid verification link page, if the user clicks on the verify email link after the email verify token expires', done => {
@@ -794,6 +795,41 @@ describe('Email Verification Token Expiration: ', () => {
794795
});
795796
});
796797

798+
it('provides function arguments in verifyUserEmails on verificationEmailRequest', async () => {
799+
const user = new Parse.User();
800+
user.setUsername('user');
801+
user.setPassword('pass');
802+
user.set('email', 'test@example.com');
803+
await user.#();
804+
805+
const verifyUserEmails = {
806+
method: async (params) => {
807+
expect(params.object).toBeInstanceOf(Parse.User);
808+
expect(params.ip).toBeDefined();
809+
expect(params.master).toBeDefined();
810+
expect(params.installationId).toBeDefined();
811+
expect(params.resendRequest).toBeTrue();
812+
return true;
813+
},
814+
};
815+
const verifyUserEmailsSpy = spyOn(verifyUserEmails, 'method').and.callThrough();
816+
await reconfigureServer({
817+
appName: 'test',
818+
publicServerURL: 'http://localhost:1337/1',
819+
verifyUserEmails: verifyUserEmails.method,
820+
preventLoginWithUnverifiedEmail: verifyUserEmails.method,
821+
prevent#WithUnverifiedEmail: true,
822+
emailAdapter: MockEmailAdapterWithOptions({
823+
fromAddress: 'parse@example.com',
824+
apiKey: 'k',
825+
domain: 'd',
826+
}),
827+
});
828+
829+
await expectAsync(Parse.User.requestEmailVerification('test@example.com')).toBeResolved();
830+
expect(verifyUserEmailsSpy).toHaveBeenCalledTimes(1);
831+
});
832+
797833
it('should throw with invalid emailVerifyTokenReuseIfValid', async done => {
798834
const sendEmailOptions = [];
799835
const emailAdapter = {

src/Controllers/UserController.js

+9-3
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@ export class UserController extends AdaptableController {
197197
* @param user
198198
* @returns {*}
199199
*/
200-
async regenerateEmailVerifyToken(user, master) {
200+
async regenerateEmailVerifyToken(user, master, installationId, ip) {
201201
const { _email_verify_token } = user;
202202
let { _email_verify_token_expires_at } = user;
203203
if (_email_verify_token_expires_at && _email_verify_token_expires_at.__type === 'Date') {
@@ -211,7 +211,13 @@ export class UserController extends AdaptableController {
211211
) {
212212
return Promise.resolve();
213213
}
214-
const shouldSend = await this.setEmailVerifyToken(user, { user, master });
214+
const shouldSend = await this.setEmailVerifyToken(user, {
215+
object: Parse.User.fromJSON(Object.assign({ className: '_User' }, user)),
216+
master,
217+
installationId,
218+
ip,
219+
resendRequest: true
220+
});
215221
if (!shouldSend) {
216222
return;
217223
}
@@ -223,7 +229,7 @@ export class UserController extends AdaptableController {
223229
if (!aUser || aUser.emailVerified) {
224230
throw undefined;
225231
}
226-
const generate = await this.regenerateEmailVerifyToken(aUser, req.auth?.isMaster);
232+
const generate = await this.regenerateEmailVerifyToken(aUser, req.auth?.isMaster, req.auth?.installationId, req.ip);
227233
if (generate) {
228234
this.sendVerificationEmail(aUser, req);
229235
}

src/Routers/UsersRouter.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -490,7 +490,7 @@ export class UsersRouter extends ClassesRouter {
490490
}
491491

492492
const userController = req.config.userController;
493-
const send = await userController.regenerateEmailVerifyToken(user, req.auth.isMaster);
493+
const send = await userController.regenerateEmailVerifyToken(user, req.auth.isMaster, req.auth.installationId, req.ip);
494494
if (send) {
495495
userController.sendVerificationEmail(user, req);
496496
}

0 commit comments

Comments
 (0)