Module: UserAuthHelper

Included in:
Mints::AdminBaseController, Mints::BaseApiController
Defined in:
lib/mints/helpers/user_auth_helper.rb

Instance Method Summary collapse

Instance Method Details

#mints_user_login(email, password) ⇒ Object

Mints user Login.

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


20
21
22
23
24
25
26
27
# File 'lib/mints/helpers/user_auth_helper.rb', line 20

def (email, password)
  # Login in mints
  response = @mints_user.(email, password)
  # Get session token from response
  session_token = response['api_token']
  # Set a permanent cookie with the session token
  cookies[:mints_user_session_token] = { value: session_token, secure: true, httponly: true, expires: 1.day }
end

#mints_user_logoutObject

Mints user Logout.

Destroy session from mints.cloud and delete local session cookie


47
48
49
50
51
52
# File 'lib/mints/helpers/user_auth_helper.rb', line 47

def mints_user_logout
  # Logout from mints
  # @mints_user.logout
  # Delete local cookie
  cookies.delete(:mints_user_session_token)
end

Mints user Login.

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


32
33
34
35
36
37
38
39
40
41
42
# File 'lib/mints/helpers/user_auth_helper.rb', line 32

def (hash)
  # Login in mints
  response = @mints_user.(hash)
  if response['data']
    # Set a cookie with the session token
    cookies[:mints_user_session_token] = { value: response['data']['api_token'], secure: true, httponly: true,  expires: 1.day }
    redirect_to response['data']['redirect_url'] ? response['data']['redirect_url'] : '/'
  else
    redirect_to '/'
  end
end

#mints_user_signed_in?Boolean

Returns:

  • (Boolean)

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

def mints_user_signed_in?
  begin
    # Check status in mints
    response = @mints_user.me['data']
  rescue => e
    # Handle the client Unauthorized error
    # if mints response is negative delete the session cookie
    cookies.delete(:mints_user_session_token)
    response = nil
  end

  response
end