简体   繁体   中英

Auth0 with Rails API using Knock

Using this tutorial I have been able to set up my Rails API with knock, but authentication doesn't seem to be working when I provide the JWT.

Here is my Knock.rb

Knock.setup do |config|

  config.token_audience = -> { Rails.application.secrets.auth0_client_id }
  config.token_secret_signature_key = -> { Rails.application.secrets.auth0_client_secret }

end

User.rb:

class User < ApplicationRecord
  has_secure_password

  def self.from_token_payload payload
    payload['sub']
  end
end

projects_controller.rb:

class ProjectsController < ApplicationController
  before_action :set_project, only: [:show, :update, :destroy]
  before_action :authenticate_user

  # GET /projects
  def index
      @projects = Project.all

      json_response(@projects)
  end

...

end

I got the JWT with a POST request to https://my-auth0-site.auth0.com/oauth/token

In postman: 在此处输入图片说明

You must add Bearer to token. Look at the picture below

在此处输入图片说明

I had the same problem without being able to make it work. At the end I changed to the method recommended by the official Auth0 documentation. It is really simple and works perfect.

https://auth0.com/docs/quickstart/backend/rails/01-authorization

Hope it helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM