使用NodeJS Express Passport-facebook加载页面回调

时间:2015-08-25 20:14:16

标签: javascript node.js facebook express passport.js

我有一个应用程序节点js使用express通过facebook进行身份验证但是url / auth / facebook / callback没有加载。

依赖版本: 快递:4.13.3 护照0.3.0 Passport-facebook 2.0.0

app.js

var app = express();


app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');


app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));


passport.use(new FacebookStrategy({
  clientID: '806175282757975',
  clientSecret: '*************',
  callbackURL: "http://localhost:3000/auth/facebook/callback"
},
function(accessToken, refreshToken, profile, done) {

  process.nextTick(function () {

        return done(null, profile);
      });
    }
  ));

app.use(function(req,res,next){
    req.passport = passport;
    next();
});

app.use('/', routes);
app.use('/users', users);

module.exports = app;

路由/ index.js

var express = require('express');
var router = express.Router();

/* GET home page. */
router.get('/', function(req, res, next) {
  res.render('index', { title: 'Express' });
});

router.get('/auth/facebook', function(req, res, next){
    req.passport.authenticate('facebook')(req, res, next);
});

router.get('/auth/facebook/callback', function(req, res, next){
    req.passport.authenticate('facebook', { successRedirect: '/',
                                  failureRedirect: '/login' });
});

module.exports = router;

控制台上没有返回错误。

1 个答案:

答案 0 :(得分:1)

/auth/facebook/callback路由中的routes / index.js中,您忘记了使用req,res,next参数调用authenticate方法。这就是为什么你的应用程序被卡住了('下一个'回调从未被调用过)。

var express = require('express');
var router = express.Router();

/* GET home page. */
router.get('/', function(req, res, next) {
  res.render('index', { title: 'Express' });
});

router.get('/auth/facebook', function(req, res, next){
    req.passport.authenticate('facebook')(req, res, next);
});

router.get('/auth/facebook/callback', function(req, res, next){
    req.passport.authenticate('facebook', {
        successRedirect: '/',
        failureRedirect: '/login' }
    )(req, res, next); // missing function call
});

module.exports = router;