Using Devise before_action: authenticate_user! doing nothing

I am trying to use login on all pages of my Rails 4 website. In ApplicationController I added before_action :authenticate_user! but he just does nothing. I tried to add the same before_action :authenticate_user! to another controller, and it works great.

Do I need to do something with ApplicationController in order to login in all actions (except registration / signing)?

+7
source share
2 answers

The actual code is used here:

 #app/controllers/application_controller.rb Class ApplicationController < ActionController::Base #Actions before_action :authenticate_user! #-> routes to the login / signup if not authenticated end 

The problem you probably have twice:

-

Make sure your other controllers inherit from application_controller :

 #app/controllers/other_controller.rb Class OtherController < ApplicationController ... end 

-

You somehow miss the before_action

If you use skip_before_action anywhere, you need to remove it. This will probably cause a problem with your authenticate_user! method authenticate_user! . To fix this, I would first test without any skip_before_action , and then see what works correctly.


A further note is that signin / signup does not matter. They all inherit from Devise controllers; they will be launched as needed.

+17
source

UPDATED 2019

In your routes.rb you could only mention the authenticated_user path, as shown below

 authenticated :user do root to: 'home#index', as: :root_app end 

You must also specify the unauthenticated_user path for it to work, or just the root path without the unauthenticated_user or authenticated_user

0
source

Source: https://habr.com/ru/post/969830/


All Articles