Rails / Devise:自动登录后执行操作 [英] Rails/Devise: execute action after automatic login

查看:388
本文介绍了Rails / Devise:自动登录后执行操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

编辑:我使用Devise 3.4.1,而after_remembered不可用。或者应用这个小补丁或使用2014/11年后发布的较新版本/ 09。



好的,所以我对Rails环境很新,我觉得我缺少一些东西。



我发现after_sign_in_path_for是在常规之后执行的 登录。我已经在我的应用程序控制器中使用了它。

  class ApplicationController< ActionController :: Base 

[...]

def after_sign_in_path_for(资源)
myAction
end
/ pre>

但是,当用户通过Devise Remember选项登录时,似乎没有调用此方法。



我发现 after_remembered 是在用户自动登录后执行,但是我不明白如何使用它。我不想修改Devise宝石,只是为了将我的操作添加到该方法中,以下似乎不起作用:

  class ApplicationController< ActionController :: Base 

[...]

def after_remembered()
myAction
end

我在这里亏本。或者我不明白如何使用after_remembered,或者我看着它的错误的方式,还有另一个解决方案(应该覆盖这两种情况,或只有记住我自动登录的情况,对我来说很好) 。有人有任何想法让它工作吗?



PS:我正在开发一个我还没有开发自己的应用程序。如果你觉得需要更多的代码才能回答,请告诉我,我将编辑我的帖子。

解决方案

可记住模块,因此它属于您的模型...假设您的资源是用户您将使用的方式。 ..

  class User< ActiveRecord :: Base 

devise:rememberable

def after_remembered
#(您的代码在这里)
end

end

我不知道Devise是否提供了一个控制器特定的方法,但如果不是,您可以抛出一个togeter。 ..在您的用户模型中创建一个属性,例如 signed_in_via_remembered

 类User< ActiveRecord :: Base 

devise:rememberable

def after_remembered
update_attribute(:signed_in_via_remember,true)
end

end

然后在你的application_controller.rb中

  class ApplicationController< ActionController :: Base 

before_action:handle_sign_in_via_rememberable

def handle_sign_in_via_rememberable
如果current_user和current_user.signed_in_via_remember?
current_user.update_attribute(:signed_in_via_remember,false)
after_sign_in_path_for(用户)
结束
结束

def after_sign_in_path_for(资源)
myAction
end

end

有人可能有一个更整洁的解决方案。 / p>

EDIT: I use Devise 3.4.1, and after_remembered isn't available. Either apply this small patch or use a newer version released after 2014/11/09.

Alright, so I am rather new to the Rails environment, and I feel I am missing something.

I wish to execute a particular action after login, be it after login from a form or automatic login.

I found out that after_sign_in_path_for is executed after a "regular" login. I already use it in my Application controller.

class ApplicationController < ActionController::Base

[…]

def after_sign_in_path_for(resource)
    myAction
end

However, it seems that this method isn't called when a user is logged in through the Devise Remember option.

I found out that after_remembered is executed after a user is automatically logged in, however I don't understand how to use it. I don't want to modify the Devise gem just to add my action to the method, and the following doesn't seem to work:

class ApplicationController < ActionController::Base

[…]

def after_remembered()
    myAction
end

I am at a loss here. Either I don't understand how to use after_remembered, or I look at it the wrong way and there is another solution for that (should it cover either both cases or only the case with "remember me" automatic login, is fine for me). Does anyone have any idea to make it work?

PS: I am working on an app I haven't developed myself. If you feel you need more code in order to answer, tell me and I will edit my post.

解决方案

It's part of the Rememberable module so it belongs in your model... assuming your resource is User the way you would use it is...

class User < ActiveRecord::Base

  devise :rememberable

  def after_remembered
    # (your code here)
  end

end

I'm not sure if Devise offers a controller-specific method but if not you could throw one togeter... create an attribute in your User model called, say, signed_in_via_remembered

class User < ActiveRecord::Base

  devise :rememberable

  def after_remembered
    update_attribute(:signed_in_via_remember, true)
  end

end

then in your application_controller.rb

class ApplicationController < ActionController::Base

  before_action :handle_sign_in_via_rememberable

  def handle_sign_in_via_rememberable
    if current_user and current_user.signed_in_via_remember?
      current_user.update_attribute(:signed_in_via_remember, false)    
      after_sign_in_path_for(User)
    end
  end

  def after_sign_in_path_for(resource)
    myAction
  end

end

Someone may have a neater solution.

这篇关于Rails / Devise:自动登录后执行操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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