Class: Macros::Auth::SignOut

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

Overview

Signout the given user. The user can be passed in the context.

Examples:

signout a user specified in the context (:scope or :model)

step Macros::Auth::SignOut()

signout the user passed in ctx

step Macros::Auth::SignOut(user_key: :impersonated_user)

Instance Method Summary collapse

Methods inherited from Base

register

Constructor Details

#initialize(user_key: nil) ⇒ Macro::Auth::SignOut

Returns step macro instance.

Parameters:

  • user_key (Hash) (defaults to: nil)

    ctx key under which is the user which we want to signout



15
16
17
18
# File 'lib/macros/auth/sign_out.rb', line 15

def initialize(user_key: nil)
  @user_key = user_key
  @user = nil
end

Instance Method Details

#call(ctx, warden:) ⇒ Object

Performs a step by signout the given user

Parameters:

  • ctx (Trailblazer::Skill)

    tbl context hash



22
23
24
25
26
27
28
29
30
31
# File 'lib/macros/auth/sign_out.rb', line 22

def call(ctx, warden:, **)
  @user = ctx[@user_key] if @user_key

  ctx[:scope] = scope(ctx)
  warden_user = warden.user(scope: ctx[:scope], run_callbacks: false)
  ctx[:model] = warden_user unless @user
  warden.logout(ctx[:scope])
  warden.clear_strategies_cache!(scope: ctx[:scope])
  true
end

#scope(ctx) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/macros/auth/sign_out.rb', line 33

def scope(ctx)
  resource_or_scope = if @user
                        @user
                      elsif ctx[:scope]
                        ctx[:scope]
                      elsif ctx[:model]
                        ctx[:model]
                      else
                        :user
                      end

  Devise::Mapping.find_scope!(resource_or_scope)
end