簡體   English   中英

如何為 devise_token_auth 會話跳過_before_action#create

[英]How to skip_before_action for devise_token_auth sessions#create

我有一個 Rails 5 應用程序(僅限 api)。 我正在使用devise_token_auth進行身份驗證。 在應用程序中,我有幾個控制器,對於所有控制器,我都想對用戶進行authenticate_user! . 我想避免重復before_action:authenticate_user! ,所以我把它放在 ApplicationController 中。

class ApplicationController < ActionController::API
  include DeviseTokenAuth::Concerns::SetUserByToken
  before_action :authenticate_user!
end

使用這種方法, authenticate_user! 即使在登錄和注冊期間也會被調用。 我怎樣才能跳過:authenticate_user! 用於登錄、注冊和注銷? 我試圖猴子修補 SessionsController 以添加skip_before_action

module DeviseTokenAuth
  class SessionsController
    skip_before_action :authenticate_user!, only: [:create, :destroy, ]
  end
end

但是控制器是延遲加載的,所以我收到以下錯誤:

undefined method `skip_before_action' for DeviseTokenAuth::SessionsController:Class

我錯過了什么? 還有其他解決方案嗎? 感謝您的幫助!

您的SessionsController類應該繼承自Devise::SessionsController以使用skip_before_action類方法。

module DeviseTokenAuth
  class SessionsController < Devise::SessionsController
    skip_before_action :authenticate_user!, only: [:create, :destroy]
  end
end

您可以在之后聲明

before_action :authenticate_user! 
  
::DeviseTokenAuth::SessionsController.class_eval do
  skip_before_action :authenticate_user!
end

暫無
暫無

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

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