From f2d11431a040cf14665d77454944dbe7c3267c9e Mon Sep 17 00:00:00 2001 From: Emmanuel Raviart Date: Sat, 5 Nov 2016 00:40:59 +0100 Subject: [PATCH] Add Google OAuth 2.0 Passport strategy. --- keys.json.sample | 6 +++++- lib/auth/index.js | 6 ++++-- package.json | 1 + 3 files changed, 10 insertions(+), 3 deletions(-) diff --git a/keys.json.sample b/keys.json.sample index b8ec781f..01f9bcab 100644 --- a/keys.json.sample +++ b/keys.json.sample @@ -19,5 +19,9 @@ , "clientSecret": "" , "callbackURL": "http://local.host:3000/auth/github/callback" } - + , "google": { + "clientID": "" + , "clientSecret": "" + , "callbackURL": "http://local.host:3000/auth/google/callback" + } } diff --git a/lib/auth/index.js b/lib/auth/index.js index bee9d7f8..2068870e 100644 --- a/lib/auth/index.js +++ b/lib/auth/index.js @@ -71,13 +71,15 @@ const setPicture = (user, profile) => { */ const generateStrategy = provider => { - // Geneate routes + // Generate routes + const options = provider === 'google' ? { scope: ['profile'] } : {}; 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 strategyName = provider === 'google' ? 'passport-google-oauth20' : `passport-${provider}`; + const Strategy = require(strategyName).Strategy; passport.use(new Strategy(keys[provider], async (token, tokenSecret, profile, done) => { let user = await User.findOne({provider_id: profile.id, provider: provider}).exec(); diff --git a/package.json b/package.json index a390536e..4c3531bd 100644 --- a/package.json +++ b/package.json @@ -31,6 +31,7 @@ "passport": "0.2.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",