isaacsanders / omniauth-stripe-connect

Stripe Connect OAuth2 Strategy for OmniAuth 1.0
MIT License
130 stars 74 forks source link

Callback Route Not Matching #32

Closed septerr closed 9 years ago

septerr commented 9 years ago

Not sure why this happens. But here is the scenario:

I have omniauth-stripe-connect as well as devise in my gemfile.

I have following in initializers/devise.rb: config.omniauth :stripe_connect, ENV['STRIPE_CONNECT_CLIENT_ID'], ENV['STRIPE_SECRET']

My routes.rb:

Rails.application.routes.draw do
 devise_for :users, :controllers => {:omniauth_callbacks => "omniauth_callbacks", :sessions => 'sessions', :registrations => 'registrations' }
 devise_for :admin_users, ActiveAdmin::Devise.config
 ActiveAdmin.routes(self)    
 #other routes
end

My OmniauthCallbacksController: class OmniauthCallbacksController < Devise::OmniauthCallbacksController

def facebook
 @user = User.find_for_facebook_oauth(request.env["omniauth.auth"], current_user)
  if @user.persisted?
   sign_in_and_redirect @user, :event => :authentication #this will throw if @user is not activated
   set_flash_message(:notice, :success, :kind => "Facebook") if is_navigational_format?
  else
   set_flash_message(:notice, :failure, :kind => "Facebook") if is_navigational_format?
   redirect_to new_user_session_path and return
  end
end

def twitter
 @user = User.find_for_twitter_oauth(request.env["omniauth.auth"], current_user)
  if @user.persisted?
   set_flash_message(:notice, :success, :kind => "Twitter") if is_navigational_format?
   sign_in_and_redirect @user, :event => :authentication #this will throw if @user is not activated
  else
   set_flash_message(:error,  @user.errors.empty? ? "Error" : @user.errors.full_messages.to_sentence, :kind => "Twitter") if is_navigational_format?
   redirect_to new_user_session_path and return
 end
end

def stripe_connect
 @user = current_user
 @user.update_attributes({
                            stripe_uid: request.env["omniauth.auth"].uid,
                            stripe_access_code: request.env["omniauth.auth"].credentials.token,
                            stripe_publishable_key: request.env["omniauth.auth"].info.stripe_publishable_key
                        })
 # head :ok  and return
 redirect_to new_user_session_path and return
end

def after_sign_in_path_for(resource)
 #this is called by OmniauthCallbacksController in sign_in_and_redirect
 #since twitter does not provide an email address, for new twitter users we want to redirect to users#show_finish_signup
 #resource will be user
  if resource.email_verified?
    super resource
  else
    show_finish_signup_path(resource)
  end
 end
end

When I do rake routes, I see the authorize and callback paths:

user_omniauth_authorize GET|POST   /users/auth/:provider(.:format)                     omniauth_callbacks#passthru {:provider=>/facebook|twitter/}
user_omniauth_callback GET|POST   /users/auth/:action/callback(.:format)              omniauth_callbacks#:action

I have set the callback url in my Stripe account to:

http://localhost:3000/users/auth/stripe_connect/callback

When I go to /users/auth/stripe_connect in the browser, it redirects me to connect.stripe.com as expected and after I click on the blue button 'Connect My Stripe Account', the page then redirects to http://localhost:3000/users/auth/stripe_connect/callback. At this point I get a 404.

I dont understand why http://localhost:3000/users/auth/stripe_connect/callback does not match the user_omniauth_callback route.

This is what I see in the console:

