简体   繁体   English

Auth0 with Rails API 使用 Knock

[英]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.使用教程,我已经能够使用knock 设置我的Rails API,但是当我提供JWT 时,身份验证似乎不起作用。

Here is my Knock.rb这是我的 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:用户.rb:

class User < ApplicationRecord
  has_secure_password

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

projects_controller.rb: project_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我通过 POST 请求获得了 JWT 到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.最后我换成了Auth0官方文档推荐的方法。 It is really simple and works perfect.这真的很简单,而且工作完美。

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

Hope it helps.希望能帮助到你。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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