0

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

Designer
  • 1,061
  • 1
  • 12
  • 26
  • I don't see any update on the User model. Check https://www.digitalocean.com/community/tutorials/how-to-configure-devise-and-omniauth-for-your-rails-application#https://www.digitalocean.com/community/tutorials/how-to-configure-devise-and-omniauth-for-your-rails-applicationstep-8-update-the-user-model for a nice example using Devise and Twitter omniauth. – Kevin Etore Oct 02 '17 at 12:39
  • Hi Kevin, I looked into that article as well, but again I got stuck at this point. I added **:omniauthable, :omniauth_providers => [:twitter]** user.rb file btw – Designer Oct 02 '17 at 12:43
  • Hello, you still need to find or create a User. After that in your `twitter` action you can call `sign_in_and_redirect @user` (define `@user` from `env["omniauth.auth"]`) – Kevin Etore Oct 02 '17 at 12:59
  • Thanks for your time mate! I added **def twitter ATuser = User.from_omniauth(request.env["omniauth.auth"]) sign_in_and_redirect ATuser end** to my omniauth_callbakcs_controller.rb, and it still redirects to sign_in form. – Designer Oct 02 '17 at 13:04

1 Answers1

0

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.

Designer
  • 1,061
  • 1
  • 12
  • 26