Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Different '/' root path for users depending if they are authenticated (using devise)

I'm writing a rails app in which I have an Editor and a Publication model. I'm using devise for editors authentication and, since an editor can't do anything as guest, I wrote a custom layout to use for the login page and I want that a guest user can see only the login page.

Now I'm trying to achieve the following behavior in my app but unsuccessfully:

require 'spec_helper'
require 'capybara/rails'

describe "Authentication" do

  describe "when logged in" do
    before(:each) do
      @editor = Factory(:editor, :password => 'secret')
      visit '/'
      fill_in 'Login', :with => @editor.login
      fill_in 'Password', :with => 'secret'
      click_button 'Sign in'
      page.should have_content('Signed in successfully.')
    end

    it "getting / should render publication page with no redirection" do
      visit '/'
      page.should_not have_content('Login')
      page.should have_content('Publications')
      # assert that there is no redirection
      page.current_path.should == '/'
    end

    it "visits the sign_in page should redirect to /" do
      visit '/editors/sign_in'
      page.should have_content('Publications')
      page.current_path.should == '/'
    end

  end

  describe "when not logged in" do
    it "getting / should not display the sign in warning" do
      visit '/'
      # I want to get rid of this message
      page.should_not have_content('You need to sign in or sign up before continuing.')
    end

    it "getting / should not redirect to the sign_in default page" do
      visit '/'
      page.should have_content('Login')
      # assert that there is no redirection
      page.current_path.should == '/'
    end

    it "getting the the sign_in default path works" do
      visit '/editors/sign_in'
      page.should have_content('Login')
      page.current_path.should == '/editors/sign_in'
    end

    it "login works and redirect me to the publications page (with /)" do
      @editor = Factory(:editor, :password => 'secret')
      visit '/'
      fill_in 'Login', :with => @editor.login
      fill_in 'Password', :with => 'secret'
      click_button 'Sign in'
      page.should have_content('Signed in successfully.')
      page.current_path.should == '/'
      page.should have_content('Publications')
    end
  end
end

The main issue is that I want to get rid of 'You need to sign in or sign up before continuing.' message when a guest user visit '/'.

I tried with hints taken from here and here but with no luck.

Any hint on how implement this with devise?

Thanks.

like image 390
Fabio Avatar asked Apr 20 '11 03:04

Fabio


2 Answers

I think you should use something like that

authenticated :user do
  root :to => 'users#index', as: :authenticated_root
end
root :to => 'welcome#index'

since rails4 doesn't allow routes with same name you need to specify as:

like image 162
wildmonkey Avatar answered Sep 30 '22 19:09

wildmonkey


This post explain how to achieve that:

authenticated :user do
  root :to => "main#dashboard"
end

root :to => "main#index"

Here is the pull request which implement this with some technical details

like image 27
Fabio Avatar answered Sep 30 '22 20:09

Fabio