Skip to content

Instantly share code, notes, and snippets.

@jslnriot
Last active September 7, 2017 18:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jslnriot/0d6bd5989d9b2eb540135abdc7bfe6dd to your computer and use it in GitHub Desktop.
Save jslnriot/0d6bd5989d9b2eb540135abdc7bfe6dd to your computer and use it in GitHub Desktop.
const express = require('express');
// Passport
const passport = require('passport');
const GoogleStrategy = require('passport-google-oauth20').Strategy;
const keys = require('./config/keys');
const app = express();
// Creates a new instance of the GoogleStrategy
// In the constructor tell it how to handle this
// Client ID and Client Secret
// callbackURL - string is the route that the user
// is sent to after they grant permission to our app
passport.use(
new GoogleStrategy(
{
clientID: keys.googleClientID,
clientSecret: keys.googleClientSecret,
callbackURL: '/auth/google/callback',
proxy: true
},
(accessToken, refreshToken, profile, done) => {
// Take identifying user information and save to db
console.log('access token', accessToken);
console.log('refresh token', refreshToken);
console.log('profile', profile);
}
)
);
// Route handler for passport flow
// Internally GoogleStrategy has a string of 'google' we can use as the first argument
// of passport.authenticate
app.get(
'/auth/google',
passport.authenticate('google', {
scope: ['profile', 'email']
})
);
// When the user hits this route they have the auth code
app.get(
'/auth/google/callback',
passport.authenticate('google')
);
app.listen(3000);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment