1

Rails application that uses devise and omniauth for authentication. Only facebook users are permitted. Facebook login working well, omniauth call backs working well.

My Gemfile include the basic, among other:

gem 'devise'
gem 'omniauth'
gem 'omniauth-facebook'

In my "books" view controller, i've added devise authentication:

class BooksController < ApplicationController
before_filter :authenticate_user!

The problem: when user authenticate_user! fails, devise is redirecting to a sign_in page (where user type username, e-mail, password, confirmation). I do not want this behavior as I only want facebook users. I need devise to redirect to facebook login if authenticate_user fails.

How to make devise redirects to facebook login when user is not authenticated?

Oren Pinsky
  • 419
  • 3
  • 19
  • 1
    Please look at this almost same question http://stackoverflow.com/questions/5832631/devise-redirect-after-login-fail – Santosh Nov 23 '12 at 17:21

3 Answers3

4

Best way will be to use a custom devise failure app and override the redirect function.

Something like below will work :

  1. Create a initializer, something like custom_failure_app.rb.
  2. Create a class CustomFailureApp which will inherit the Devise::FailureApp
  3. Override the redirect function.

    class CustomFailureApp < Devise::FailureApp
    
      # will be called wen some failure occurs. 
      # Like unauthorized, session_expiry etc
      def redirect 
        message = warden.message || warden_options[:message]
        if message == :timeout
            # session expires
        else
            # unauthorized
            # redirect_to "facebook.com"
        end
      end
    end
    
  4. Add another initializer, something like devise.rb and put the following code in it.

    Devise.setup do |config|
      config.warden do |manager|
        manager.failure_app = CustomFailureApp
      end
    end
    
Alex.Bullard
  • 5,533
  • 2
  • 25
  • 32
Ramandeep Singh
  • 5,063
  • 3
  • 28
  • 34
  • Thanks - it worked. I've also had to add the initializer code config.warden do |manager| manager.failure_app = CustomFailure end and the lib directory scanning config.autoload_paths += %W(#{config.root}/lib) – Oren Pinsky Nov 23 '12 at 17:58
  • I can't get this to work, I still get redirected to the login page – Chris Edwards Jan 16 '13 at 15:57
0

You can try adding

match '/auth/failure', :to => 'your_controller#failure'

to your routes.rb and then redirecting from within the failure method inside your controller.

toashd
  • 1,002
  • 8
  • 10
  • Thanks - but it not working as expected. When authenticate_user! fails (because user is not logged in yet), it is redirected to /users/sign_in and /auth/failure route is not called – Oren Pinsky Nov 23 '12 at 17:20
0

Ryan Bates gives great screencast http://railscasts.com/episodes/360-facebook-authentication how to use "omniauth-facebook" gem. And you can use this fix to sign in with standart Devise methods: Sign in to Devise using omniauth-facebook

Community
  • 1
  • 1
Gediminas Šukys
  • 7,101
  • 7
  • 46
  • 59