Module: ContactAuthHelper

Included in:
Mints::BaseApiController, Mints::BaseController
Defined in:
lib/mints/helpers/contact_auth_helper.rb

Instance Method Summary collapse

Instance Method Details

#mints_contact_login(email, password) ⇒ Object

Mints Contact Login.

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

[View source]

5
6
7
8
9
10
11
12
13
14
15
# File 'lib/mints/helpers/contact_auth_helper.rb', line 5

def (email, password)
  # Login in mints
  response = @mints_contact.(email, password)
  # Get session token from response
  session_token = response['session_token']
  id_token = response['contact']['contact_token'] ? response['contact']['contact_token'] : response['contact']['id_token']
  # Set a permanent cookie with the session token
  cookies.permanent[:mints_contact_session_token] = { value: session_token, secure: true, httponly: true }
  cookies.permanent[:mints_contact_id] = { value: id_token, secure: true, httponly: true }
  @contact_token = id_token
end

#mints_contact_logoutObject

Mints Contact Logout.

Destroy session from mints.cloud and delete local session cookie

[View source]

41
42
43
44
45
46
47
48
# File 'lib/mints/helpers/contact_auth_helper.rb', line 41

def mints_contact_logout
  # Logout from mints
  @mints_contact.logout
  # Delete local cookie
  cookies.delete(:mints_contact_session_token)
  cookies.delete(:mints_contact_id)
  @contact_token = nil
end

Mints contact Login.

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

[View source]

20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/mints/helpers/contact_auth_helper.rb', line 20

def (hash, redirect_in_error = false)
  # Login in mints
  response = @mints_contact.(hash)

  if response['data']
    # Get session token from response
    session_token = response['data']['session_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[:mints_contact_session_token] = { value: session_token, secure: true, httponly: true }
    cookies.permanent[:mints_contact_id] = { value: id_token, secure: true, httponly: true }
    @contact_token = id_token
    redirect_to response['data']['redirect_url'] ? response['data']['redirect_url'] : '/' if redirect_in_error
  else
    redirect_to '/' if redirect_in_error
  end
end

#mints_contact_signed_in?Boolean

Returns:

  • (Boolean)
[View source]

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

def mints_contact_signed_in?

  begin
    # Check status in mints
    # Check status in mints
    response = @mints_contact.status
    status = response['success'] || false
  rescue => e
    # Handle the client Unauthorized error
    # if mints response is negative delete the session cookie
    cookies.delete(:mints_contact_session_token)

    status = false
  end

  status
end