npm install passport-google-oauth20 code example

Example: passport google oauth20

//To install
npm install passport-google-oauth20  

//To Acquire it
var GoogleStrategy = require('passport-google-oauth20').Strategy;

//To use findOrCreate install the package <findorcreate-mongoose>
passport.use(new GoogleStrategy({
    clientID: GOOGLE_CLIENT_ID,
    clientSecret: GOOGLE_CLIENT_SECRET,
    callbackURL: "http://www.example.com/auth/google/callback"
  },
  function(accessToken, refreshToken, profile, cb) {
    User.findOrCreate({ googleId: profile.id }, function (err, user) {
      return cb(err, user);
    });
  }
));

//GET Route
app.get('/auth/google',
  passport.authenticate('google', { scope: ['profile'] })
);

//Callback GET Route from Google
app.get('/auth/google/callback', 
  passport.authenticate('google', { failureRedirect: '/login' }),
  function(req, res) {
    // Successful authentication, redirect home.
    res.redirect('/');
});

Tags:

Go Example