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
# 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
  if response.key? 'data'
    session_token = response['data']['session_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]

49
50
51
52
53
54
55
56
57
# File 'lib/cxf/helpers/contact_auth_helper.rb', line 49

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]

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

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]

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

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]

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

def update_contact_tokens
  access_token = @cxf_user.get_client.session_token
  refresh_token = @cxf_user.get_client.refresh_token
  access_token_expires_at = @cxf_user.get_client.session_token_expires_at
  refresh_token_expires_at = @cxf_user.get_client.refresh_token_expires_at

  cookies[:cxf_contact_session_token] = { value: access_token, secure: true, httponly: true, expires: Time.at(access_token_expires_at) } if access_token && access_token_expires_at
  cookies[:cxf_contact_refresh_token] = { value: refresh_token, secure: true, httponly: true, expires: Time.at(refresh_token_expires_at) } if refresh_token && refresh_token_expires_at
end