gpt4 book ai didi

javascript - NodeJS Express Passport-facebook 无法加载页面回调

转载 作者:行者123 更新时间:2023-12-03 09:04:48 25 4
gpt4 key购买 nike

我有一个应用程序 Node js,使用 Express 通过 facebook 进行身份验证,但 url/auth/facebook/callback 未加载。

依赖版本: express :4.13.3 Passport 0.3.0Passport-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;

控制台上没有返回错误。

最佳答案

/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;

关于javascript - NodeJS Express Passport-facebook 无法加载页面回调,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32213239/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com