diff --git a/spec/EmailVerificationToken.spec.js b/spec/EmailVerification.spec.js similarity index 96% rename from spec/EmailVerificationToken.spec.js rename to spec/EmailVerification.spec.js index ec3d7b8ec0..24123f232f 100644 --- a/spec/EmailVerificationToken.spec.js +++ b/spec/EmailVerification.spec.js @@ -3,6 +3,7 @@ const Auth = require('../lib/Auth'); const Config = require('../lib/Config'); const request = require('../lib/request'); +const { getAuthForSessionToken } = require('../lib/Auth'); const MockEmailAdapterWithOptions = require('./support/MockEmailAdapterWithOptions'); describe('Email Verification Token Expiration: ', () => { @@ -1351,3 +1352,62 @@ describe('Email Verification Token Expiration: ', () => { }); }); }); + +describe('Auth Context', () => { + let user; + let config; + + beforeEach(async (done) => { + await reconfigureServer({ + verifyUserEmails: jasmine.createSpy('verifyUserEmails'), + }); + + user = new Parse.User(); + await user.signUp({ + username: 'user', + password: 'pass', + email: 'test@example.com', + }); + + config = Config.get('test'); + }); + + 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', + }); + }); + + it('should call verifyUserEmails with correct auth context on login', async (done) => { + await Parse.User.logIn('user', 'pass'); + + expect(config.verifyUserEmails).toHaveBeenCalledWith({ + action: 'login', + authProvider: 'password', + }); + }); + + 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', + }); + }); +}); + diff --git a/src/Controllers/UserController.js b/src/Controllers/UserController.js index 296b7f6868..70a8ccd6e2 100644 --- a/src/Controllers/UserController.js +++ b/src/Controllers/UserController.js @@ -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() { @@ -38,8 +39,11 @@ 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(req, this.authContext) + )) === true); + if (!shouldSendEmail) { return false; } diff --git a/src/Controllers/index.js b/src/Controllers/index.js index abf0950640..ca3ec71369 100644 --- a/src/Controllers/index.js +++ b/src/Controllers/index.js @@ -98,12 +98,12 @@ 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 {