Skip to content

env['omniauth.auth'] is nil when customizing callback_path #767

Closed

Description

@mhutter

I have a Rails app that "lives" in a subdirectory. To make sure the user gets to my app after authentication, I've added the following to my config/initializers/omniauth.rb:

callback_path: Rails.application.secrets.path+'/auth/ldap/callback'

which works fine so far. However, now in my callback controller, env['omniauth.auth'] is always nil

Any Ideas to why this is happening?

I have an almost identical setup in another App (that's served from a Root directory) which works just fine.

Activity

  1. geori commented on Sep 30, 2014

    @geori

    I'm having the same exact problem with a Sinatra implementation of omniauth-twitter. If i set :callback_path, it returns to the correct place I specified in :callback_path, but request.env['omniauth.env'] is nil. And FYI, request.env['omniauth.origin'] is also nil, but you can access the value via rack session.

  2. zmajstor commented on Nov 5, 2014

    @zmajstor

    I'm also having the same problem. In my Rails 4.2 app, I've added following to my config/initializers/omniauth.rb:
    provider :foo, name: :bar, callback_path: "/sub/auth/bar/callback"
    and in my callback controller, env['omniauth.auth'] is always nil

  3. delacruzjames commented on Feb 11, 2015

    @delacruzjames

    Any update im also having same problem?

  4. mcfiredrill commented on May 8, 2015

    @mcfiredrill

    I'm not sure if this is your issue, but I had an issue with the callback path not being recognised if it contained extraneous slashes (like //auth/ldap/callback).

    You may want to try removing the first slash and see if it works.

  5. mcfiredrill commented on May 8, 2015

    @mcfiredrill

    If you don't see the Callback Phase Initiated message in your logs, then the callback_path was probably not recognised.

  6. KELiON commented on Jul 24, 2015

    @KELiON

    +1, have almost the same issue with rails 4.2.3 and omniauth 1.2.2. I see double slashes after callback (//auth/:provider/callback) and no Callback Phase Initiated in logs. After removing one slash it works fine. But I don't customize callback_path param

  7. PandaWhisperer commented on Oct 13, 2015

    @PandaWhisperer

    I'm having the same issue. Weirdly, it's not that request.env['omniauth.auth'] is nil, rather, it returns nil, which makes it even weirder. I inspected it in better_errors, and when I type just request.env, it clearly shows the 'omniauth.auth' key and it's value, but for some reason, request.env['omniauth.auth'] returns nil nevertheless.

    As soon as I remove the customized callback path, this behavior goes away, as OP suggested.

  8. tobias74 commented on Oct 18, 2015

    @tobias74

    this happens in the following situation: (applies to 1.2.2 as well as the current master)

    the following situation arises:
    -> "request.path_info" is set to "/internal/callback" by rack
    -> "on_callback_path" compares "my/custom/callback" with "/internal/callback" and returns false
    --> the method callback_call is never executed.
    --> env['omniauth'] is nil

  9. added a commit that references this issue on Feb 19, 2016
  10. jiggneshhgohel commented on Jul 25, 2016

    @jiggneshhgohel

    I am facing this similar issue. Following are the gem versions used in my application.

    ruby (2.3.0)
    rails (4.2.6)
    omniauth-facebook (3.0.0)
    omniauth-oauth2 (1.4.0)
    omniauth (1.3.1)

    Did anyone found a solution or a work-around for this?

  11. jiggneshhgohel commented on Jul 28, 2016

    @jiggneshhgohel

    I have resolved my problem through a workaround. I have posted my solution in a gist. Hoping that it will be beneficial to people like me looking out for a solution to this problem.

    External Links:

    http://stackoverflow.com/q/38627949/936494

  12. simonneutert commented on May 15, 2017

    @simonneutert

    i was able to retrieve the necessary data by changing env["omniauth.auth"] to request.env["omniauth.auth"]

    Rails 5.1
    Devise 4.3.0
    omniauth (1.6.1)
    omniauth-facebook (4.0.0)
    omniauth-oauth2 (1.4.0)

  13. SandipBhowmick commented on Nov 22, 2017

    @SandipBhowmick

    Provider Gem: 'omniauth', '~> 1.2.1'
    Ruby Version: ruby 2.2.1p85
    Platform: Ubuntu 14.04 LTS
    I want to create different login flow for admin, so I have create another sessions controller under admin. But when I try to access env["omniauth.auth"] from admin/sessions_controller.rb it shows error "NoMethodError at /admin/auth/identity/callback
    undefined method `[]' for nil:NilClass" I found env["omniauth.auth"] return nil
    Please help me

  14. nantestdeveloper commented on Dec 1, 2017

    @nantestdeveloper

    provider: identity

    calling api/v1/auth/identity/callback for api return nil auth_hash and also set the routes for same

    getting error

    NoMethodError at /api/v1/auth/identity/callback

    ===============================================

    undefined method `[]' for nil:NilClass

    I want to call the post call api for sign in and sign up using omniauth identity

  15. wangthony commented on May 18, 2020

    @wangthony

    This issue has sprawled out to a bunch of different cases, but it does seem like the callback_path option gets lost somehow.

    For an alternative solution using querystring params, see #661 (comment)

  16. s1monj commented on Jul 8, 2020

    @s1monj

    I had the same issue as outlined by tobias74 above - my app is behind a reverse proxy with a RAILS_RELATIVE_URL_ROOT (also #903). My workaround is to add the relative URL component to full_host and set the callback_path to match the original (so "my/custom/callback" matches "/internal/callback" per tobias74) and use a querystring param (see above) if you then need to switch from the callback

      OmniAuth.config.full_host = lambda do |_|
        'https://www.example.com/my_relative_path'
      end
      
      config.omniauth :azure_oauth2,
        client_id: ENV['AZURE_CLIENT_ID'],
        client_secret: ENV['AZURE_CLIENT_SECRET'],
        tenant_id: ENV['AZURE_TENANT_ID'],
        callback_path: '/users/auth/azure_oauth2/callback'
    

    rack 2.2.3
    rails 5.2.0
    omniauth 1.9.1

  17. trkoch commented on Feb 18, 2021

    @trkoch

    For me, simply setting both client_options.redirect_uri and callback_path did the trick. Note the former is relative, while the latter is a full URL (i.e. do not set to the same value). I guess client_options.redirect_uri is to tell the provider, and callback_path is where to expect the callback (from the standpoint of Omniauth).

    Not sure you're affected by this issue? If you don't see Callback phase initiated in your console after receiving the callback, Omniauth did not recognize the (customized) callback URL (i.e. it does not care that this particular request just came in).

  18. khangal commented on Jan 10, 2025

    @khangal

    @trkoch thank you for giving me clue, in my case my callback_path, and redirect_uri needed to be different and explicitly set because I'm using envoy.

  19. pboling commented on Nov 6, 2025

    @pboling
    Member

    I've documented how to accomplish using a subdirectory in the README.md of omniauth-ldap, and it mostly applies to any omniauth setup: https://github.com/omniauth/omniauth-ldap#mounted-under-a-subdirectory-script_name

  20. added theissue type on Oct 17, 2026
  21. sferik commented on Oct 17, 2026

    @sferik
    Contributor

    Thanks for opening this issue. In OmniAuth 1.x the callback path was compared against PATH_INFO, which leaves out the app's mount point, so a callback_path that included the sub-directory never matched. #903, released in v2.0.0, compares against the full request path (including SCRIPT_NAME) and prefixes the default request and callback paths with SCRIPT_NAME automatically. If you still see env['omniauth.auth'] as nil with a custom callback_path on the latest version, please open a new issue with your configuration.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions