Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ruby on Rails Devise, how to change signin path when authentication fails?

I have recently implemented Devise into my new app, in conjunction with Omniauth, however I am unsure how to change the default signin path in Devise so that when I call:

user_authenticated!

It will redirect to the authentication controller page.
Any ideas how to do this?

EDIT: To better explain my problem ->

What I want is when a user tries to access a page that requires you to be logged in and then gets sent to users/sign_in by the user_authenticated before filter, however I want them to be redirected to /auth via user_authenticated! not users/sign_in.

like image 420
Hard-Boiled Wonderland Avatar asked Oct 24 '22 22:10

Hard-Boiled Wonderland


2 Answers

In your controller:

before_filter do
  authenticate_user! rescue redirect_to auth_url
end
like image 142
Andy Lindeman Avatar answered Oct 31 '22 10:10

Andy Lindeman


I don't think I really understood your problem, but to redirect to a particular page this should be the implementation

class ApplicationController < ActionController::Base
  private

  def after_sign_in_path_for(resource_or_scope)
    root_path
  end
end

if you want to have another url for the sign_in process, in your routes.rb

  devise_scope :user do
    get "sign_in", :to => "devise/sessions#new"
  end

and everything else you need at https://github.com/plataformatec/devise/wiki

hope it's useful

like image 42
ecoologic Avatar answered Oct 31 '22 09:10

ecoologic