簡體   English   中英

Rails + Devise:如何在登錄時檢查是否記住了用戶

[英]Rails + Devise: How can I check if a user is remembered when signing in

我如何確定登錄的用戶是否在較舊的會話中被記住。

我找到了這個答案: Rails / Devise:自動登錄后執行操作 ,它說在記住用戶后調用after_remembered方法。 我嘗試了一下,然后將其放入模型中,但是沒有被調用。 我什至在此行之前放置了binding.pry https://github.com/plataformatec/devise/blob/master/lib/devise/strategies/rememberable.rb#L30,但整個過程都進行了身份驗證! 登錄時甚至不會調用方法。

我希望有人能幫幫忙

好的,我以前創建after_update回調的想法不起作用,因為它不會在記住的會話中進行更新。

我最終做的不是很漂亮,但是我也不想胡鬧看守人。

class Users::SessionsController < Devise::SessionsController
  around_action :check_remembered, only: :create

  def create
    super
  end

  private
  def check_remembered
    # find the user based on the email in the login params. 
    # You can't use current_user here because once you call it, 
    # it will update the user before the action even ran (no idea why)
    user = User.find_by_email(params[:user][:email])
    yield
    if user && user.sign_in_count != current_user.sign_in_count
      # user was not remembered
    end
  end
 end

對於僅電子郵件登錄,就足夠了。 如果使用的是omniauth登錄,則必須通過檢查是否存在params[:user][:email]來阻止check_remembered函數中的每個omniauth登錄。 這些將必須在您的omniauth控制器中進行處理。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM