Max Rose-Collins
Max Rose-Collins

Reputation: 1924

Omniauth/Devise, authenticating with Twitter works but callback shows failure

I send a user to twitter to authorize my application to use their account.

If a user clicks sign in and authenticates correctly then they are sent to the failure method. I can still access their oauth_token and oauth_token_secret through the hash that is returned. (seems weird to me?)

Whether the user clicks sign in or cancel they are always sent back to the failure method.

Why do they not get sent to the success method when they authenticate correctly?

In my routes file I tried using

devise_for :users, :controllers => { :registrations => 'registrations',
                                     :omniauth_callbacks => "authentications" }

but it doesn't ever call the create method, always the failure method.

What am I doing wrong?

EDIT: authentications_controller.rb

class AuthenticationsController < Devise::OmniauthCallbacksController

  def create
    render :text => 'yes'
  end

  def failure
    render :text => request.env['omniauth.auth']
  end

  alias_method :twitter, :create
end

routes.rb

MyApp::Application.routes.draw do
  resources :authentications
  devise_for :users, :controllers => { :registrations => 'registrations', 
                                       :omniauth_callbacks => "authentications" }
  resources :users
end

Upvotes: 2

Views: 797

Answers (1)

Ashitaka
Ashitaka

Reputation: 19203

Since you are not using pure OmniAuth but you are also using Devise, things are a little bit different:

  1. You do not need to define the following route:

    match '/users/auth/:provider/callback' => 'authentications#create'

  2. Devise automatically calls methods whose names match the providers'. So if you are logging in with Twitter, you should have a twitter method. If you are logging in with Facebook, you should have a facebook method.

If you don't want this distinction, and you want all authentications to be directed to the same method (example: create), then you can add the following at the end of your controller:

# authentications_controller.rb
alias_method :facebook, :create
alias_method :twitter, :create

This way, you don't have to create twitter and facebook methods.

Upvotes: 2

Related Questions