Sha256: f66231df8b89e7b356f6994c2a480673001a9ce0803f6e120797c8b473a7a95c

Contents?: true

Size: 1.33 KB

Versions: 7

Compression:

Stored size: 1.33 KB

Contents

# frozen_string_literal: true

module Macros
  class Auth
    # Sign in the given user. The user is passed in ctx[:model].
    #
    class SignIn < Macros::Base
      # @return [Macro::Auth::SignIn] step macro instance
      def initialize(condition: nil)
        @condition = condition
      end

      # Performs a step by signing in the given user
      # @param ctx [Trailblazer::Skill] tbl context hash
      def call(ctx, model:, warden:, **)
        return false if @condition && ctx[@condition].blank?

        options = ctx[:sign_in_options] || {}
        scope = Devise::Mapping.find_scope!(model)
        expire_data_after_sign_in(session: warden.session_serializer.session)

        if warden.user(scope) == model && !options.delete(:force)
          # Do nothing. User already signed in and we are not forcing it.
          true
        else
          options[:scope] = scope
          warden.set_user(model, options)
        end

        ctx[:current_user] = model
        true
      end

      def expire_data_after_sign_in(session:)
        # session.keys will return an empty array if the session is not yet loaded.
        # This is a bug in both Rack and Rails.
        # A call to #empty? forces the session to be loaded.
        session.empty?
        session.keys.grep(/^devise\./).each { |k| session.delete(k) }
      end
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
ff-tbl-macros-2.0.2 lib/macros/auth/sign_in.rb
ff-tbl-macros-2.0.1 lib/macros/auth/sign_in.rb
ff-tbl-macros-2.0.0 lib/macros/auth/sign_in.rb
ff-tbl-macros-1.0.2 lib/macros/auth/sign_in.rb
ff-tbl-macros-1.0.1 lib/macros/auth/sign_in.rb
ff-tbl-macros-1.0.0 lib/macros/auth/sign_in.rb
ff-tbl-macros-0.1.8 lib/macros/auth/sign_in.rb