Skip to content

feat: Add conditional email verification via dynamic Parse Server options verifyUserEmails, sendUserEmailVerification that now accept functions #8425

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

Merged
merged 26 commits into from
Jun 20, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
39c0ada
feat: add conditional email
dblythy Feb 5, 2023
f68cdef
Merge branch 'alpha' into conditional-email
dblythy Feb 9, 2023
29b279c
Merge branch 'alpha' into conditional-email
mtrezza Feb 27, 2023
f399e5c
wip
dblythy Mar 1, 2023
620aa27
Merge branch 'alpha' into conditional-email
dblythy Mar 2, 2023
edd3495
Update EmailVerificationToken.spec.js
dblythy Mar 2, 2023
ce9f3f5
refactor
dblythy Mar 2, 2023
85119fc
Merge branch 'alpha' into conditional-email
dblythy Mar 5, 2023
88c687c
Merge branch 'alpha' into conditional-email
dblythy Mar 29, 2023
95785e2
add sendUserEmailVerification
dblythy Mar 29, 2023
051810d
Merge branch 'conditional-email' of https://github.com/dblythy/parse-…
dblythy Mar 29, 2023
006356a
tests
dblythy Mar 29, 2023
ed71cfd
tests
dblythy Mar 29, 2023
e64410a
Merge branch 'alpha' into conditional-email
mtrezza May 21, 2023
bba6750
fix typo
mtrezza May 21, 2023
5283b60
fix typo in definitions
mtrezza May 21, 2023
4d40339
fix typo
mtrezza May 21, 2023
50314b3
wip
dblythy May 22, 2023
860c2dd
Merge branch 'alpha' into conditional-email
mtrezza May 22, 2023
bb1ed0f
Merge branch 'alpha' into conditional-email
dblythy Jun 9, 2023
d9eb91f
tests
dblythy Jun 9, 2023
51ca662
Merge branch 'alpha' into conditional-email
mtrezza Jun 9, 2023
c9b873c
Merge branch 'alpha' into conditional-email
dblythy Jun 19, 2023
2a6b0d7
wip
dblythy Jun 19, 2023
d457358
Merge branch 'conditional-email' of https://github.com/dblythy/parse-…
dblythy Jun 19, 2023
9d87f1e
Merge branch 'alpha' into conditional-email
mtrezza Jun 20, 2023
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
106 changes: 106 additions & 0 deletions spec/EmailVerificationToken.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,112 @@ describe('Email Verification Token Expiration: ', () => {
});
});

it('can conditionally send emails', async () => {
let sendEmailOptions;
const emailAdapter = {
sendVerificationEmail: options => {
sendEmailOptions = options;
},
sendPasswordResetEmail: () => Promise.resolve(),
sendMail: () => {},
};
await reconfigureServer({
appName: 'emailVerifyToken',
verifyUserEmails: true,
emailAdapter: emailAdapter,
emailVerifyTokenValidityDuration: 5, // 5 seconds
publicServerURL: 'http://localhost:8378/1',
});
const beforeSave = {
method(req) {
expect(req.setSendEmailVerificationEmail).toBeTrue();
expect(req.setEmailVerified).toBeFalse();
req.setSendEmailVerificationEmail = false;
req.setEmailVerified = true;
},
};
const saveSpy = spyOn(beforeSave, 'method').and.callThrough();
const emailSpy = spyOn(emailAdapter, 'sendVerificationEmail').and.callThrough();
Parse.Cloud.beforeSave(Parse.User, beforeSave.method);
const user = new Parse.User();
user.setUsername('sets_email_verify_token_expires_at');
user.setPassword('expiringToken');
user.set('email', '[email protected]');
await user.signUp();

const config = Config.get('test');
const results = await config.database.find(
'_User',
{
username: 'sets_email_verify_token_expires_at',
},
{},
Auth.maintenance(config)
);

expect(results.length).toBe(1);
const user_data = results[0];
expect(typeof user_data).toBe('object');
expect(user_data.emailVerified).toEqual(true);
expect(user_data._email_verify_token).toBeUndefined();
expect(user_data._email_verify_token_expires_at).toBeUndefined();
expect(emailSpy).not.toHaveBeenCalled();
expect(saveSpy).toHaveBeenCalled();
expect(sendEmailOptions).toBeUndefined();
});

