Skip to content

Instantly share code, notes, and snippets.

@kdby-io
Created July 30, 2017 03:27
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 kdby-io/00e22514b0e27812cf781d6ee0caa9e0 to your computer and use it in GitHub Desktop.
Save kdby-io/00e22514b0e27812cf781d6ee0caa9e0 to your computer and use it in GitHub Desktop.
[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