我如何使用Passport.js返回到当前散列位置?

我使用Passport.js通过OAuth与Google进行身份validation(我正在使用passport-google-oauth策略)。 它工作正常,但我目前redirect到“/”,我想发送他们到“/”加上当前的哈希标签。 我可以在一个查询string参数中发送散列值,但我似乎无法将该值设置为我传递进行身份validation的对象的callbackURL属性。

有人可以提供一个例子或解释正确的方法来做到这一点? 我并不赞成使用查询string,它似乎是最直接的路线,但我打开使用会话variables或其他东西,如果这将是更容易或更好的做法。

谢谢。

您可以通过在会话中存储返回url来实现此效果。

// server var app, express; express = require('express'); app = express(); app.configure(function() { app.use(express.cookieSession({secret: 'shh'})); }); app.get('/auth/twitter', function(req, res, next) { // to return to '/#/returnHash', request this url: // http://example.com/auth/twitter?return_url=%2F%23%2FreturnHash // on the client you can get the hash value like this: // encodeURIComponent("/"+window.location.hash) req.session.return_url = req.query.return_url; next(); }, passport.authenticate('twitter')); app.get('/auth/twitter/callback', passport.authenticate('twitter', { failureRedirect: '/login' }), function(req, res) { var url = req.session.return_url; delete req.session.return_url; // redirects to /#/returnHash res.redirect(url); });