Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save ankitadhandha/1dc96bcfb6c286808de56bc2951411cf to your computer and use it in GitHub Desktop.
Save ankitadhandha/1dc96bcfb6c286808de56bc2951411cf to your computer and use it in GitHub Desktop.
oauth.js:
const GoogleStrategy = require('passport-google-oauth').OAuth2Strategy;
module.exports = (passport) => {
passport.serializeUser((user, done) => {
done(null, user);
});
passport.deserializeUser((user, done) => {
done(null, user);
});
passport.use(new GoogleStrategy({
clientID: '382676186715-498u44993pmuuh735h2mg62olrq5lvlv.apps.googleusercontent.com',
clientSecret: 'X-kksGokyz_yVmi91--3_n-I',
callbackURL: 'https://example.com/amp-access/amp-access/login/gmail_login/callback',
},
(token, refreshToken, profile, done) => {
return done(null, {
profile: profile,
token: token
});
}));
};
login.js
//this is to login using gmail
router.get('/gmail_login',
//middlware1
(req, res, next) => {
console.log('readerId from index page: ' + req.query.rid);
//stroing readerId to use it later
RID = req.query.rid;
//storing return url to come back later
RURL = req.query.return;
//is to go to next middleware2
next();
},
//middleware2
//this is to authenticate gmail login
passport.authenticate('google', {
scope: [
'https://www.googleapis.com/auth/userinfo.profile',
'https://www.googleapis.com/auth/userinfo.email'
]
})
);
router.get('/gmail_login/callback',
//middleware1
//if user enters wrong password, prompt him again
passport.authenticate('google', {
failureRedirect: '/'
}),
//middleware2
(req, res) => {
//get user profile and save and secure it as per your business requirement
}
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment