Facebook Authentication with Passport and ExpressJS - Why is verify callback not called?
Asked Answered
W

1

6

I am Passport to authenticate users in an ExpressJS application. I am attempting to place all Facebook routes in their own module because I intend to support other OAuth providers. In my start script I have defined the required FB endpoints:

var express = require('express');
var routes = require('./routes');
var user = require('./routes/user');
var facebook = require('./routes/facebook');
var login = require('./routes/login');
var http = require('http');
var path = require('path');

var app = express();

/* Configuration stuff here*/

// development only
if ('development' == app.get('env')) {
  app.use(express.errorHandler());
}

app.get('/login', login.index);
app.get('/auth/facebook', facebook.fb_auth);
app.get('/auth/facebook/callback', facebook.fb_callback);

As you can see I am requiring my 'facebook' module which contains the actual routes and Facebook verify callback. It looks like this:

var passport = require('passport')
, FacebookStrategy = require('passport-facebook').Strategy
, User = require('../services/user');

passport.use(new FacebookStrategy({
  clientID: 'myclientid',
  clientSecret: 'mysecretkey',
  callbackURL: "http://localhost:3000/auth/facebook/callback" //localhost:3000 domain is registered domain in fb
},
function(accessToken, refreshToken, profile, done) {
   console.log('verify')
  User.findOrCreate(profile, function(err, user){
      if(err) return done(err);
      done(null, user);
  });
}
));

// Redirect the user to Facebook for authentication.  When complete,
// Facebook will redirect the user back to the application at
//     /auth/facebook/callback
exports.fb_auth = function(req, res){
  passport.authenticate('facebook')
};
// Facebook will redirect the user to this URL after approval.  Finish the
// authentication process by attempting to obtain an access token.  If
// access was granted, the user will be logged in.  Otherwise,
// authentication has failed.
exports.fb_callback = function(req, res){
  console.log('callback')
  passport.authenticate('facebook', { successRedirect: '/',
    failureRedirect: '/login' });
};

I can see (logging to stdout) that fb_auth is called but the verify callback function defined above never is. Am I overlooking something? Is there an error occurring somewhere I can capture?

Thanks!

Wehner answered 22/10, 2013 at 2:19 Comment(0)
W
8

I found the answer here: Using PassportJS with Connect for NodeJS to authenticate Facebook Users

You need to explicitly call the 'authenticate' function and provide it with req, res, and next.

  exports.fb_auth = function(req, res, next){
  passport.authenticate('facebook')(req, res, next);
  return;
};

exports.fb_callback = function(req, res, next){
  passport.authenticate('facebook', { successRedirect: '/',
    failureRedirect: '/login' })(req, res, next);
};
Wehner answered 22/10, 2013 at 2:27 Comment(2)
passport.authenticate is middleware. You could simplify this to export it directly: exports.fb_auth = passport.authenticate('facebook'); and similarly for the callback.Toothy
@JaredHanson Good Call. Thanks.Wehner

© 2022 - 2024 — McMap. All rights reserved.