Search code examples
ruby-on-railsrubyruby-on-rails-5

warden.authenticate!(auth_options) returns nil


Rails 5.1. Hi. Currently out app has an authentication system implemented that we did and we are going to migrate to devise. I am at wits end here trying to get the devise log in to work.Here is code of customized sessions_controller

class Users::SessionsController < Devise::SessionsController
  include ExpireExistingToken

  def new
    super
  end

  def create
    self.resource = warden.authenticate!(auth_options)
    set_flash_message!(:notice, :signed_in)
    sign_in(resource_name, resource)
    yield resource if block_given?
    resource.update(language: Constant::SUPPORTED_LANGUAGES.key(params['locale'])) if params['locale'].present?
    resource.send_otp(force_expire: true) if resource.email_verified?
    respond_with resource, location: after_sign_in_path_for(resource)
    flash.delete(:notice)
  end

  def destroy
    session.delete(:is_otp_verified)
    super
  end
end
auth_options
{:scope=>:user, :recall=>"users/sessions#new"}

Started GET "/cable/" [WebSocket] for 127.0.0.1 at 2021-01-27 16:07:46 +0530
An unauthorized connection attempt was rejected
Processing by Users::SessionsController#new as HTML
Successfully upgraded to WebSocket (REQUEST_METHOD: GET, HTTP_CONNECTION: keep-alive, Upgrade, HTTP_UPGRADE: websocket)
Failed to upgrade to WebSocket (REQUEST_METHOD: GET, HTTP_CONNECTION: keep-alive, Upgrade, HTTP_UPGRADE: websocket)
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"wP84hj41ml3YlPaJpTBfnT985MSSpEkbaOvvQaNvxwesoSe19JWobaD4IQEZrm6btpWgJ5vldd9E0VzmsOIV5Q==", "user"=>{"email"=>"[email protected]", "password"=>"[FILTERED]"}, "button"=>"", "locale"=>"en"}
Finished "/cable/" [WebSocket] for 127.0.0.1 at 2021-01-27 16:07:46 +0530
Finished "/cable/" [WebSocket] for 127.0.0.1 at 2021-01-27 16:07:46 +0530
An unauthorized connection attempt was rejected
Finished "/cable/" [WebSocket] for 127.0.0.1 at 2021-01-27 16:07:46 +0530
  Rendering devise/sessions/new.html.haml within layouts/pre_authentication
Failed to upgrade to WebSocket (REQUEST_METHOD: GET, HTTP_CONNECTION: keep-alive, Upgrade, HTTP_UPGRADE: websocket)
  Rendered devise/sessions/new.html.haml within layouts/pre_authentication (3.7ms)
Finished "/cable/" [WebSocket] for 127.0.0.1 at 2021-01-27 16:07:46 +0530
  Rendered shared/_flash.haml (2.0ms)
  Rendered shared/_language_selector.html.haml (2.9ms)
Completed 200 OK in 159ms (Views: 30.1ms | ActiveRecord: 0.0ms)

The problem here is that warden.authenticate!(auth_options) returns nil. I checked my other, simpler application and authenticates behavior scans the database, regardless if email/password is correct. How do I get Warden/Devise for that matter to actually do a select statement to check the database out? Here is routes that are defined -

scope 'web' do
     scope '/:locale' do
       devise_for :users, controllers: {
         passwords: 'users/passwords',
         sessions: 'users/sessions',
         registrations: 'users/registrations'
       }
     end
   end

(If there is anything I can paste to help you guys I'll paste it)


Solution

  • Looks like the problem is with /cable action, that is somehow blocking the current authentication process or action to complete. You can verify this by commenting /cable from your app.