From 95a2bdd95062e5f95ae5e1e41e83c76f5c7155be Mon Sep 17 00:00:00 2001 From: Julian Lam Date: Wed, 19 Mar 2014 08:42:01 -0400 Subject: [PATCH] error handling and proper handling of missing settings on first install --- library.js | 30 +++++++++++++++++------------- 1 file changed, 17 insertions(+), 13 deletions(-) diff --git a/library.js b/library.js index 1286ada..82e557e 100644 --- a/library.js +++ b/library.js @@ -28,18 +28,16 @@ app.get('/admin/plugins/sso-oauth', middleware.admin.buildHeader, render); app.get('/api/admin/plugins/sso-oauth', render); - - meta.settings.get('sso-oauth', function(err, settings) { - if (settings['oauth:type'] === '2') { - passportOAuth = require('passport-oauth').OAuth2Strategy; - } else if (settings['oauth:type'] === '1') { - passportOAuth = require('passport-oauth').OAuthStrategy; - } - }); }; OAuth.getStrategy = function(strategies, callback) { meta.settings.get('sso-oauth', function(err, settings) { + if (err) { + winston.error('[plugins/sso-oauth] Could not retrieve OAuth settings: ' + err.message); + } else if (!settings) { + settings = {}; + } + var oAuthKeys = ['oauth:reqTokenUrl', 'oauth:accessTokenUrl', 'oauth:authUrl', 'oauth:key', 'oauth:secret'], oAuth2Keys = ['oauth2:authUrl', 'oauth2:tokenUrl', 'oauth2:id', 'oauth2:secret'], configOk = oAuthKeys.every(function(key) { @@ -49,6 +47,12 @@ }), opts; + if (settings['oauth:type'] === '2') { + passportOAuth = require('passport-oauth').OAuth2Strategy; + } else if (settings['oauth:type'] === '1') { + passportOAuth = require('passport-oauth').OAuthStrategy; + } + if (passportOAuth && configOk) { if (settings['oauth:type'] === '1') { // OAuth options @@ -102,16 +106,16 @@ // Alter this section to include whatever data is necessary // NodeBB requires the following: id, displayName, emails, e.g.: - // profile.id = json.id; - // profile.displayName = json.name; - // profile.emails = [{ value: json.email }]; + profile.id = json.id; + profile.displayName = json.name; + profile.emails = [{ value: json.email }]; // Find out what is available by uncommenting this line: // console.log(json); // Delete or comment out the next TWO (2) lines when you are ready to proceed - console.log('===\nAt this point, you\'ll need to customise the above section to id, displayName, and emails into the "profile" object.\n==='); - return done(new Error('Congrats! So far so good -- please see server log for details')); + // console.log('===\nAt this point, you\'ll need to customise the above section to id, displayName, and emails into the "profile" object.\n==='); + // return done(new Error('Congrats! So far so good -- please see server log for details')); done(null, profile); } catch(e) {