From 6aa38ea8ca544d87250116bb5c79e9ae7730a2c5 Mon Sep 17 00:00:00 2001 From: Florent Vilmart Date: Sun, 28 Feb 2016 00:15:59 -0500 Subject: [PATCH] Improves validation of email parameters in Configuration --- src/Config.js | 23 +++++++++++++++++++++++ src/Controllers/AdaptableController.js | 5 ++--- src/Controllers/UserController.js | 14 +++++--------- src/index.js | 22 ++++------------------ 4 files changed, 34 insertions(+), 30 deletions(-) diff --git a/src/Config.js b/src/Config.js index cfa53361..ae656011 100644 --- a/src/Config.js +++ b/src/Config.js @@ -39,6 +39,29 @@ export class Config { this.mount = mount; } + static validate(options) { + this.validateEmailConfiguration({verifyUserEmails: options.verifyUserEmails, + appName: options.appName, + publicServerURL: options.publicServerURL}) + } + + static validateEmailConfiguration({verifyUserEmails, appName, publicServerURL}) { + if (verifyUserEmails) { + if (typeof appName !== 'string') { + throw 'An app name is required when using email verification.'; + } + if (!process.env.TESTING && typeof publicServerURL !== 'string') { + if (process.env.NODE_ENV === 'production') { + throw 'A public server url is required when using email verification.'; + } else { + console.warn(""); + console.warn("You should set publicServerURL to serve the public pages"); + console.warn(""); + } + } + } + } + get linksServerURL() { return this.publicServerURL || this.serverURL; } diff --git a/src/Controllers/AdaptableController.js b/src/Controllers/AdaptableController.js index bfe0705c..902a6eb3 100644 --- a/src/Controllers/AdaptableController.js +++ b/src/Controllers/AdaptableController.js @@ -16,8 +16,8 @@ export class AdaptableController { constructor(adapter, appId, options) { this.options = options; - this.adapter = adapter; this.appId = appId; + this.adapter = adapter; } set adapter(adapter) { @@ -62,8 +62,7 @@ export class AdaptableController { }, {}); if (Object.keys(mismatches).length > 0) { - console.error(adapter, mismatches); - throw new Error("Adapter prototype don't match expected prototype"); + throw new Error("Adapter prototype don't match expected prototype", adapter, mismatches); } } } diff --git a/src/Controllers/UserController.js b/src/Controllers/UserController.js index b707e124..35da9a1f 100644 --- a/src/Controllers/UserController.js +++ b/src/Controllers/UserController.js @@ -162,16 +162,12 @@ export class UserController extends AdaptableController { const token = encodeURIComponent(user._perishable_token); const username = encodeURIComponent(user.username); let link = `${this.config.requestResetPasswordURL}?token=${token}&username=${username}` - - if (!user.username) { - console.log('No username...'); - } - + let options = { - appName: this.config.appName, - link: link, - user: inflate('_User', user), - }; + appName: this.config.appName, + link: link, + user: inflate('_User', user), + }; if (this.adapter.sendPasswordResetEmail) { this.adapter.sendPasswordResetEmail(options); diff --git a/src/index.js b/src/index.js index 1fa39aa7..3eebb483 100644 --- a/src/index.js +++ b/src/index.js @@ -11,6 +11,7 @@ var batch = require('./batch'), Parse = require('parse/node').Parse; import cache from './cache'; +import Config from './Config'; import ParsePushAdapter from './Adapters/Push/ParsePushAdapter'; //import passwordReset from './passwordReset'; @@ -72,17 +73,6 @@ addParseCloud(); // "javascriptKey": optional key from Parse dashboard // "push": optional key from configure push -let validateEmailConfiguration = (verifyUserEmails, appName, emailAdapter) => { - if (verifyUserEmails) { - if (typeof appName !== 'string') { - throw 'An app name is required when using email verification.'; - } - if (!emailAdapter) { - throw 'User email verification was enabled, but no email adapter was provided'; - } - } -} - function ParseServer({ appId = requiredParameter('You must provide an appId!'), masterKey = requiredParameter('You must provide a masterKey!'), @@ -127,13 +117,7 @@ function ParseServer({ if (databaseURI) { DatabaseAdapter.setAppDatabaseURI(appId, databaseURI); } - - if (verifyUserEmails && !publicServerURL && !process.env.TESTING) { - console.warn(""); - console.warn("You should set publicServerURL to serve the public pages"); - console.warn(""); - } - + if (cloud) { addParseCloud(); if (typeof cloud === 'function') { @@ -186,6 +170,8 @@ function ParseServer({ if (process.env.FACEBOOK_APP_ID) { cache.apps.get(appId)['facebookAppIds'].push(process.env.FACEBOOK_APP_ID); } + + Config.validate(cache.apps.get(appId)); // This app serves the Parse API directly. // It's the equivalent of https://api.parse.com/1 in the hosted Parse API.