Sha256: 235c858d8f35bf2d4c5142705e847ef40a524727598619d7ba66f70115ef3c6e

Contents?: true

Size: 1023 Bytes

Versions: 1

Compression:

Stored size: 1023 Bytes

Contents

# encoding: utf-8

module PagesCore
  module Authentication
    extend ActiveSupport::Concern

    included do
      before_action :start_authenticated_session
      after_action :finalize_authenticated_session
      helper_method :current_user, :logged_in?
    end

    # Returns the current user if logged in, or nil.
    attr_reader :current_user

    # Returns true if the user is logged in.
    def logged_in?
      current_user ? true : false
    end

    def authenticate!(user)
      user.mark_active!
      @current_user = user
    end

    def deauthenticate!
      @current_user = nil
      session[:current_user_id] = nil
    end

    protected

    def start_authenticated_session
      if session[:current_user_id]
        user = User.where(id: session[:current_user_id]).first
      end

      return unless user && user.can_login?

      authenticate!(user)
    end

    def finalize_authenticated_session
      return unless current_user
      session[:current_user_id] = current_user.id
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
pages_core-3.5.1 app/controllers/concerns/pages_core/authentication.rb