Search code examples
ruby-on-railsrubyauthenticationdeviseomniauth

Callback for Omniauth-Trello (RoR) - can't sign in


I am using RoR 5.2, devise and omniauth-trello gem. I can't sign in with Trello. I need to create ability to sign in without existing user with returned provider and uid: it should create user if it doesn't exist.

I have already added in config/routes.rb:

devise_for :users, controllers: { omniauth_callbacks: 'omniauth_callbacks' }

config/initializers/devise.rb:

config.omniauth :trello, "#{Rails.application.credentials.trello[:key]}", "#{Rails.application.credentials.trello[:secret]}"

config/initializers/omniauth.rb

Rails.application.config.middleware.use OmniAuth::Builder do
      provider :trello, Rails.application.credentials.trello[:key], Rails.application.credentials.trello[:secret],
               app_name: "Trello-Rooney", scope: 'read,write,account', expiration: 'never'
end

app/controllers/omniauth_callbacks_controller.rb

class OmniauthCallbacksController < Devise::OmniauthCallbacksController

      def trello
        @user = User.from_omniauth(request.env['omniauth.auth'])

        if @user.persisted?
          sign_in_and_redirect @user, event: :authentication
          set_flash_message(:notice, :success, kind: 'Trello') if is_navigational_format?
        end
      end

end

app/models/user.rb

class User < ApplicationRecord
  # Include default devise modules. Others available are:
  # :confirmable, :lockable, :timeoutable, :trackable and :omniauthable
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :validatable, :omniauthable, omniauth_providers: %i[trello]


  def self.from_omniauth(auth)
    user = User.where(provider: auth.provider, uid: auth.uid.to_s).first
    return user if user

    email = auth.info[:email]

    while email.nil?
      generated_email = "#{SecureRandom.base58(10)}@roonyx.trello"
      if User.where(email: generated_email).blank?
        email = generated_email
      end
    end

    user = User.where(email: email).first

    if user
      user.update(provider: auth.provider, uid: auth.uid)
    else
      password = Devise.friendly_token[0, 12]
      user = User.create!(email: email, password: password, password_confirmation: password, provider: auth.provider, uid: auth.uid)
    end
    user
  end


end

But when I am trying to sign in with Trello, I see this in console. Looks like it doesn't cause my callback. Can anyone help? Thank you in advance.

E, [2019-05-13T14:10:29.647241 #19958] ERROR -- omniauth: (trello) Authentication failure! service_unavailable: Net::HTTPFatalError, 500 "Internal Server Error"
[2019-05-13 14:10:29] (pida=19958)   INFO -- : Processing by OmniauthCallbacksController#failure as HTML
[2019-05-13 14:10:29] (pida=19958)   INFO -- :   Parameters: {"oauth_token"=>"47215df2b25b4fc089953da32acf0730", "oauth_verifier"=>"8a0b310f2afe98d0aebbd2073efc5b54"}
[2019-05-13 14:10:29] (pida=19958)   INFO -- : Redirected to http://localhost:3000/users/sign_in
[2019-05-13 14:10:29] (pida=19958)   INFO -- : Completed 302 Found in 1ms (ActiveRecord: 0.0ms)

Solution

  • The error is right there in your log: Authentication failure! service_unavailable: Net::HTTPFatalError, 500 "Internal Server Error"

    Your callback isn't being hit because you've inserted the OmniAuth::Builder middleware. You could try removing that and inspecting the params that are sent along with the callback.

    Once you're removed the middleware you can drop a byebug or a binding.pry at the top of your callback action. Once you're in the debugger check the value of request.env['omniauth.auth']. That should give you some insight as to what the problem is. Hard to say more without knowing more about the environment.