Module: ContactAuthHelper

Included in:
Cxf::BaseApiController, Cxf::BaseController
Defined in:
lib/cxf/helpers/contact_auth_helper.rb

Instance Method Summary collapse

Instance Method Details

#cxf_contact_login(email, password) ⇒ Object

Cxf Contact Login.

Starts a contact session in cxf.cloud and set a session cookie



7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/cxf/helpers/contact_auth_helper.rb', line 7

def (email, password)
  # Login in cxf
  response = @cxf_contact.(email, password)

  # Get session token from response
  return response unless response.is_a? Hash
  if response.key? 'data'
    id_token = response['data']['contact_token'] || response['data']['id_token'] || nil
  end

  @contact_token = id_token

  response
end

#cxf_contact_logoutObject

Cxf Contact Logout.

Destroy session from cxf.cloud and delete local session cookie



39
40
41
42
43
44
45
46
47
# File 'lib/cxf/helpers/contact_auth_helper.rb', line 39

def cxf_contact_logout
  # Logout from cxf
  @cxf_contact.logout
  # Delete session token and keep the contact token id
  # Never delete the cxf_contact_id cookie to avoid the creation of ghosts
  cookies.delete("cxf_contact_access_token")
  cookies.delete("cxf_contact_refresh_token")
  @contact_token = nil
end

Cxf contact Login.

Starts a contact session in cxf.cloud and set a session cookie



25
26
27
28
29
30
31
32
33
34
# File 'lib/cxf/helpers/contact_auth_helper.rb', line 25

def (hash, redirect_in_error = false)
  # Login in cxf
  response = @cxf_contact.(hash)

  if response['data']
    redirect_to response['data']['redirect_url'] || '/' if redirect_in_error
  else
    redirect_to '/' if redirect_in_error
  end
end

#cxf_contact_signed_in?Boolean

Returns:

  • (Boolean)


49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/cxf/helpers/contact_auth_helper.rb', line 49

def cxf_contact_signed_in?

  begin
    # Check status in cxf
    # Check status in cxf
    response = @cxf_contact.status
    status = response['success'] || false
  rescue => e
    # Handle the client Unauthorized error
    # if cxf response is negative delete the session cookie
    cookies.delete("cxf_contact_session_token")
    cookies.delete("cxf_contact_refresh_token")
    status = false
  end

  status
end

#sync_contact_cookiesObject



67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/cxf/helpers/contact_auth_helper.rb', line 67

def sync_contact_cookies
  response_cookies = @cxf_contact.get_client.response_cookies

  response_cookies.each do |key, cookie|
    cookies[cookie['name']] = {
      value: cookie['value'],
      expires: cookie['expires'] ? Time.parse(cookie['expires']) : nil,
      path: cookie['path'] || '/',
      secure: cookie['secure'] || false,
      httponly: cookie['httponly'] || false,
      same_site: (cookie['samesite'] || 'Lax').downcase.to_sym
    }
  end
end