class Macros::Auth::SignIn

Sign in the given user. The user is passed in ctx.

Public Class Methods

new(condition: nil) click to toggle source

@return [Macro::Auth::SignIn] step macro instance

# File lib/macros/auth/sign_in.rb, line 9
def initialize(condition: nil)
  @condition = condition
end

Public Instance Methods

call(ctx, model:, warden:, **) click to toggle source

Performs a step by signing in the given user @param ctx [Trailblazer::Skill] tbl context hash

# File lib/macros/auth/sign_in.rb, line 15
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
expire_data_after_sign_in(session:) click to toggle source
# File lib/macros/auth/sign_in.rb, line 34
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