I'm following the Michael Hartl RoR tutorial, but implementing Rollify and Authority along the way. I've never used Authority before and I am wondering if the following before_action is appropriate for Authority use
# app/controllers/users_controller.rb
class UsersController < ApplicationController
before_action :logged_in_user, only: [:edit, :update]
.
.
.
private
def user_params
params.require(:user).permit(:name, :email, :password,
:password_confirmation)
end
# Before filters
# Confirms a logged-in user.
def logged_in_user
unless logged_in?
flash[:danger] = "Please log in."
redirect_to login_url
end
end
end
would it be "good programming practice" to put the def logged_in_user
inside of the ApplicationAuthorizer class for future use?
No.
There is a difference between
Authentication
andAuthorization
:The difference is subtle but important - you'd expect authentication to happen before authorization, or at least independently.
A good analogy is authentication is when you get access to a secret party (password); authorization is which table you're able to sit at.
If you used one of the pre-rolled authentication systems (
Devise
orSorcery
), you'd have your authentication handled, providing you with such helpers asuser_signed_in?
etc.To answer your question, your current pattern will suffice, considering you've rolled your own authentication.
If you were using
Devise
, you'd want to use the following:--
What you're trying to do is evaluate the
@user.id
againstcurrent_user.id
: