Node.js Passport integration edit

Facebook authentication

The passport-facebook module is used to implement a Facebook authentication. In this example, if the user does not exist on sign-in, he is created.

Implementing strategy :

const passport = require('passport');
const FacebookStrategy = require('passport-facebook').Strategy;

// Strategy is named 'facebook' by default
passport.use({
    clientID: 'yourclientid',
    clientSecret: 'yourclientsecret',
    callbackURL: '/auth/facebook/callback'
},
// Facebook will send a token and user's profile
function(token, refreshToken, profile, next) {
    // Check in database if user is already registered
    findUserByFacebookId(profile.id, function(user) {
        // If user exists, returns his data to callback
        if (user) return next(null, user);
        // Else, we create the user
        else {
            let newUser = createUserFromFacebook(profile, token);

            newUser.save(function() {
                // Pass the user to the callback
                return next(null, newUser);
            });
        }
    });
});

Creating routes :

// ...
app.use(passport.initialize());
app.use(passport.session());

// Authentication route
app.get('/auth/facebook', passport.authenticate('facebook', {
    // Ask Facebook for more permissions
    scope : 'email'
}));

// Called after Facebook has authenticated the user
app.get('/auth/facebook/callback',
    passport.authenticate('facebook', {
        successRedirect : '/me',
        failureRedirect : '/'
}));
//...

app.listen(3000);

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:


Table Of Contents
2 npm
42 cli
60 Hack
65 ES6
71 Passport integration
92 CORS
106 N-API
  ↑ ↓ to navigate     ↵ to select     Esc to close