Created
July 30, 2017 03:27
-
-
Save kdby-io/00e22514b0e27812cf781d6ee0caa9e0 to your computer and use it in GitHub Desktop.
[blog] /2017-third-party-oauth-authentication-on-expressjs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const express = require('express'); | |
const passport = require('passport'); | |
const FacebookStrategy = require('passport-facebook'); | |
/* | |
* Server | |
*/ | |
const app = express(); | |
/* | |
* Setup for JWT authentication | |
*/ | |
passport.use(new FacebookStrategy({ | |
clientID: '448621075525254', | |
clientSecret: '9d82d0042fca547d41b07e5970aba706', | |
callbackURL: '/login/facebook/callback', | |
profileFields: ['id', 'email', 'name'], | |
}, (accessToken, refreshToken, profile, done) => { | |
const { id, email } = profile._json; | |
user = { | |
provider: 'facebook', | |
providerID: id, | |
email: email, | |
} | |
return done(null, user); | |
})); | |
app.use(passport.initialize()); | |
/* | |
* Routes | |
*/ | |
app.get('/login/facebook', | |
passport.authenticate('facebook', { | |
scope: ['public_profile', 'email'], | |
}) | |
); | |
app.get('/login/facebook/callback', | |
passport.authenticate('facebook', { | |
session: false, | |
failureRedirect: '/login/facebook', | |
}), (req, res) => { | |
return res.json(req.user); | |
} | |
); | |
const port = 3000; | |
app.listen(port, () => { | |
console.log(('App is running at http://localhost:%d'), port); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment