Call a registered API in rails from rails using SDK

  • Which SDK this is regarding: omniauth-auth0
  • SDK Version: e.g. 2.5.0
  • Platform Version: e.g. ruby 3 / rails 6.1.3.1

I’m having some trouble understanding what to do exactly to use de SDK to call an API.
What I have done:

  • An API in rails, protected by Auth0. (api)
  • An Angular app, protected by Auth0. Login works. (app1)
  • Successfully called the API from the angular app.
  • A new Rails web App, protected by Auth0. Login works. (app2)
  • ERROR calling the API from app2.
# Error in the logs of the api side. 
JSON::JWT::InvalidFormat Invalid JWT Format.
JWT should include 3 or 5 segments gem jwt.rb

I use the following code to request the API from rails using faraday:

      def request(method, path, body = {})
        conn = Faraday.new(url: @base_uri) do |c|
        c.request :url_encoded
        c.adapter :net_http
      end

      conn.send(method) do |req|
        req.url path
        req.headers['Content-Type'] = 'application/json'
        req.headers['Accept'] = 'application/json'
        req.headers['Authorization'] = "Bearer #{@token}" # 👈
        req.body = body.to_json
      end

The @token I use there is obtained during login in app2, in a similar way I do in app1. In this case I save it in the callback of the auth0 controller:

    auth_info = request.env['omniauth.auth']
    session[:userinfo] = auth_info['extra']['raw_info']
    session[:token] = auth_info['credentials']['token'] # 👈

I check on docs but I always end up in the description of calling the API from Rails directly with net/http instead of using the SDK.

1 Like