问题描述
几个小时后,我独自尝试解决此问题,我来到了SO社区,寻求一些帮助.
After hours trying to solve this by myself I reach to SO community looking for some light.
我正在使用护照进行用户身份验证.根据文档,它已经在我的主要express.js文件中初始化了:
I'm using passport for user authentication. It's already initialized in my main express.js file as per the docs:
app.use(passport.initialize());
我得到了一个index.js文件,该文件以这种方式处理 facebook-passport
:Index.js
I got a index.js file which handles facebook-passport
in this manner:Index.js
'use strict';
import express from 'express';
import passport from 'passport';
import auth from '../auth.service';
let router = express.Router();
//this function is defined in the auth.service import but copied it here in case it's needed (the `signToken` is also defined in the service)
function setTokenCookie(req, res) {
if (!req.user) return res.json(404, { message: 'Something went wrong, please try again.'});
var token = signToken(req.user._id, req.user.role);
res.cookie('token', JSON.stringify(token));
res.redirect('/');
}
router
.get('/', passport.authenticate('facebook', {
scope: ['email', 'user_about_me'],
failureRedirect: '/login'
}))
.get('/callback', passport.authenticate('facebook', {
successRedirect: '/',
failureRedirect: '/login'
}), setTokenCookie);
module.exports = router;
以这种方式在index.js中导入的password.js:
And the passport.js that's being imported in the index.js in this manner:
passport.js
passport.js
import passport from 'passport';
import {
Strategy as FacebookStrategy
}
from 'passport-facebook';
exports.setup = function(User, config) {
passport.use(new FacebookStrategy({
clientID: config.facebook.clientID,
clientSecret: config.facebook.clientSecret,
callbackURL: config.facebook.callbackURL
},
function(accessToken, refreshToken, profile, done) {
User.findOne({
'facebook.id': profile.id
}, (findErr, user) => {
if (findErr) {
return done(findErr);
}
if (!user) {
let userToSave = new User({
name: profile.displayName,
email: profile.emails[0].value,
role: 'user',
username: profile.username,
provider: 'facebook',
facebook: profile._json
});
userToSave.save((saveErr) => {
if (saveErr) done(saveErr);
return done(null, user);
});
} else {
return done(null, user);
}
});
}
));
};
这是当前发生的情况:
- 提示登录Facebook
- 在Facebook中成功通过身份验证后,回调(/auth/facebook/callback)已通过预期的用户信息和令牌到达.
- 用户保存在具有预期字段的数据库中
哪里很奇怪:
- 保存用户后,
done(null,user)
不执行任何操作.该应用程序挂在回调上,并且客户端一直在等待响应. - 永远不会调用中间件setTokenCookie,因此问题肯定在上一步中.
- After saving the User, the
done(null,user)
does nothing. The app hangs on the callback and client keeps waiting for response. - The middleware setTokenCookie never gets called so the problem is definitely in the previous step.
我尝试过的事情:
- 在process.tick中将
passport.js
的整个设置功能包装起来(发现有人使用了它,但未能解决问题) - 在
User.findOne({...}).exec().then(user => {...})
中使用带有承诺的猫鼬
- Wrapping the whole setup function from
passport.js
in a process.tick (found some people use it but didn't resolve the issue) - Using Mongoose with promise as in
User.findOne({...}).exec().then(user => {...})
如果您需要其他信息,请随时询问.任何帮助,我们都感激不尽.
If you need additional information please don't hesitate to ask. Any help is really appreciated.
谢谢!
推荐答案
如果您像我一样是护照/快递的菜鸟,这可能会有所帮助:
If you're a noob to passport/express like me, this may help:
检查您是否在护照逻辑中调用了done():
check if you invoke done() in your passport logic:
passport.use(new twitchStrategy({
clientID: config.twitchID,
clientSecret: config.twitchSecret,
/*to be updated*/
callbackURL: config.callbackURL,
scope: "user_read"
},
function(accessToken, refreshToken, profile, done) {
console.log(profile);
***return done();***
}
));
这篇关于Passport身份验证回调挂起的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!