Skip to content
Open
Show file tree
Hide file tree
Changes from 7 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
66 changes: 66 additions & 0 deletions spec/verifyUserEmails.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
'use strict';

const { getAuthForSessionToken } = require('../lib/Auth');
const Config = require('../lib/Config');

describe('verifyUserEmails with Auth Context', () => {
let user;
let config;

beforeEach(async (done) => {
await reconfigureServer({
verifyUserEmails: jasmine.createSpy('verifyUserEmails'),
});

user = new Parse.User();
await user.signUp({
username: 'testuser',
password: 'securepassword',
email: 'test@example.com',
});

config = Config.get('test');
done();
});

it('should call verifyUserEmails with correct auth context on signup', async (done) => {
const sessionToken = user.getSessionToken();
expect(sessionToken).toBeDefined();

await getAuthForSessionToken({
sessionToken,
config,
});

expect(config.verifyUserEmails).toHaveBeenCalledWith({
action: 'signup',
authProvider: 'password',
});
done();
});

it('should call verifyUserEmails with correct auth context on login', async (done) => {
await Parse.User.logIn('testuser', 'securepassword');

expect(config.verifyUserEmails).toHaveBeenCalledWith({
action: 'login',
authProvider: 'password',
});
done();
});

it('should call verifyUserEmails with correct provider for social login', async (done) => {
const socialAuthData = {
id: '1234567890',
access_token: 'mockAccessToken',
};

await Parse.User.logInWith('facebook', { authData: socialAuthData });

expect(config.verifyUserEmails).toHaveBeenCalledWith({
action: 'login',
authProvider: 'facebook',
});
done();
});
});
12 changes: 9 additions & 3 deletions src/Controllers/UserController.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@ var RestQuery = require('../RestQuery');
var Auth = require('../Auth');

export class UserController extends AdaptableController {
constructor(adapter, appId, options = {}) {
constructor(adapter, appId, options = {}, authContext = {}) {
super(adapter, appId, options);
this.authContext = authContext;
}

get config() {
Expand All @@ -38,8 +39,13 @@ export class UserController extends AdaptableController {
async setEmailVerifyToken(user, req, storage = {}) {
const shouldSendEmail =
this.shouldVerifyEmails === true ||
(typeof this.shouldVerifyEmails === 'function' &&
(await Promise.resolve(this.shouldVerifyEmails(req))) === true);
(typeof this.shouldVerifyEmails === "function" &&
(await Promise.resolve(
this.shouldVerifyEmails({
user: Parse.Object.fromJSON({ className: "_User", ...user }),
authContext: this.authContext
})
)) === true);
if (!shouldSendEmail) {
return false;
}
Expand Down
5 changes: 3 additions & 2 deletions src/Controllers/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,14 +98,15 @@ export function getFilesController(options: ParseServerOptions): FilesController
});
}

export function getUserController(options: ParseServerOptions): UserController {
export function getUserController(options: ParseServerOptions, authContext = {}): UserController {
const { appId, emailAdapter, verifyUserEmails } = options;
const emailControllerAdapter = loadAdapter(emailAdapter);
return new UserController(emailControllerAdapter, appId, {
verifyUserEmails,
});
}, authContext);
}


export function getCacheController(options: ParseServerOptions): CacheController {
const { appId, cacheAdapter, cacheTTL, cacheMaxSize } = options;
const cacheControllerAdapter = loadAdapter(cacheAdapter, InMemoryCacheAdapter, {
Expand Down