Module: ContactAuthHelper
- Included in:
- Cxf::BaseApiController, Cxf::BaseController
- Defined in:
- lib/cxf/helpers/contact_auth_helper.rb
Instance Method Summary collapse
-
#cxf_contact_login(email, password) ⇒ Object
Cxf Contact Login.
-
#cxf_contact_logout ⇒ Object
Cxf Contact Logout.
-
#cxf_contact_magic_link_login(hash, redirect_in_error = false) ⇒ Object
Cxf contact Login.
- #cxf_contact_signed_in? ⇒ Boolean
- #update_contact_tokens ⇒ Object
Instance Method Details
permalink #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 21 22 23 24 |
# File 'lib/cxf/helpers/contact_auth_helper.rb', line 7 def cxf_contact_login(email, password) # Login in cxf response = @cxf_contact.login(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 .permanent["cxf_contact_session_token"] = { value: session_token, secure: true, httponly: true } .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 |
permalink #cxf_contact_logout ⇒ Object
Cxf Contact Logout.
Destroy session from cxf.cloud and delete local session cookie
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 .delete("cxf_contact_session_token") .delete("cxf_contact_refresh_token") @contact_token = nil end |
permalink #cxf_contact_magic_link_login(hash, redirect_in_error = false) ⇒ Object
Cxf contact Login.
Starts a contact session in cxf.cloud and set a session cookie
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 cxf_contact_magic_link_login(hash, redirect_in_error = false) # Login in cxf response = @cxf_contact.magic_link_login(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 .permanent["cxf_contact_session_token"] = { value: session_token, secure: true, httponly: true } .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 |
permalink #cxf_contact_signed_in? ⇒ Boolean
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 .delete("cxf_contact_session_token") .delete("cxf_contact_refresh_token") status = false end status end |
permalink #update_contact_tokens ⇒ Object
[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 ["cxf_contact_session_token"] = { value: access_token, secure: true, httponly: true} if access_token ["cxf_contact_refresh_token"] = { value: refresh_token, secure: true, httponly: true } if refresh_token end |