Started GET "/users/auth/stripe_connect" for 127.0.0.1 at 2015-03-07 17:28:50 -0600
 I, [2015-03-07T17:28:50.074541 #6297]  INFO -- omniauth: (stripe_connect) Request phase initiated.
 Started GET "/users/auth/stripe_connect/callback?state=a45b3c5dc922db0494f5e2ef5b3d40fb93c3e94260074fd3&scope=read_only&code=ac_5xyzX" for 127.0.0.1 at 2015-03-07 17:28:53 -0600
I, [2015-03-07T17:28:53.335441 #6297]  INFO -- omniauth: (stripe_connect) Callback phase initiated.
ActionController::RoutingError (No route matches [GET] "/users/auth/stripe_connect/callback"):
   actionpack (4.1.5) lib/action_dispatch/middleware/debug_exceptions.rb:21:in `call'
   actionpack (4.1.5) lib/action_dispatch/middleware/show_exceptions.rb:30:in `call'
   railties (4.1.5) lib/rails/rack/logger.rb:38:in `call_app'
   railties (4.1.5) lib/rails/rack/logger.rb:20:in `block in call'
   activesupport (4.1.5) lib/active_support/tagged_logging.rb:68:in `block in tagged'
   activesupport (4.1.5) lib/active_support/tagged_logging.rb:26:in `tagged'
   activesupport (4.1.5) lib/active_support/tagged_logging.rb:68:in `tagged'
   railties (4.1.5) lib/rails/rack/logger.rb:20:in `call'
   actionpack (4.1.5) lib/action_dispatch/middleware/request_id.rb:21:in `call'
   rack (1.5.2) lib/rack/methodoverride.rb:21:in `call'
   rack (1.5.2) lib/rack/runtime.rb:17:in `call'
   activesupport (4.1.5) lib/active_support/cache/strategy/local_cache_middleware.rb:26:in `call'
   rack (1.5.2) lib/rack/lock.rb:17:in `call'
   actionpack (4.1.5) lib/action_dispatch/middleware/static.rb:64:in `call'
   rack (1.5.2) lib/rack/sendfile.rb:112:in `call'
   rack-cors (0.3.0) lib/rack/cors.rb:72:in `call'
   railties (4.1.5) lib/rails/engine.rb:514:in `call'
   railties (4.1.5) lib/rails/application.rb:144:in `call'
   rack (1.5.2) lib/rack/lock.rb:17:in `call'
   rack (1.5.2) lib/rack/content_length.rb:14:in `call'
   rack (1.5.2) lib/rack/handler/webrick.rb:60:in `service'
   /Users/sony/.rvm/rubies/ruby-2.1.2/lib/ruby/2.1.0/webrick/httpserver.rb:138:in `service'
   /Users/sony/.rvm/rubies/ruby-2.1.2/lib/ruby/2.1.0/webrick/httpserver.rb:94:in `run'
   /Users/sony/.rvm/rubies/ruby-2.1.2/lib/ruby/2.1.0/webrick/server.rb:295:in `block in start_thread'

I was able to get past the issue by manually adding the routes to route.rb:

Rails.application.routes.draw do
  devise_for :users, :controllers => {:omniauth_callbacks => "omniauth_callbacks", :sessions => 'sessions', :registrations => 'registrations' }
  devise_for :admin_users, ActiveAdmin::Devise.config
  ActiveAdmin.routes(self)
 devise_scope :user do
    get '/users/auth/stripe_connect/callback' => 'omniauth_callbacks#stripe_connect'
    post '/users/auth/stripe_connect/callback' => 'omniauth_callbacks#stripe_connect'
  end
end

Why did the callback route did not match until I added those routes manually?

xdmx commented 9 years ago

I'm having the same problem, I had to add the custom routes in the devise_scope as suggested by septerr

isaacsanders commented 9 years ago

@septerr @xdmx

Hey. I just found a bug in omniauth-oauth2 that might have been the cause of this bug. I am so sorry it has taken this long to get this resolved. I hope that a new version of omniauth-oauth2 will come out soon with my fix in it, then I will release another version of this gem, hopefully fixing all of your problems.

Thank you for your patience.

– Isaac

isaacsanders commented 9 years ago

@septerr @xdmx

Just released v2.8.0.

Please tell me if the issue persists. We can reopen from there.

marckohlbrugge commented 7 years ago

FWIW I run into the same problem with omniauth-oauth2 1.4.0. Adding the routes manually fixed it.

swrobel commented 6 years ago

Still occurring for me with omniauth-oauth2 1.5.0 & omniauth-stripe-connect 2.10.0. Adding the routes manually worked, of course.

My devise route, for reference

  devise_for :users,
             path_names: { sign_in: 'login', sign_out: 'logout' },
             controllers: {
               sessions: 'users/sessions',
               confirmations: 'users/confirmations',
               registrations: 'users/registrations',
               omniauth_callbacks: 'users/omniauth_callbacks',
               invitations: 'users/invitations'
             }