簡體   English   中英

用戶登錄rails時返回JWT令牌

[英]Return JWT token when user signs in rails

使用devise gemdevise-jwt gem獲取JWT令牌的問題。 這就是我確認的樣子。

devise.rb

  Devise.setup do |config|

    config.jwt do |jwt|
      jwt.secret =  SECRETS.devise_jwt_secret_key
      jwt.dispatch_requests = [ ['POST', %r{^/authentication_tokens/create$}] ]
    end
end 

user.rb

class User < ApplicationRecord

    devise :database_authenticatable, :registerable,
           :recoverable, :rememberable, :trackable, :validatable,
           :jwt_authenticatable, jwt_revocation_strategy: Devise::JWT::RevocationStrategies::Null

  end

authentication_tokens_controller.rb

class Api::V1::AuthenticationTokensController < Devise::SessionsController
  include Devise::Controllers::Helpers
  skip_before_action :verify_authenticity_token 

  prepend_before_action :require_no_authentication, only: [:create]

  before_action :rewrite_param_names, only: [:create]

  def new
    render json: { response: "Authentication required" }, status: 401
  end

  def create
    self.resource = warden.authenticate!(auth_options)
    sign_in(resource_name, resource)
    yield resource if block_given?
     render json: {success: true, jwt: current_token, response: "Authentication successful" }
  end

  private

  def rewrite_param_names
    request.params[:user] = {email: request.params[:email], password: request.params[:password]}
  end

  def current_token
    request.env['warden-jwt_auth.token']
  end

end

的routes.rb

   get 'home#secret'
   devise_for :users
   resources :tasks
   #other routes for the website removed for brevity

  namespace :api, defaults: { format: :json } do
    namespace :v1 do
      resources :users
      devise_scope :user do
        post '/authentication_tokens/create', to: "authentication_tokens#create"
      end
    end
  end

出於某種原因, request.env['warden-jwt_auth.token']返回null ,但是,用戶已通過身份驗證。 當用戶登錄時,我需要添加什么來獲取JWT令牌嗎?

更新 - 路由和命名空間

經過幾天的調試,我相信我找到了問題的根源。 我的應用程序有一個使用正常路由的前端。 如果我執行類似下面的代碼,上面的代碼不起作用。 一切都很好。

  scope :api, defaults: {format: :json} do
    devise_for :users, controllers: {sessions: 'v1/authentication_tokens'}
  end

有沒有一種方法devise_for為我的devise_for命名API,即使它已在上面用於網站?

你只需要讓你的路線RESTful。

的routes.rb

post '/authentication_tokens', to: "authentication_tokens#create"

devise.rb

config.jwt do |jwt|
  jwt.secret =  SECRETS.devise_jwt_secret_key
  jwt.dispatch_requests = [ ['POST', %r{^/authentication_tokens$}] ]
end

我簡要地看了一下你的問題,這可能是錯的,但有些東西可供您試一試:

看以下幾行

def create
  self.resource = warden.authenticate!(auth_options)
end

def current_token
  request.env['warden-jwt_auth.token']
end

如果你說即使從current_token方法返回nil ,用戶也正在進行身份驗證,這意味着jwt正確傳遞,但你獲取它的方式是錯誤的。

嘗試調試self.resource = warden.authenticate!(auth_options)行並查看auth_options包含的auth_options ,可能你可以從那里獲取JWT,或者你只是試圖以錯誤的方式獲取warden-jwt_auth.token 嘗試調試這一行,看看你是否應該從request.headers["warden-jwt_auth.token"]或類似的東西中取“warden-jwt_auth.token”。 只需打印出您的請求的整個響應,並按需要的標題進行搜索。

我希望這有幫助!

使用完整路徑更新jwt.dispatch_requests ,之后重啟rails,對我jwt.dispatch_requests

devise.rb

config.jwt do |jwt|
  jwt.secret = "secret"
  jwt.dispatch_requests = [ ['POST', %r{^/api/v1/authentication_tokens/create$}] ]
  jwt.expiration_time = 1.day.to_i
end

暫無
暫無

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

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