Class: ApiKeys::Helpers::TokenSession
- Inherits:
-
Object
- Object
- ApiKeys::Helpers::TokenSession
- Defined in:
- lib/api_keys/helpers/token_session.rb
Overview
Helper for managing API key tokens in the session.
Secret keys can only be shown once (immediately after creation) because the plaintext token is not stored in the database. This helper provides a clean interface for the "show token once" pattern:
- After creating a key, store the token in the session
- On the success page, retrieve (and clear) the token
- If the user refreshes, the token is gone
Constant Summary collapse
- DEFAULT_SESSION_KEY =
Default session key for storing the token
:api_keys_new_token
Class Method Summary collapse
-
.available?(session, key: DEFAULT_SESSION_KEY) ⇒ Boolean
Check if a token is available in the session without removing it.
-
.retrieve_once(session, key: DEFAULT_SESSION_KEY) ⇒ String?
Retrieve and clear the token from the session.
-
.store(session, api_key, key: DEFAULT_SESSION_KEY) ⇒ String
Store an API key's token in the session for later retrieval.
Class Method Details
.available?(session, key: DEFAULT_SESSION_KEY) ⇒ Boolean
Check if a token is available in the session without removing it. Useful for conditional rendering.
62 63 64 |
# File 'lib/api_keys/helpers/token_session.rb', line 62 def available?(session, key: DEFAULT_SESSION_KEY) session[key].present? end |
.retrieve_once(session, key: DEFAULT_SESSION_KEY) ⇒ String?
Retrieve and clear the token from the session. Returns nil if no token is stored (e.g., page was refreshed).
52 53 54 |
# File 'lib/api_keys/helpers/token_session.rb', line 52 def retrieve_once(session, key: DEFAULT_SESSION_KEY) session.delete(key) end |
.store(session, api_key, key: DEFAULT_SESSION_KEY) ⇒ String
Store an API key's token in the session for later retrieval.
40 41 42 43 44 |
# File 'lib/api_keys/helpers/token_session.rb', line 40 def store(session, api_key, key: DEFAULT_SESSION_KEY) token = api_key.respond_to?(:token) ? api_key.token : api_key.to_s session[key] = token token end |