在我的 node.js Express 应用程序中,我使用 passport.js 集成了 Facebook 身份验证.我可以在弹出窗口中打开 Facebook 登录页面,然后进行登录。在此阶段,重定向发生在弹出窗口中。但我需要关闭弹出窗口并在父窗口中进行重定向(即父窗口将重定向)。我将在此处粘贴完整代码虽然我认为主要问题在模板文件 (.ejs) 中。
而且我在 SO 中的类似问题中找不到任何解决方案。
// Passport session setup.
passport.serializeUser(function(user, done) {
done(null, user);
});
passport.deserializeUser(function(obj, done) {
done(null, obj);
});
// Use the FacebookStrategy within Passport.
passport.use(new FacebookStrategy({
clientID: config.facebook_api_key,
clientSecret:config.facebook_api_secret ,
callbackURL: config.callback_url
},
function(accessToken, refreshToken, profile, done) {
console.log(" id = "+profile.id);
process.nextTick(function () {
//Check whether the User exists or not using profile.id
//Further DB code.
profile.user_id="00000444000555";
return done(null, profile);
});
}
));
app.set('views', __dirname + '/views');
app.set('view engine', 'ejs');
//app.set('view engine', 'jade');
app.use(cookieParser());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(session({ secret: 'keyboard cat', key: 'sid'}));
app.use(passport.initialize());
app.use(passport.session());
app.use(express.static(__dirname + '/public'));
//Router code
app.get('/', function(req, res){
res.render('index', { user: req.user });
});
app.get('/account', ensureAuthenticated, function(req, res){
console.log(req.user);
res.render('account', { user: req.user });
});
//Passport Router
app.get('/auth/facebook', passport.authenticate('facebook'));
app.get('/auth/facebook/callback',
passport.authenticate('facebook', {
successRedirect : '/',
failureRedirect: '/login'
}),
function(req, res) {
res.redirect('/');
});
app.get('/logout', function(req, res){
req.logout();
res.redirect('/');
});
function ensureAuthenticated(req, res, next) {
if (req.isAuthenticated()) { return next(); }
res.redirect('/login')
}
app.listen(8000);
模板文件是:
<script type="text/javascript">
if (window.location.hash && window.location.hash == '#_=_') {
if (window.history && history.pushState) {
window.history.pushState("", document.title, window.location.pathname);
} else {
// Prevent scrolling by storing the page's current scroll offset
var scroll = {
top: document.body.scrollTop,
left: document.body.scrollLeft
};
window.location.hash = '';
// Restore the scroll offset, should be flicker free
document.body.scrollTop = scroll.top;
document.body.scrollLeft = scroll.left;
}
}
</script>
<% if (!user) { %>
<div style="width:500px;height:180px;">
<h2 style="font-size:40px;">Welcome! Please log in.</h2>
<a href="javascript:void(0)" onclick=" window.open('/auth/facebook','',' scrollbars=yes,menubar=no,width=500, resizable=yes,toolbar=no,location=no,status=no')"><img src="fb-login.jpg" width="151" height="24"></a>
</div>
<% } else { %>
<script type="text/javascript">
window.parent.location.href ="/";
window.close();
</script>
<h2>Hello, <%= user.displayName %>.</h2>
<% } %>
最佳答案
我上面的评论可能没有完整的答案,所以这是我在我的网站上所做的完整解决方案。
在我的 Node 后端,我创建了一个路由,基本上在成功验证后路由到一个名为 after-auth.html 的特殊页面
app.get('/auth/twitter/callback',
auth.passport.authenticate('twitter', { failureRedirect: '/failedLogin.html' }),
function(req, res) {
res.redirect('/after-auth.html');
});
我的 after-auth.html 页面只有 javascript 可以将焦点放回父页面并像这样自行关闭
<script type="text/javascript">
if (window.opener) {
window.opener.focus();
if(window.opener.loginCallBack) {
window.opener.loginCallBack();
}
}
window.close();
</script>
您可能还注意到我调用了一个名为 loginCallBack 的函数。我将其用作 Hook ,以便父浏览器知道在成功验证后更新其模型/ View 。例如,在我的 loginCallBack 中,我检查用户是否登录并删除所有“登录”按钮并用用户的个人资料图像替换它们 例如
var loginCallBack = function() {
loginCntr.checkLogin();
}
关于node.js - passport.js ,在弹出窗口中验证后,关闭它并重定向父窗口,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28392393/