Rails 5 Devise and OmniAuth Twitter setup redirects to sign_in form after authorising app

146 views Asked by At

I'm trying to install OmniAuth-Twitter on top of my Devise. I'm following this tutorial https://www.youtube.com/watch?v=X6tKAUOMzCs&t=42s but I'm getting an error that I can not find anywhere online. Actually it is not an error, after I Authorise app from Twitter, it redirects to my sign_in form!

omniauth.rb

Rails.application.config.middleware.use OmniAuth::Builder do
  provider :twitter, "ididputmykeyhere", "ididputmykeyhere"
end

routes.rb

Rails.application.routes.draw do
  devise_for :users, :controllers => { :omniauth_callbacks => "omniauth_callbacks" }

  resources :posts
  resources :categories
  root 'posts#index'
end

omniauth_callbakcs_controller.rb

class OmniauthCallbacksController < Devise::OmniauthCallbacksController
  def twitter
    # Show me what I get from Twitter
    raise request.env["omniauth.auth"].to_yaml

    @user = User.from_omniauth(request.env["omniauth.auth"])
    sign_in_and_redirect @user
  end
end

devise.rb

  config.omniauth :twitter, ENV["ididputmykeyhere"], ENV["ididputmykeyhere"]

user.rb

class User < ApplicationRecord
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable,
         :omniauthable, :omniauth_providers => [:twitter]

  has_many :posts
end

I placed user_twitter_omniauth_authorize_path link to my view.

  1. It redirects to Twitter Authorise page ✅
  2. I authorise app from Twitter ✅
  3. I'm expecting it to raise request.env["omniauth.auth"] but it redirects to localhost:3000/users/sign_in where I see sign in form ❌

It acts like skipping my omniauth_callbacks_controller where I will be writing saving to database codes, what am I missing? Thank you tons!


++++++++++++++++++++++ UPDATE ++++++++++++++++++++++

I just added def failure to my omniauth_callbakcs_controller.rb and I see that app is going for it.

  def failure

    raise request.env["omniauth.auth"].to_yaml

    flash[:error] = 'There was a problem signing you in. Please register or try signing in later.'
    redirect_to root_path
  end

enter image description here So, for some reason, the app is skipping my def Twitter

1

There are 1 answers

0
Designer On BEST ANSWER

It is soooooo weird but removing omniauth.rb file resolved the issue. I was defining the API key both in omniauth.rb and devise.rb files. Maybe defining in multiple files causes some sort of conflict.