简体   繁体   English

如何在Devise登录后返回上一页

[英]How to return to previous page after Devise sign in

After a successful login via Devise, how can I redirect the user back to the page they were on? 通过Devise成功登录后,如何将用户重定向回他们所在的页面?

I've read and searched a lot already, and I understand that I need to define after_sign_in_path_for . 我已经阅读和搜索了很多,我知道我需要定义after_sign_in_path_for I've done this and its working correctly, what I'm having trouble with is understanding how the previous page is stored, and how to call it correctly. 我已经完成了这个并且它正常工作,我遇到的问题是理解上一页的存储方式,以及如何正确调用它。

I have this in my sessions controller: 我在会话控制器中有这个:

def after_sign_in_path_for(resource)
    return request.env['omniauth.origin'] || session[:user_return_to] || root_path
end

I've also tried 我也试过了

...
return request.env['omniauth.origin'] || stored_location_for(resource) || root_path
...

I don't think I am understanding how to store the location, as the user is redirected back to the root path if they click to log in. 我不认为我理解如何存储位置,因为如果用户单击登录,则会将用户重定向回根路径。

A sign in can be initiated in two ways. 登录可以通过两种方式启动。 Either (a) the user attempts to access a restricted view (ie before_filter :authenticate_user!... , in which case they are redirected and prompted to login. Or (b) the user clicks a sign in link which is available on every page if the user is not logged in. (a)用户尝试访问受限视图(即before_filter :authenticate_user!... ,在这种情况下,它们被重定向并提示登录。或者(b)用户单击每个页面上可用的登录链接如果用户未登录

(a) seems to be working. (a)似乎有效。 (b) is not. (b)不是。 I guess I need to store the current location to session when the user clicks the log in link. 我想我需要在用户点击登录链接时将当前位置存储到会话。

How do I do this? 我该怎么做呢? Or, where is a good source of information that would help me understand this. 或者,哪里有一个很好的信息来源,可以帮助我理解这一点。

Thanks! 谢谢!

你可以使用request.referrer获取前一个url,如下所述: 如何在Ruby On Rails中重定向到上一页?

采用

redirect_to request.referrer

Heads up: The code should go in your application_controller.rb, not sessions controller. 抬头:代码应该放在application_controller.rb中,而不是会话控制器。

How it works is that newer versions of Devise automatically store the initial page the user tried to visit (before getting redirected to sign in). 它的工作原理是较新版本的Devise自动存储用户试图访问的初始页面(在重定向到登录之前)。

That can be accessed by using the devise helper: stored_location_for(resource) 可以使用设计帮助器访问: stored_location_for(resource)

What you add to your application controller is something like this: 您添加到应用程序控制器的内容是这样的:

def after_sign_in_path_for(resource)
  stored_location_for(resource) || users_dashboard_path
  # replace users_dashboard_path by whichever route you want to redirect to after login - default is root_path
end

After a successful sign in, Devise will automatically run your method from application controller instead of it's own (overriding it). 成功登录后,Devise将自动从应用程序控制器运行您的方法,而不是自己的方法(覆盖它)。

For a more complex example, here's one from the Devise team: 对于更复杂的示例,这是来自Devise团队的示例:

def after_sign_in_path_for(resource)
  stored_location_for(resource) ||
    if resource.is_a?(User) && resource.can_publish?
      publisher_url
    else
      super
    end
end

I hope this answer helps anyone searching how to do this with newer versions of devise. 我希望这个答案可以帮助任何人搜索如何使用更新版本的设备进行此操作。

Here is another approach. 这是另一种方法。 I am enabling this "return to" feature only on certain links (via the continue URL parameter). 我只在某些链接上启用此“返回”功能(通过continue URL参数)。 In the example below I am taint checking params[:continue] prior to assigning it to session[:continue] in the after_action - though perhaps that is unnecessary if you have authorization in place . 在下面的例子中,我在检查params[:continue]之前将其分配给after_action中的session[:continue] - 尽管如果你有授权,这可能是不必要的 Finally, in the after_sign_in_path_for method override I delete session[:continue] meanwhile using it (as it is not needed afterwards) - deleting the key/value pair returns the value if the key matches, otherwise nil is returned, in which case the statement falls back on root_path . 最后,在after_sign_in_path_for方法覆盖中我删除 session[:continue]同时使用它(因为之后不需要) - 删除键/值对如果键匹配则返回值,否则返回nil ,在这种情况下语句回到root_path

app/views/journal/show.html.erb 应用程序/视图/杂志/ show.html.erb

<%= link_to 'sign in', new_user_session_path(continue: journal_url(@journal)) %> or
<%= link_to 'sign up', new_user_registration_path(continue: journal_url(@journal)) %>

app/controllers/application_controller.rb 应用程序/控制器/ application_controller.rb

after_action :store_location

def store_location
    if params[:continue] =~ /\/(journal\/[0-9]+|foo|bar)\z/ # safelist
        session[:continue] = params[:continue]
    end
end

def after_sign_in_path_for(resource)
    session.delete(:continue) || root_path
end

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

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