Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
[blog] /2017-third-party-oauth-authentication-on-expressjs
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