Sha256: 0e05a68bd5792857444669b1b0e413d8a4ddfb0b5a37cf9e405dc48be16a2bf5

Contents?: true

Size: 1.67 KB

Versions: 1

Compression:

Stored size: 1.67 KB

Contents

module Passwd::Rails
  module ActionControllerExt
    extend ActiveSupport::Concern

    included do
      helper_method :current_user, :signin?
    end

    private

      def current_user
        return @current_user if instance_variable_defined?(:@current_user)

        @current_user = _auth_class.find_by(id: session[_auth_key])
      end

      def signin?
        current_user.present?
      end

      def signin(user)
        if user.present?
          @current_user = user
          session[_auth_key] = user&.id
        end

        user.present?
      end

      def signout
        session[_auth_key] = nil
        @current_user = nil
      end

      def redirect_to_referer_or(path, options = {})
        redirect_to session[:signin_referer].presence || path, **options
      end

      def require_signin
        return if signin?

        path = _signin_path
        raise UnauthorizedAccess unless path

        session[:signin_referer] = request.fullpath
        redirect_to path
      end

      def passwd_auth_class
        nil
      end

      def passwd_auth_key
        nil
      end

      def passwd_signin_path
        nil
      end

      def _auth_class
        (Rails.application.config.passwd.auth_class || passwd_auth_class || :User).to_s.constantize
      end

      def _auth_key
        Rails.application.config.passwd.session_key || passwd_auth_key || :user_id
      end

      def _signin_path
        name = Rails.application.config.passwd.signin_path || passwd_signin_path || :signin_path
        _url_helpers.respond_to?(name) ? _url_helpers.public_send(name) : nil
      end

      def _url_helpers
        Rails.application.routes.url_helpers
      end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
passwd-0.4.0 lib/passwd/rails/action_controller_ext.rb