Skip to content

Commit

Permalink
feat: Add installationId, ip, resendRequest to arguments passed…
Browse files Browse the repository at this point in the history
… 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 signup or login; the user object is not a plain JavaScript object anymore but an instance of `Parse.User`
  • Loading branch information
mtrezza committed Jan 6, 2024
1 parent 0d58e39 commit 8adcbee
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 4 deletions.
36 changes: 36 additions & 0 deletions spec/EmailVerificationToken.spec.js
Expand Up @@ -3,6 +3,7 @@
const Auth = require('../lib/Auth');
const Config = require('../lib/Config');
const request = require('../lib/request');
const MockEmailAdapterWithOptions = require('./support/MockEmailAdapterWithOptions');

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

it('provides function arguments in verifyUserEmails on verificationEmailRequest', async () => {
const user = new Parse.User();
user.setUsername('user');
user.setPassword('pass');
user.set('email', 'test@example.com');
await user.signUp();

const verifyUserEmails = {
method: async (params) => {
expect(params.object).toBeInstanceOf(Parse.User);
expect(params.ip).toBeDefined();
expect(params.master).toBeDefined();
expect(params.installationId).toBeDefined();
expect(params.resendRequest).toBeTrue();
return true;
},
};
const verifyUserEmailsSpy = spyOn(verifyUserEmails, 'method').and.callThrough();
await reconfigureServer({
appName: 'test',
publicServerURL: 'http://localhost:1337/1',
verifyUserEmails: verifyUserEmails.method,
preventLoginWithUnverifiedEmail: verifyUserEmails.method,
preventSignupWithUnverifiedEmail: true,
emailAdapter: MockEmailAdapterWithOptions({
fromAddress: 'parse@example.com',
apiKey: 'k',
domain: 'd',
}),
});

await expectAsync(Parse.User.requestEmailVerification('test@example.com')).toBeResolved();
expect(verifyUserEmailsSpy).toHaveBeenCalledTimes(1);
});

it('should throw with invalid emailVerifyTokenReuseIfValid', async done => {
const sendEmailOptions = [];
const emailAdapter = {
Expand Down
12 changes: 9 additions & 3 deletions src/Controllers/UserController.js
Expand Up @@ -197,7 +197,7 @@ export class UserController extends AdaptableController {
* @param user
* @returns {*}
*/
async regenerateEmailVerifyToken(user, master) {
async regenerateEmailVerifyToken(user, master, installationId, ip) {
const { _email_verify_token } = user;
let { _email_verify_token_expires_at } = user;
if (_email_verify_token_expires_at && _email_verify_token_expires_at.__type === 'Date') {
Expand All @@ -211,7 +211,13 @@ export class UserController extends AdaptableController {
) {
return Promise.resolve();
}
const shouldSend = await this.setEmailVerifyToken(user, { user, master });
const shouldSend = await this.setEmailVerifyToken(user, {
object: Parse.User.fromJSON(Object.assign({ className: '_User' }, user)),
master,
installationId,
ip,
resendRequest: true
});
if (!shouldSend) {
return;
}
Expand All @@ -223,7 +229,7 @@ export class UserController extends AdaptableController {
if (!aUser || aUser.emailVerified) {
throw undefined;
}
const generate = await this.regenerateEmailVerifyToken(aUser, req.auth?.isMaster);
const generate = await this.regenerateEmailVerifyToken(aUser, req.auth?.isMaster, req.auth?.installationId, req.ip);
if (generate) {
this.sendVerificationEmail(aUser, req);
}
Expand Down
2 changes: 1 addition & 1 deletion src/Routers/UsersRouter.js
Expand Up @@ -490,7 +490,7 @@ export class UsersRouter extends ClassesRouter {
}

const userController = req.config.userController;
const send = await userController.regenerateEmailVerifyToken(user, req.auth.isMaster);
const send = await userController.regenerateEmailVerifyToken(user, req.auth.isMaster, req.auth.installationId, req.ip);
if (send) {
userController.sendVerificationEmail(user, req);
}
Expand Down

0 comments on commit 8adcbee

Please sign in to comment.