模块:
"express": "~3.0.0",
"jade": ">= 0.0.1",
"mongoose": ">= 3.6.2",
"connect-mongo": "0.3.2",
"nodemailer": ">= 0.3.20",
"socket.io": "0.9",
"cookie": "0.0.5",
"passport": "0.2.3",
"passport-facebook": "*",
"underscore": "*"
-- 后端 --
Expressjs 配置:
app.configure(function(){
app.set('port', process.env.PORT || 3000);
app.set('views', config.root + '/app/views');
app.set('view engine', 'jade');
app.use(express.favicon());
app.use(express.logger('dev'));
app.use(express.limit('1mb'));
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(express.cookieParser(''));
app.use(express.session());
app.use(express.static(path.join(config.root, 'public')));
// express/mongo session storage
app.use(express.session({
secret: '',
store: new mongoStore({
url: config.db,
collection : 'sessions'
})
}));
app.use(passport.initialize());
app.use(passport.session());
app.use(app.router);
});
在路由器中:
app.get('/login/facebook', passport.authenticate('facebook', { display: 'popup', scope: [ 'email', 'user_about_me'], failureRedirect: '/' }));
app.get('/login/facebook/callback', passport.authenticate('facebook', { failureRedirect: '/' }), user.callbackLogin);
在 user.callbackLogin 中:
exports.callbackLogin = function(req, res){
res.render('callback_login');
};
callback_login 视图有一个 js 脚本来关闭窗口登录弹出。
- 前端 -
Utils.popupCenter = function(url, width, height, name) {
var left = (screen.width/2)-(width/2);
var top = (screen.height/2)-(height/2);
return window.open(url, name, "menubar=no,toolbar=no,status=no,width="+width+",height="+height+",toolbar=no,left="+left+",top="+top);
};
Utils.popupCenter('login/facebook', 600, 400, 'Facebook Login');
我在使用passportjs 集成时遇到问题。弹出窗口有效,调用了 facebook 登录,当我单击“确定”时,facebook 返回数据并将护照保存在 mongodb 中。但是,在这一点之后,一切都没有了。Expressjs 阻止请求。当我尝试访问另一个 url nodejs 保持忙碌。花了一些时间后,我收到了“未收到数据”的消息。