Skip to content

Commit 6655eb3

Browse files
author
Vishal
committed
feat:feat: Pass event information to verifyUserEmails
1 parent 517bd62 commit 6655eb3

File tree

3 files changed

+82
-15
lines changed

3 files changed

+82
-15
lines changed

spec/verifyUserEmails.spec.js

+66
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
'use strict';
2+
3+
const { getAuthForSessionToken } = require('../lib/Auth');
4+
const Config = require('../lib/Config');
5+
6+
describe('verifyUserEmails with Auth Context', () => {
7+
let user;
8+
let config;
9+
10+
beforeEach(async (done) => {
11+
await reconfigureServer({
12+
verifyUserEmails: jasmine.createSpy('verifyUserEmails'),
13+
});
14+
15+
user = new Parse.User();
16+
await user.signUp({
17+
username: 'testuser',
18+
password: 'securepassword',
19+
20+
});
21+
22+
config = Config.get('test');
23+
done();
24+
});
25+
26+
it('should call verifyUserEmails with correct auth context on signup', async (done) => {
27+
const sessionToken = user.getSessionToken();
28+
expect(sessionToken).toBeDefined();
29+
30+
await getAuthForSessionToken({
31+
sessionToken,
32+
config,
33+
});
34+
35+
expect(config.verifyUserEmails).toHaveBeenCalledWith({
36+
action: 'signup',
37+
authProvider: 'password',
38+
});
39+
done();
40+
});
41+
42+
it('should call verifyUserEmails with correct auth context on login', async (done) => {
43+
await Parse.User.logIn('testuser', 'securepassword');
44+
45+
expect(config.verifyUserEmails).toHaveBeenCalledWith({
46+
action: 'login',
47+
authProvider: 'password',
48+
});
49+
done();
50+
});
51+
52+
it('should call verifyUserEmails with correct provider for social login', async (done) => {
53+
const socialAuthData = {
54+
id: '1234567890',
55+
access_token: 'mockAccessToken',
56+
};
57+
58+
await Parse.User.logInWith('facebook', { authData: socialAuthData });
59+
60+
expect(config.verifyUserEmails).toHaveBeenCalledWith({
61+
action: 'login',
62+
authProvider: 'facebook',
63+
});
64+
done();
65+
});
66+
});

src/Controllers/UserController.js

+11-4
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,9 @@ var RestQuery = require('../RestQuery');
1111
var Auth = require('../Auth');
1212

1313
export class UserController extends AdaptableController {
14-
constructor(adapter, appId, options = {}) {
14+
constructor(adapter, appId, options = {}, authContext = {}) {
1515
super(adapter, appId, options);
16+
this.authContext = authContext;
1617
}
1718

1819
get config() {
@@ -38,16 +39,21 @@ export class UserController extends AdaptableController {
3839
async setEmailVerifyToken(user, req, storage = {}) {
3940
const shouldSendEmail =
4041
this.shouldVerifyEmails === true ||
41-
(typeof this.shouldVerifyEmails === 'function' &&
42-
(await Promise.resolve(this.shouldVerifyEmails(req))) === true);
42+
(typeof this.shouldVerifyEmails === "function" &&
43+
(await Promise.resolve(
44+
this.shouldVerifyEmails({
45+
user: Parse.Object.fromJSON({ className: "_User", ...user }),
46+
authContext: this.authContext
47+
})
48+
)) === true);
4349
if (!shouldSendEmail) {
4450
return false;
4551
}
4652
storage.sendVerificationEmail = true;
4753
user._email_verify_token = randomString(25);
4854
if (
4955
!storage.fieldsChangedByTrigger ||
50-
!storage.fieldsChangedByTrigger.includes('emailVerified')
56+
!storage.fieldsChangedByTrigger.includes("emailVerified")
5157
) {
5258
user.emailVerified = false;
5359
}
@@ -60,6 +66,7 @@ export class UserController extends AdaptableController {
6066
return true;
6167
}
6268

69+
6370
async verifyEmail(token) {
6471
if (!this.shouldVerifyEmails) {
6572
// Trying to verify email when not enabled

src/Controllers/index.js

+5-11
Original file line numberDiff line numberDiff line change
@@ -98,21 +98,15 @@ export function getFilesController(options: ParseServerOptions): FilesController
9898
});
9999
}
100100

101-
export function getUserController(options: ParseServerOptions) {
102-
const { appId, emailAdapter } = options;
101+
export function getUserController(options: ParseServerOptions, authContext = {}): UserController {
102+
const { appId, emailAdapter, verifyUserEmails } = options;
103103
const emailControllerAdapter = loadAdapter(emailAdapter);
104-
105104
return new UserController(emailControllerAdapter, appId, {
106-
verifyUserEmails: (user, request) => {
107-
108-
const createdWith = request?.sessionToken?.createdWith || {};
109-
const { action, authProvider } = createdWith;
110-
111-
return action === "signup" && authProvider === "password";
112-
},
113-
});
105+
verifyUserEmails,
106+
}, authContext);
114107
}
115108

109+
116110
export function getCacheController(options: ParseServerOptions): CacheController {
117111
const { appId, cacheAdapter, cacheTTL, cacheMaxSize } = options;
118112
const cacheControllerAdapter = loadAdapter(cacheAdapter, InMemoryCacheAdapter, {

0 commit comments

Comments
 (0)