Skip to content

Set objectId into query for Email Validation #6930

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 5 commits into from
Oct 9, 2020
Merged
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
60 changes: 59 additions & 1 deletion spec/ParseLiveQuery.spec.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
'use strict';

const UserController = require('../lib/Controllers/UserController')
.UserController;
const Config = require('../lib/Config');
describe('ParseLiveQuery', function () {
it('can subscribe to query', async done => {
await reconfigureServer({
Expand Down Expand Up @@ -241,6 +243,62 @@ describe('ParseLiveQuery', function () {
}, 1000);
});

it('should execute live query update on email validation', async done => {
const emailAdapter = {
sendVerificationEmail: () => {},
sendPasswordResetEmail: () => Promise.resolve(),
sendMail: () => {},
};

await reconfigureServer({
liveQuery: {
classNames: ['_User'],
},
startLiveQueryServer: true,
verbose: false,
silent: true,
websocketTimeout: 100,
appName: 'liveQueryEmailValidation',
verifyUserEmails: true,
emailAdapter: emailAdapter,
emailVerifyTokenValidityDuration: 20, // 0.5 second
publicServerURL: 'http://localhost:8378/1',
}).then(() => {
const user = new Parse.User();
user.set('password', 'asdf');
user.set('email', '[email protected]');
user.set('username', 'zxcv');
user
.signUp()
.then(() => {
const config = Config.get('test');
return config.database.find('_User', {
username: 'zxcv',
});
})
.then(async results => {
const foundUser = results[0];
const query = new Parse.Query('_User');
query.equalTo('objectId', foundUser.objectId);
const subscription = await query.subscribe();

subscription.on('update', async object => {
expect(object).toBeDefined();
expect(object.get('emailVerified')).toBe(true);
done();
});

const userController = new UserController(emailAdapter, 'test', {
verifyUserEmails: true,
});
userController.verifyEmail(
foundUser.username,
foundUser._email_verify_token
);
});
});
});

afterEach(async function (done) {
const client = await Parse.CoreManager.getLiveQueryController().getDefaultLiveQueryClient();
client.close();
Expand Down
10 changes: 6 additions & 4 deletions src/Controllers/UserController.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,15 +64,17 @@ export class UserController extends AdaptableController {
updateFields._email_verify_token_expires_at = { __op: 'Delete' };
}
const masterAuth = Auth.master(this.config);
var checkIfAlreadyVerified = new RestQuery(
var findUserForEmailVerification = new RestQuery(
this.config,
Auth.master(this.config),
'_User',
{ username: username, emailVerified: true }
{ username: username }
);
return checkIfAlreadyVerified.execute().then(result => {
if (result.results.length) {
return findUserForEmailVerification.execute().then(result => {
if (result.results.length && result.results[0].emailVerified) {
return Promise.resolve(result.results.length[0]);
} else if (result.results.length) {
query.objectId = result.results[0].objectId;
}
return rest.update(this.config, masterAuth, '_User', query, updateFields);
});
Expand Down