View Source WebauthnComponents.TokenComponent (WebauthnComponents v0.5.3)
A LiveComponent for handling session tokens.
TokenComponent manages the client-side session token, allowing the parent LiveView to do the following:
- Redirect when a user is already signed in.
- Store a new token upon registration or authentication.
- Clear a token upon sign-out.
See USAGE.md for example code.
Assigns
@id(Optional) An HTML element ID.@token: A Base64-encoded session token to be stored in the client.
The parent LiveView may use Phoenix.LiveView.send_update/3 to set or clear a token in the client.
Clear a Token
send_update(TokenComponent, id: "token-component", token: :clear)Store a Token
send_update(TokenComponent, id: "token-component", token: base64_encoded_token)Events
The following events are handled internally by TokenComponent:
"token-exists": Sent by the client whensessionStoragecontains auserToken."token-stored": Sent by the client when a token has been stored insessionStorage."token-cleared": Sent by the client when a token has been cleared fromsessionStorage."error": Sent by the client when an error occurs.
Messages
{:token_exists, token: token}:tokenis a Base64-encoded token found by the client.- The parent LiveView may use this token to authenticate users.
{:token_stored, token: token}:tokenis a Base64-encoded token stored by the client.- The parent LiveView may compare the returned token against the token sent to the component to ensure there has been no tampering.
{:token_cleared}- A token has been cleared from the client.
{:error, payload}payloadcontains themessage,name, andstackreturned by the browser upon timeout or other client-side errors.
Summary
Functions
Callback implementation for Phoenix.LiveComponent.handle_event/3.
Callback implementation for Phoenix.LiveComponent.mount/1.
Callback implementation for Phoenix.LiveComponent.render/1.
Stores or clears a session token.
Functions
Callback implementation for Phoenix.LiveComponent.handle_event/3.
Callback implementation for Phoenix.LiveComponent.mount/1.
Callback implementation for Phoenix.LiveComponent.render/1.
Stores or clears a session token.
When a :token assign is received, this function will either clear or store the user's token.
- Assign
token: :clearto remove a user's token. - Assign a binary token (typically a base64-encoded crypto hash) to persist a user's token to the browser's
sessionStorage. - Invalid token assigns will be logged and the socket will be returned unchanged.