summaryrefslogtreecommitdiff
path: root/app/controllers/concerns/authenticatable.rb
blob: b0c8878dd409edd75f18c4c615696668518df1dd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
module Authenticatable
  extend ActiveSupport::Concern
  included do
    before_action :authenticate!
    helper_method :current_user, :current_session
  end

  protected

  def current_session(session_id = session[:user_id])
    @current_session ||= UserSession.authenticate(session_id)
  end

  def current_user
    @current_user ||= User.find(current_session.try(:user_id))
  rescue ActiveRecord::RecordNotFound
    nil
  end

  def authenticate!
    return if current_user.present?
    redirect_to new_session_path
  rescue
    redirect_to new_session_path
  end
end