简体   繁体   English

注销和登录(sign_out,sign_in)后,Rails,devise,SPA重定向到自定义URL(查询字符串中包含参数)

[英]Rails, devise, SPA, redirect to custom url (with param in query string) after logout and login (sign_out, sign_in)

I have a default Rails app with devise gem and some routes are totally SPA (single page application). 我有一个带有devise gem的默认Rails应用,有些路线完全是SPA(单页应用)。

If I go on that SPA pages (let's say " /home ") and I'm not authenticated (or the session is over) it redirects me to: 如果我进入该SPA页面(假设为“ /home ”)并且未通过身份验证(或会话结束),则会将我重定向至:

  • " localhost:3000/users/sign_out " localhost:3000/users/sign_out
  • and then devise to " localhost:3000/users/sign_in " 然后设计为“ localhost:3000/users/sign_in

Everything good, but. 一切都很好,但是。

What I need now is that if I go to let's say " /home/jobs " and like before I'm not authenticated or the session is over it redirects me to: 现在我需要的是,如果我说“ /home/jobs ”,就像在未通过身份验证或会话结束之前那样,它将重定向到:

  • " localhost:3000/users/sign_out?continue=localhost:3000/home/jobs " (I do that with javascript "window.location.replace") localhost:3000/users/sign_out?continue=localhost:3000/home/jobs ”(我使用javascript“ window.location.replace”做到这一点)
  • but then devise redirects me to " localhost:3000/users/sign_in " 但后来设计将我重定向到“ localhost:3000/users/sign_in
  • and after that I loose my " continue " params in query string and I cannot proceed to "continue" URL after login. 之后,我松开了查询字符串中的“ continue ”参数,并且登录后无法继续“ Continue” URL。

I know about after_sign_in_path_for and after_sign_out_path_for but I think that doesn't work with params in query string, right? 我知道after_sign_in_path_forafter_sign_out_path_for但是我认为这不适用于查询字符串中的参数,对吗?

Is this a crazy scenario? 这是一个疯狂的场景吗?

That's one problem of mixing SPA routes and non-spa routes. 这是混合SPA路由和非SPA路由的问题之一。 I think it's preferable to loose some upfront time translating devise into an api only response and then making the user session routing be SPA as well. 我认为最好放弃一些前期时间,将设计转换为仅api响应,然后将用户会话路由也设置为SPA。

I'm not sure if you can't access the request params in the devise controller, you should check, perhaps you can, but if not there is at least one other way you could work around this and that would be to have a before_action on your application_controller.rb before any devise logic, to just assign a session key based on the params, and in your after_action remove it, for example (just to illustrate): 我不确定是否无法在devise控制器中访问请求参数,应该检查一下,也许可以,但是如果没有,至少可以通过其他方法解决此问题,那就是需要一个before_action在您的application_controller.rbapplication_controller.rb任何设计逻辑之前,只需根据参数分配一个会话密钥,然后在after_action删除,例如(仅作说明):

#application_controller.rb
before_action :set_continue
#...

def set_continue
  if params[:continue]
    session[:continue] = params[:continue]
  end
end

This then would allow you to use it on the after_sign_in_path : 然后,这将允许您在after_sign_in_path上使用它:

def after_sign_in_path_for(resource)
  if session[:continue]
     navigation = session[:continue]
     session[:continue] = nil
     navigation
  else
     your_regular_path_helper #or super I guess
  end
end

You can use stored_location_for and store_location_for . 您可以使用stored_location_forstore_location_for This example is taken from the Devise wiki's How To: redirect to a specific page on successful sign in . 此示例摘自Devise Wiki的“ 如何:成功登录后重定向到特定页面”

# some_controller.rb (or even the application_controller)
if params[:redirect_to].present?
  store_location_for(resource, params[:redirect_to])    
end

# application_controller.rb
class ApplicationController < ActionController::Base
  protect_from_forgery

  protected  
    def after_sign_in_path_for(resource)
      sign_in_url = new_user_session_url
      if request.referer == sign_in_url
        super
      else
        stored_location_for(resource) || request.referer || root_path
      end
    end
end

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM