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

[View source]

7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# 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'
    session_token = response['data']['access_token']
    refresh_token = response['data']['refresh_token']
    id_token = response['data']['contact_token'] || response['data']['id_token'] || nil
  end

  # Set a permanent cookie with the session token
  cookies.permanent["cxf_contact_session_token"] = { value: session_token, secure: true, httponly: true }
  cookies.permanent["cxf_contact_refresh_token"] = { value: refresh_token, secure: true, httponly: true }
  # cookies.permanent[:cxf_contact_id] = { value: id_token, secure: true, httponly: true }
  @contact_token = id_token
end

#cxf_contact_logoutObject

Cxf Contact Logout.

Destroy session from cxf.cloud and delete local session cookie

[View source]

52
53
54
55
56
57
58
59
60
# File 'lib/cxf/helpers/contact_auth_helper.rb', line 52

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_session_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

[View source]

29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/cxf/helpers/contact_auth_helper.rb', line 29

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

  if response['data']
    # Get session token from response
    session_token = response['data']['session_token']
    refresh_token = response['data']['refresh_token']
    # id_token = response['data']['contact']['contact_token'] ? response['data']['contact']['contact_token'] : response['data']['contact']['id_token']
    # Set a permanent cookie with the session token
    cookies.permanent["cxf_contact_session_token"] = { value: session_token, secure: true, httponly: true }
    cookies.permanent["cxf_contact_refresh_token"] = { value: refresh_token, secure: true, httponly: true }
    # cookies.permanent[:cxf_contact_id] = { value: id_token, secure: true, httponly: true }
    # @contact_token = id_token
    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)
[View source]

62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/cxf/helpers/contact_auth_helper.rb', line 62

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

#update_contact_tokensObject

[View source]

80
81
82
83
84
85
86
# File 'lib/cxf/helpers/contact_auth_helper.rb', line 80

def update_contact_tokens
  access_token = @cxf_contact.get_client.session_token
  refresh_token = @cxf_contact.get_client.refresh_token

  cookies["cxf_contact_session_token"] = { value: access_token, secure: true, httponly: true} if access_token
  cookies["cxf_contact_refresh_token"] = { value: refresh_token, secure: true, httponly: true } if refresh_token
end