我正在使用 Passport.js 通过 OAuth 向 Google 进行身份验证(我正在使用 passport-google-oauth 策略)。它工作正常,但我目前正在将用户重定向到“/”,我想将他们发送到“/”加上当前的哈希标签。我可以在查询字符串参数中发送哈希值,但我似乎无法将该值设置为我要传递给身份验证的对象的 callbackURL 属性。
有人可以提供有关执行此操作的正确方法的示例或解释吗?我不喜欢使用查询字符串,它似乎是最直接的途径,但我愿意使用 session 变量或其他东西,如果这样做更容易或更好的话。
谢谢。
最佳答案
您可以通过在 session 中存储返回 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);
});
关于node.js - 如何使用 Passport.js 返回到当前哈希位置?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12293554/