Sha256: 5b5b351e93f4451c7ec4eef0d85ef774d813572908c8f755dec02889a0aad5b1

Contents?: true

Size: 1.42 KB

Versions: 6

Compression:

Stored size: 1.42 KB

Contents

module Trestle
  module Auth
    module Controller
      module Authentication
        extend ActiveSupport::Concern

        included do
          helper_method :current_user, :logged_in?, :authentication_scope

          prepend_before_action :require_authenticated_user
          prepend_before_action :authenticate_user

          # Ensure that CSRF protection happens before authentication
          protect_from_forgery prepend: true
        end

      protected
        def authentication_backend
          @_authentication_backend ||= Trestle.config.auth.backend.new(controller: self, request: request, session: session, cookies: cookies)
        end

        def current_user
          authentication_backend.user
        end

        def logged_in?
          authentication_backend.logged_in?
        end

        def authenticate_user
          authentication_backend.authenticate
        end

        def require_authenticated_user
          logged_in? || login_required!
        end

        def login!(user)
          authentication_backend.login!(user)
        end

        def logout!
          authentication_backend.logout!
        end

        def login_required!
          authentication_backend.store_location(request.fullpath)
          redirect_to instance_exec(&Trestle.config.auth.login_url)
          false
        end

        def authentication_scope
          authentication_backend.scope
        end
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
trestle-auth-0.5.0 lib/trestle/auth/controller/authentication.rb
trestle-auth-0.5.0.pre2 lib/trestle/auth/controller/authentication.rb
trestle-auth-0.5.0.pre lib/trestle/auth/controller/authentication.rb
trestle-auth-0.4.4 lib/trestle/auth/controller/authentication.rb
trestle-auth-0.4.3 lib/trestle/auth/controller/authentication.rb
trestle-auth-0.4.2 lib/trestle/auth/controller/authentication.rb