Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Devise authentication error when entering site's root first time

When user enters our web site's root or logs out, Devise authentication displays "You need to sign in or sign up before continuing" the first time. After page reload this message disappears.

Root of our web site is set to controller which has:

before_filter :authenticate_user!

We need this controller to be the root. How to eliminate this message?

like image 431
Paul Avatar asked Dec 12 '22 01:12

Paul


2 Answers

The correct solution would be to set an authenticated block as noted in the official wiki: https://github.com/plataformatec/devise/wiki/How-To:-Require-authentication-for-all-pages

  authenticated :user do
    root to: 'home#index', as: :authenticated_root
  end
  root to: redirect('/users/sign_in')

The scoped authenticated block will only be called once they are logged in, so you can happily direct users to a controller of your choice. In the example it redirects them to the sign in page when unauthenticated, but this could be any action in your routes.

like image 52
Jay Avatar answered Dec 15 '22 00:12

Jay


I did not find the solution better than defining

unauthenticated: ''

in /config/locales/devise.en.yml file.

like image 32
Paul Avatar answered Dec 15 '22 01:12

Paul