Victor Pudeyev
Victor Pudeyev

Reputation: 4539

Devise redirect to sign_in_path for unauthenticated users without using :database_authenticatable

I am using cancan, devise, and omniauth in a rails app. I'm quite happy with omniauth, and the method current_user is helpful. Ability class is also helpful. When a user registers, I only get his email from omniauth, and manually create a User for him with some parameters. So I am not using the devise way of creating users.

Consequently, I have no need for :database_authenticatable. Having it prevents me from saving a user without a password. Not having it however, makes users go to '/' instead of '/users/sign_in' when they hit an unauthorized resource.

I would like to not use :database_authenticatable, and have users go to '/users/sign_in' if they are unauthorized. How would I do that?

Note: almost all my controllers have fine-grained authenticate_user! beforefilter, for example:

class UsersController < ApplicationController

    before_filter :authenticate_user!, :except => [:index, :index_small, :show,
     :new , :sign_in ]

so I'm not sure if I can do anything with before_filter :authenticate_user! in application_controller. If I can get rid of this filter on all the controllers, it would be nice also.

Upvotes: 1

Views: 3123

Answers (1)

Victor Pudeyev
Victor Pudeyev

Reputation: 4539

Got it!

In any helper, overwrite sign_in_path

 module UsersHelper
  def sign_in_path
    '/users/sign_in'
  end
 end

also, most importantly, before_filter :authenticate_user! should be removed completely from controllers, if cancan is used.

Upvotes: 2

Related Questions