node.js - 如何使用 Passport.js 返回到当前哈希位置?

标签 node.js authentication hash passport.js callbackurl

我正在使用 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/

相关文章:

security - 如何验证 JavaScript 库?

node.js - 返回日期范围内的数组文档

javascript - Angular 2 + Zone.js + Common js 模块 : IF statement anomaly, 代码即使在错误情况下也会执行

node.js - Redis 连接检查

c# - 在 Java 和 C# 中计算 SHA-1 哈希

security - 什么时候使用散列散列函数安全?

c - 实现二次探测和链接 - 搜索字典

node.js - 使用 node.js 应用程序和套接字 io 进行水平缩放

authentication - 将 FaunaDB 客户端 secret 存储在 cookie 中安全吗?

Laravel Passport 密码授予 token : own mobile app