diff --git a/client/app/HackdashApp.js b/client/app/HackdashApp.js index 9874f577..be11df46 100644 --- a/client/app/HackdashApp.js +++ b/client/app/HackdashApp.js @@ -24,7 +24,7 @@ module.exports = function(){ var providers = window.hackdash.providers; app.modals.show(new LoginView({ - model: new Backbone.Model({ providers: providers.split(',') }) + model: new Backbone.Model({ providers: providers }) })); }; diff --git a/client/app/helpers/handlebars.js b/client/app/helpers/handlebars.js index 065b0a1a..4d3ee306 100644 --- a/client/app/helpers/handlebars.js +++ b/client/app/helpers/handlebars.js @@ -14,10 +14,6 @@ Handlebars.registerHelper('embedCode', function() { }); }); -Handlebars.registerHelper('firstUpper', function(text) { - return text.charAt(0).toUpperCase() + text.slice(1); -}); - Handlebars.registerHelper('firstLetter', function(text) { if (text){ return text.charAt(0); diff --git a/client/app/views/Home/index.js b/client/app/views/Home/index.js index b834c1b7..eaaa6633 100644 --- a/client/app/views/Home/index.js +++ b/client/app/views/Home/index.js @@ -170,7 +170,7 @@ module.exports = Backbone.Marionette.LayoutView.extend({ var app = window.hackdash.app; app.modals.show(new LoginView({ - model: new Backbone.Model({ providers: providers.split(',') }) + model: new Backbone.Model({ providers: providers }) })); return false; diff --git a/client/app/views/templates/login.hbs b/client/app/views/templates/login.hbs index bf352c20..f839fb96 100644 --- a/client/app/views/templates/login.hbs +++ b/client/app/views/templates/login.hbs @@ -10,8 +10,8 @@ {{#each providers}}
- - {{__ "Access with"}} {{firstUpper .}} + + {{__ "Access with"}} {{name}}
diff --git a/keys.json.sample b/keys.json.sample index b8ec781f..633269dc 100644 --- a/keys.json.sample +++ b/keys.json.sample @@ -15,9 +15,22 @@ , "callbackURL": "http://local.host:3000/auth/facebook/callback" } , "github": { - "clientID": "" + "name": "GitHub" + , "clientID": "" , "clientSecret": "" , "callbackURL": "http://local.host:3000/auth/github/callback" } - + , "google": { + , "clientID": "" + , "clientSecret": "" + , "callbackURL": "http://local.host:3000/auth/google/callback" + } + , "auth0": { + "name": "Others" + "icon": "paper-plane" + , "domain": "" + , "clientID": "" + , "clientSecret": "" + , "callbackURL": "http://local.host:3000/auth/auth0/callback" + } } diff --git a/lib/auth/index.js b/lib/auth/index.js index bee9d7f8..ef9c10b9 100644 --- a/lib/auth/index.js +++ b/lib/auth/index.js @@ -71,15 +71,20 @@ const setPicture = (user, profile) => { */ const generateStrategy = provider => { - // Geneate routes - app.get(`/auth/${provider}`, saveRedirect, passport.authenticate(provider)); - app.get(`/auth/${provider}/callback`, passport.authenticate(provider, { failureRedirect: '/' }), redirectSubdomain); - // Require provider own module // TODO: Figure out how to use ES2015 syntax instead of requires - const Strategy = require(`passport-${provider}`).Strategy; + const moduleName = provider === 'google' ? 'passport-google-oauth20' : `passport-${provider}`; + const Strategy = require(moduleName).Strategy; + + // Generate routes + const authenticateOptions = provider === 'google' ? { scope: ['profile'] } : {}; + app.get(`/auth/${provider}`, saveRedirect, passport.authenticate(provider, authenticateOptions)); + app.get(`/auth/${provider}/callback`, passport.authenticate(provider, { failureRedirect: '/' }), redirectSubdomain); - passport.use(new Strategy(keys[provider], async (token, tokenSecret, profile, done) => { + const strategyOptions = Object.assign({}, keys[provider]); + delete strategyOptions.name; + delete strategyOptions.icon; + passport.use(new Strategy(strategyOptions, async (token, tokenSecret, profile, done) => { let user = await User.findOne({provider_id: profile.id, provider: provider}).exec(); if(!user) { user = new User(); diff --git a/lib/models/user.js b/lib/models/user.js index 4967f1d0..417f4764 100644 --- a/lib/models/user.js +++ b/lib/models/user.js @@ -5,7 +5,7 @@ export default { 'provider': { type: String, required: true }, - 'provider_id': { type: Number, required: true }, + 'provider_id': { type: String, required: true }, 'username': { type: String, required: true }, 'name': { type: String, required: true }, 'email': { type: String, validate: /.+@.+\..+/ }, // TODO: Improve this validation diff --git a/lib/routes/helpers.js b/lib/routes/helpers.js index 2dcde987..3d517c8e 100644 --- a/lib/routes/helpers.js +++ b/lib/routes/helpers.js @@ -50,6 +50,10 @@ export const setViewVar = (key, value) => ((req, res, next) => { */ export const loadProviders = (req, res, next) => { - res.locals.providers = Object.keys(providers); + res.locals.providers = Object.entries(providers).map(([key, options]) => ({ + key: key, + name: options.name || key.charAt(0).toUpperCase() + key.slice(1), + icon: `fa-${options.icon || key}`, + })); next(); }; diff --git a/package.json b/package.json index a390536e..d91808f9 100644 --- a/package.json +++ b/package.json @@ -29,8 +29,10 @@ "multer": "0.1.8", "nodemailer": "0.4.4", "passport": "0.2.2", + "passport-auth0": "^0.5.2", "passport-facebook": "2.0.0", "passport-github": "0.1.5", + "passport-google-oauth20": "^1.0.0", "passport-http": "^0.3.0", "passport-meetup": "0.1.2", "passport-twitter": "1.0.3", diff --git a/views/layout.jade b/views/layout.jade index aa455af0..b545d5df 100644 --- a/views/layout.jade +++ b/views/layout.jade @@ -56,7 +56,7 @@ html baseURL: "!{host}", disqus_shortname: "!{disqus_shortname}", statuses: "!{statuses}", - providers: "!{providers}", + providers: !{JSON.stringify(providers)}, fbAppId: "!{fbAppId}" };