Passport.js + Express.js验证后将用户转发到原始目的地 [英] Passport.js + Express.js forward user to original destination after authenticating

查看:133
本文介绍了Passport.js + Express.js验证后将用户转发到原始目的地的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个应用程序是服务器上的Node + Express + Passport和客户端上的jQuery + Backbone.js。客户端使用URL中的哈希标记,但对于某些功能,用户登录是非常重要的。



我希望该应用可以通过网址,例如 http://mydomain.com/app#cone/waffle/flavor/mint/toppings/sprinkles ,以便:




  • 如果用户已经登录,则如果用户尚未登录,他们直接访问请求的URL,而不会出现麻烦

  • ,他们转到 / login 然后转到所请求的URL



b Node.js护照的Google策略的自定义returnUrl ,I如果他们已经登录,他们直接转到URL,哈希标签和所有 $ b $($)


    $ b
  • 如果没有登录,则将它们登录到登录页面,然后将其发送到所请求的URL,但...



在登录后,重定向页面上的哈希参数似乎从原始URL中删除。



有没有办法保留哈希重定向时的参数t下摆到原来的目的地?



从这篇文章,从请求url获取哈希参数我得到的想法是哈希标签在服务器上不可用,这是使用哈希标签的整体。



所以我怀疑是不可能的。也许可以缓存本地的params,并在重定向上检索它们,比如说 [原始URL减去hastags] +#use-cached-params

解决方案

哈希参数仅限浏览器,不会访问服务器。您可以使用此技术来重定向回来,哈希标签和所有:




  • GET / some / page#with / hash

  • 登录?然后呈现页面

  • 未登录?渲染一个页面上有一些JavaScript,说'getHash.jade'

  • getHash.jade:复制完整的URL,然后重定向到'/ login& redirect ='+ originalURL

  • GET / login(现在可以在服务器上保存哈希,并从中取出)


I have an app that is Node + Express + Passport on the server and jQuery + Backbone.js on the client. The client makes use of hash tags in the URL, but for some functionality it's important for the user to be logged in.

I'd like the app to be accessible via URL, e.g. http://mydomain.com/app#cone/waffle/flavor/mint/toppings/sprinkles such that:

  • if user is already logged in, they go to the requested URL directly without hassles
  • if user is not already logged in, they go to /login and then go to the requested URL

Following this SO post, Custom returnUrl on Node.js Passport's Google strategy, I have it so that

  • If they are logged in already, they go directly the URL, hash tags and all
  • If they weren't logged, it takes them to the login page, then to the requested url, but...

It seems to strip out the hash parameters from the original URL on the redirect after login.

Is there any way to preserve the hash parameters when redirecting them to the original destination?

From this post, Getting hash parameters from request url I get the idea that the hash tags aren't available on the server, which is the whole point of using hash tags.

So I suspect it's not possible. Maybe cache the params locally somehow and retrieve them on redirect, say to [original URL minus hastags] + #use-cached-params?

解决方案

Hash parameters are browser-only, they won't go to the server. You can use this technique though for redirecting back, hash-tags and all:

  • GET /some/page#with/hash
  • logged in? then render the page
  • not logged in? render a page that has some JavaScript on it, say 'getHash.jade'
  • getHash.jade: copies the full URL, then redirects to '/login&redirect=' + originalURL
  • GET /login (now you can save the hash on the server and take it from there)

这篇关于Passport.js + Express.js验证后将用户转发到原始目的地的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