Class: Macros::Auth::SignIn

Inherits:
Base
  • Object
show all
Defined in:
lib/macros/auth/sign_in.rb

Overview

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

Instance Method Summary collapse

Methods inherited from Base

register

Constructor Details

#initialize(condition: nil) ⇒ Macro::Auth::SignIn

Returns step macro instance.



9
10
11
# File 'lib/macros/auth/sign_in.rb', line 9

def initialize(condition: nil)
  @condition = condition
end

Instance Method Details

#call(ctx, model:, warden:) ⇒ Object

Performs a step by signing in the given user

Parameters:

  • ctx (Trailblazer::Skill)

    tbl context hash



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# 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)
  (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:) ⇒ Object



34
35
36
37
38
39
40
# File 'lib/macros/auth/sign_in.rb', line 34

def (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