module Authenticable

Add authentication support to a controller

Public Instance Methods

authorize_user!() click to toggle source

Check for a current user. If not, render an error. Overrides devise implementation.

# File app/controllers/concerns/authenticable.rb, line 18
def authorize_user!
  if header_token.blank?
    # Anonymous users can't request current user
    render json: { errors: 'Login required' }, status: :forbidden
  else
    # Token may be invalid
    render json: { errors: 'Not authorized' }, status: :unauthorized unless user_signed_in?
  end
end
current_user() click to toggle source

Get user associated with auth token. Overrides devise implementation.

# File app/controllers/concerns/authenticable.rb, line 10
def current_user
  @current_user ||= User.find_by(auth_token: header_token)
end
user_signed_in?() click to toggle source

Check for a current user. Overrides device implementation.

# File app/controllers/concerns/authenticable.rb, line 31
def user_signed_in?
  current_user.present?
end