Rails:Warden / Devise - 如何在登录/访问失败之前捕获url [英] Rails: Warden/Devise - How to capture the url before login/failed access

查看:89
本文介绍了Rails:Warden / Devise - 如何在登录/访问失败之前捕获url的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图找出如何使用Warden / Devise将用户重定向到他们登录的页面(或无法登录)。我认为有一个会话变量在某个地方可用或可以提供。

I am trying to figure out how to redirect a user to the page they logged in from (or failed to login) using Warden/Devise. I figure there is a session variable somewhere that is either available or could be made available.

例如。
方案1:
非授权用户转到受保护的页面X;
重定向到登录页面;
用户登录;
用户重定向到受保护的页面x

E.g. Scenario 1: Non-authorized user goes to protected page X; Redirected to login page; User logs in; User redirected to protected page x

方案2:
非授权用户希望在第x页上采取受保护的操作;
用户点击登录链接;
用户登录;
用户重定向到页面x,该操作现在可用

Scenario 2: Non-authorized user wants to take a protected action on page x; User clicks on login link; User logs in; User redirected to page x where the action is now available

任何指针都将被欣赏。

谢谢!

推荐答案

有一个设计助手方法称为 after_sign_in_path_for(资源) http://rdoc.info/github/plataformatec/devise/master / Devise / Controllers / Helpers ),以及一个名为 session [:user.return_to] 的会话变量,它存储最后一个URL。 after_sign_in_path_for 方法需要返回一个字符串,然后设计自动使用此路径在登录后重定向用户。

There is a devise helper method called after_sign_in_path_for(resource) (http://rdoc.info/github/plataformatec/devise/master/Devise/Controllers/Helpers) , and a session variable called session[:"user.return_to"] which stores the last url. The after_sign_in_path_for method needs to return a string, then devise automatically uses this path to redirect the user after login.

在我的应用程序控制器中,如果会话变量未设置,我将以下内容重定向到主页:

In my application controller I have put the following which redirects my users to the home page if the session variable is not set:

def after_sign_in_path_for(resource)
    (session[:"user.return_to"].nil?) ? "/" : session[:"user.return_to"].to_s
end

这篇关于Rails:Warden / Devise - 如何在登录/访问失败之前捕获url的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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