簡體   English   中英

第二次登錄后 Devise-jwt 找不到活動的 session

[英]Devise-jwt unable to find active session after 2nd login

我正在努力僅創建一個 API 應用程序(無視圖)。 我已經安裝了 Devise 和用於身份驗證的 Devise-JWT Gem https://github.com/waiting-for-dev/devise-jwt 當在本地運行服務器時,我可以注冊並且可以找到一個活動的 session 給我我的 users/1 端點“顯示”或者如果我播種數據庫我可以登錄一次並找到一個活動的 session。

在我注銷並再次登錄並在 User show 方法上發出 GET 請求后,它返回一個錯誤,如果我注銷,則錯誤指出未找到活動的 session。

`

class Users::SessionsController < Devise::SessionsController
  respond_to :json

  private

  def respond_with(resource, _opts = {})
    render json: {
      status: { code: 200, message: 'Logged in sucessfully.' },
      data: UserSerializer.new(resource).serializable_hash[:data][:attributes]
    }, status: :ok
  end


  def respond_to_on_destroy
    if current_user
      render json: {
        status: 200,
        message: 'logged out successfully'
      }, status: :ok
    else
      render json: {
        status: 401,
        message: "Couldn't find an active session."
      }, status: :unauthorized
    end
  end
end

`

`

class Users::RegistrationsController < Devise::RegistrationsController
  respond_to :json

  private

  def respond_with(resource, _opts = {})
    if resource.persisted?
      render json: {
        status: { code: 200, message: 'Signed up sucessfully.' },
        data: UserSerializer.new(resource).serializable_hash[:data][:attributes]
      }
    else
      render json: {
        status: { message: "User couldn't be created successfully. #{resource.errors.full_messages.to_sentence}" }
      }, status: :unprocessable_entity
    end
  end
end

`

我的功能分支的鏈接在這里https://github.com/AKeeganDev/client_relationship_manager_back_end/tree/api-endpoints

如果我沒有提供足夠的信息,請原諒我。 我是rails新手,ruby,JWT。這也是我的第一篇文章。 幾天來我盡了最大努力在谷歌上搜索並搜索這個網站,但我還沒有找到解決我做錯了什么以及為什么做錯的方法。 謝謝!

我試過刪除/更改 protect_from_forgery 行並調整 ApplicationController

`

class ApplicationController < ActionController::Base
  protect_from_forgery except: :sign_in
  before_action :configure_permitted_parameters, if: :devise_controller?

  respond_to :json

  protected

  def configure_permitted_parameters
    devise_parameter_sanitizer.permit(:sign_up, keys: %i[username name])
  end
end

`

刪除防止偽造會增加有關 csrf 真實性的更多問題。

在數據庫中創建了一個新的 Bearer Token 和 jti token,並且每次都在 fetch header 中發送,但由於某種原因,只有第一個 bearer token 有效,而在第一個 session 終止后創建的任何 token 都沒有。

我解決了這個問題。 我有一行包含 JTI MATCHER 作為我的撤銷策略。 通過將其適當更改為 DenyList,我能夠在第一個 session 之后再次自由登錄和注銷。

我的用戶 Model 現在看起來像這樣

class User < ApplicationRecord
  # Include default devise modules. Others available are:
  # :confirmable, :lockable, :timeoutable, :trackable and :omniauthable
  include Devise::JWT::RevocationStrategies::Denylist

  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :validatable,
         :jwt_authenticatable, jwt_revocation_strategy: JwtDenylist

  has_many :contacts, dependent: :delete_all
  has_many :logs, through: :contacts

  validates :name, :username, presence: true, length: { minimum: 1 }

  def first_five_contacts
    contacts.order(created_at: :desc).limit(5)
  end
end

暫無
暫無

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

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