it('beforeSave options do not change existing behaviour', async () => {
let sendEmailOptions;
const emailAdapter = {
sendVerificationEmail: options => {
sendEmailOptions = options;
},
sendPasswordResetEmail: () => Promise.resolve(),
sendMail: () => {},
};
await reconfigureServer({
appName: 'emailVerifyToken',
verifyUserEmails: true,
emailAdapter: emailAdapter,
emailVerifyTokenValidityDuration: 5, // 5 seconds
publicServerURL: 'http://localhost:8378/1',
});
const beforeSave = {
method(req) {
if (!req.original) {
expect(req.setSendEmailVerificationEmail).toBeTrue();
expect(req.setEmailVerified).toBeFalse();
}
},
};
const saveSpy = spyOn(beforeSave, 'method').and.callThrough();
const emailSpy = spyOn(emailAdapter, 'sendVerificationEmail').and.callThrough();
Parse.Cloud.beforeSave(Parse.User, beforeSave.method);
const newUser = new Parse.User();
newUser.setUsername('unsets_email_verify_token_expires_at');
newUser.setPassword('expiringToken');
newUser.set('email', '[email protected]');
await newUser.signUp();
const response = await request({
url: sendEmailOptions.link,
followRedirects: false,
});
expect(response.status).toEqual(302);
const config = Config.get('test');
const results = await config.database.find('_User', {
username: 'unsets_email_verify_token_expires_at',
});

expect(results.length).toBe(1);
const user = results[0];
expect(typeof user).toBe('object');
expect(user.emailVerified).toEqual(true);
expect(typeof user._email_verify_token).toBe('undefined');
expect(typeof user._email_verify_token_expires_at).toBe('undefined');
expect(saveSpy).toHaveBeenCalled();
expect(emailSpy).toHaveBeenCalled();
});

it('unsets the _email_verify_token_expires_at and _email_verify_token fields in the User class if email verification is successful', done => {
const user = new Parse.User();
let sendEmailOptions;
Expand Down
28 changes: 23 additions & 5 deletions src/RestWrite.js
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,11 @@ RestWrite.prototype.runBeforeSaveTrigger = function () {
identifier,
};

const additionalData = {};
if (this.config.userController.shouldVerifyEmails && !originalObject) {
additionalData.setSendEmailVerificationEmail = true;
additionalData.setEmailVerified = false;
}
return Promise.resolve()
.then(() => {
// Before calling the trigger, validate the permissions for the save operation
Expand Down Expand Up @@ -277,7 +282,8 @@ RestWrite.prototype.runBeforeSaveTrigger = function () {
updatedObject,
originalObject,
this.config,
this.context
this.context,
additionalData
);
})
.then(response => {
Expand All @@ -299,6 +305,13 @@ RestWrite.prototype.runBeforeSaveTrigger = function () {
}
}
this.checkProhibitedKeywords(this.data);

if (!additionalData.setSendEmailVerificationEmail) {
this.storage.sendVerificationEmail = false;
}
if (additionalData.setEmailVerified) {
this.storage.emailVerified = true;
}
});
};

Expand Down Expand Up @@ -612,6 +625,10 @@ RestWrite.prototype.transformUser = function () {
throw new Parse.Error(Parse.Error.OPERATION_FORBIDDEN, error);
}

if (this.storage.emailVerified) {
this.data.emailVerified = true;
}

// Do not cleanup session if objectId is not set
if (this.query && this.objectId()) {
// If we're updating a _User object, we need to clear out the cache for that user. Find all their
Expand Down Expand Up @@ -742,10 +759,11 @@ RestWrite.prototype._validateEmail = function () {
);
}
if (
!this.data.authData ||
!Object.keys(this.data.authData).length ||
(Object.keys(this.data.authData).length === 1 &&
Object.keys(this.data.authData)[0] === 'anonymous')
(!this.data.authData ||
!Object.keys(this.data.authData).length ||
(Object.keys(this.data.authData).length === 1 &&
Object.keys(this.data.authData)[0] === 'anonymous')) &&
this.storage.sendVerificationEmail !== false
) {
// We updated the email, send a new validation
this.storage['sendVerificationEmail'] = true;
Expand Down
9 changes: 8 additions & 1 deletion src/triggers.js
Original file line number Diff line number Diff line change
Expand Up @@ -831,7 +831,8 @@ export function maybeRunTrigger(
parseObject,
originalParseObject,
config,
context
context,
additionalData
) {
if (!parseObject) {
return Promise.resolve({});
Expand All @@ -847,6 +848,9 @@ export function maybeRunTrigger(
config,
context
);
if (additionalData) {
Object.assign(request, additionalData);
}
var { success, error } = getResponseObject(
request,
object => {
Expand All @@ -868,6 +872,9 @@ export function maybeRunTrigger(
) {
Object.assign(context, request.context);
}
for (const key in additionalData || {}) {
additionalData[key] = request[key];
}
resolve(object);
},
error => {
Expand Down