Module: AuthHelper
- Included in:
- KindeSdk::AuthController
- Defined in:
- app/helpers/auth_helper.rb
Instance Method Summary collapse
-
#get_client ⇒ KindeSdk::Client?
Gets a Kinde client instance for the current session.
-
#logged_in? ⇒ Boolean
Checks if the user is currently logged in.
-
#refresh_session_tokens ⇒ Boolean
Attempts to refresh the session tokens.
-
#session_present_in? ⇒ Boolean
Checks if the session contains token data.
-
#set_session_tokens(tokens) ⇒ void
Sets up session tokens and user information after successful authentication.
-
#token_expired? ⇒ Boolean
Checks if the current token has expired.
Instance Method Details
#get_client ⇒ KindeSdk::Client?
Gets a Kinde client instance for the current session
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
33 34 35 |
# File 'app/helpers/auth_helper.rb', line 33 def logged_in? !token_expired? end |
#refresh_session_tokens ⇒ Boolean
Attempts to refresh the session tokens
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
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
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) user_info = client.oauth.get_user_profile_v2.to_hash # Validate user info before storing raise ArgumentError, "Invalid user info received" unless user_info[:id].present? session[:kinde_user] = { id: user_info[:id], email: user_info[:email], first_name: user_info[:given_name], last_name: user_info[:family_name] } end |
#token_expired? ⇒ Boolean
Checks if the current token has expired
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 |