Sha256: 7815e3177875e9711bacd79d06bb5908163f79a645dfe80e13d230d58b808ec0

Contents?: true

Size: 892 Bytes

Versions: 1

Compression:

Stored size: 892 Bytes

Contents

module CurrentUser
  module Controller
    module Helpers
      def self.included(base)
        base.hide_action %w(authenticate_user! current_user sign_in sign_out signed_in? user_signed_in?)
        base.helper_method %w(current_user signed_in? user_signed_in?)
      end

      def authenticate_user!
        if current_user.nil?
          render_unauthorized
        end
      end

      def current_user
        user_id = session[::CurrentUser::USER_SESSION_KEY]

        ::User.find_by_id user_id
      end

      def sign_in(user)
        session[::CurrentUser::USER_SESSION_KEY] = user.id
      end

      def sign_out
        session[::CurrentUser::USER_SESSION_KEY] = nil
      end

      def signed_in?
        current_user.present?
      end

      private

      def render_unauthorized
        render :text => 'Unauthorized', :status => :unauthorized
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
current_user-0.1.0 lib/current_user/controller/helpers.rb