【问题标题】:Auth0 in Rails with devise has nil in the request.env['omniauth.auth']带有设计的 Rails 中的 Auth0 在 request.env['omniauth.auth'] 中为零
【发布时间】:2019-09-02 18:33:57
【问题描述】:

我正在使用 devise 设置一个具有 Active Admin 的 Rails 应用程序,并且我正在尝试使用 Auth0 添加 OmniAuth 身份验证。

我已经安装了gem 'omniauth-auth0', '~> 2.2'

我已经添加了回调控制器和路由。

我添加了中间件初始化程序(注意AUTH_CLIENT_SECRET 为空)

# config/initializers/auth0.rb
Rails.application.config.middleware.use OmniAuth::Builder do
  provider(
    :auth0,
    ENV.fetch('AUTH0_CLIENT_ID'),
    ENV.fetch('AUTH0_CLIENT_SECRET'),
    ENV.fetch('AUTH0_DOMAIN'),
    callback_path: '/auth/auth0/callback'
  )
end

我的设计初始化程序与 OAuth 无关。

视图有代码:

<div class="oauth hidden">
  <div id="root" style="width: 320px; margin: 40px auto; padding: 10px; border-style: dashed; border-width: 1px; box-sizing: border-box;">
    embedded area
  </div>
  <script src="https://cdn.auth0.com/js/lock/10.2/lock.min.js"></script>
  <script>
    var lock = new Auth0Lock(
      '<%= ENV.fetch('AUTH0_CLIENT_ID') %>',
      '<%= ENV.fetch('AUTH0_DOMAIN') %>', {
        container: 'root',
        auth: {
          redirectUrl: '<%=ENV.fetch('AUTH0_CALLBACK_URL') %>',
          responseType: 'code',
          params: {
            scope: 'openid profile email' // Learn about scopes: https://auth0.com/docs/scopes
          }
        }
      });
    lock.show();
  </script>
</div>

控制器有:

module Api
  class Auth0Controller < ApplicationController
    def callback
      # This stores all the user information that came from Auth0
      # and the IdP
      session[:userinfo] = request.env['omniauth.auth']

      # Redirect to the URL you want after successful auth
      redirect_to admin_dashboard_url
    end

    def failure
      # show a failure page or redirect to an error page
      @error_msg = request.params['message']
    end
  end
end

现在,中间件似乎无法正常工作。来自 Auth0 服务器的回调有一个这样的 url 参数:code=XXXXXXXXXXXX,并且在回调操作中,request.env['omniauth.auth'] 的值为 nil。

我做错了什么?

【问题讨论】:

    标签: ruby-on-rails devise activeadmin omniauth auth0


    【解决方案1】:

    我发现了问题:

    1. 路由在作用域内,因此config/initializers/auth0.rb 中定义的回调路径应考虑如下:/api/auth/auth0/callback
    2. admin_user 模型是omniauthable,它覆盖了为回调定义的路由。

    【讨论】:

      猜你喜欢
      • 2014-09-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-04-21
      • 2013-08-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多