Module: AuthHelper

Included in:
KindeSdk::AuthController
Defined in:
app/helpers/auth_helper.rb

Instance Method Summary collapse

Instance Method Details

#get_clientKindeSdk::Client?

Gets a Kinde client instance for the current session

Returns:



84
85
86
87
# File 'app/helpers/auth_helper.rb', line 84

def get_client
  return nil unless session[:kinde_token_store].present?
  KindeSdk.client(session[:kinde_token_store])
end

#logged_in?Boolean

Checks if the user is currently logged in

Returns:

  • (Boolean)

    true if the user is logged in and token is valid



33
34
35
# File 'app/helpers/auth_helper.rb', line 33

def logged_in?
  !token_expired?
end

#refresh_session_tokensBoolean

Attempts to refresh the session tokens

Returns:

  • (Boolean)

    true if refresh was successful



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'app/helpers/auth_helper.rb', line 62

def refresh_session_tokens
  return false unless session[:kinde_token_store].present?
  
  # Create token store from session data
  token_store = KindeSdk::TokenStore.new(session[:kinde_token_store])
  
  # Attempt to refresh tokens
  new_tokens = KindeSdk::TokenManager.refresh_tokens(token_store, session)
  if new_tokens
    set_session_tokens(new_tokens)
    true
  else
    false
  end
rescue StandardError => e
  Rails.logger.error("Error refreshing tokens: #{e.message}")
  session.delete(:kinde_token_store)
  false
end

#session_present_in?Boolean

Checks if the session contains token data

Returns:

  • (Boolean)

    true if session contains token data



39
40
41
# File 'app/helpers/auth_helper.rb', line 39

def session_present_in?
  session[:kinde_token_store].present?
end

#set_session_tokens(tokens) ⇒ void

This method returns an undefined value.

Sets up session tokens and user information after successful authentication

Parameters:

  • tokens (Hash)

    The authentication tokens received from Kinde

Raises:

  • (ArgumentError)


5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'app/helpers/auth_helper.rb', line 5

def set_session_tokens(tokens)
  # Create token store from tokens
  token_store = KindeSdk::TokenManager.create_store(tokens)
  
  # Store minimal token data in session
  session[:kinde_token_store] = {
    access_token: token_store.bearer_token,
    refresh_token: token_store.tokens[:refresh_token],
    expires_at: token_store.expires_at
  }
  
  # Get and store minimal user info
  client = KindeSdk.client(tokens)
   = client.oauth..to_hash
  
  # Validate user info before storing
  raise ArgumentError, "Invalid user info received" unless [:id].present?
  
  session[:kinde_user] = {
    id: [:id],
    email: [:email],
    first_name: [:given_name],
    last_name: [:family_name]
  }
end

#token_expired?Boolean

Checks if the current token has expired

Returns:

  • (Boolean)

    true if token is expired or invalid



45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'app/helpers/auth_helper.rb', line 45

def token_expired?
  return true unless session[:kinde_token_store].present?
  
  client = get_client
  return true unless client
  
  client.token_expired?
rescue JWT::DecodeError => e
  Rails.logger.error("JWT decode error: #{e.message}")
  true
rescue StandardError => e
  Rails.logger.error("Error checking token expiration: #{e.message}")
  true
end