-
Notifications
You must be signed in to change notification settings - Fork 1.1k
feat: add docs for client side events in elements #2498
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
jonas-jonas
wants to merge
4
commits into
master
Choose a base branch
from
jonas-jonas/clientSideEvents
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,102 @@ | ||
| --- | ||
| id: events | ||
| title: Submit and error events | ||
| sidebar_label: Lifecycle Events | ||
| --- | ||
|
|
||
| Ory Elements emits events for various actions, such as successful form submissions and errors that occur during form submissions. | ||
|
jonas-jonas marked this conversation as resolved.
Outdated
|
||
| You can listen to these events to perform custom actions in your application. | ||
|
jonas-jonas marked this conversation as resolved.
Outdated
|
||
|
|
||
| To attach an event listener, simply pass a callback function to the corresponding event prop of the form component. The available | ||
|
jonas-jonas marked this conversation as resolved.
Outdated
|
||
| events are: | ||
|
|
||
| ## `onSuccess` | ||
|
|
||
| The `onSuccess` event is emitted when a form is successfully submitted. You can use this event to track successful form | ||
| submissions. | ||
|
|
||
| ```tsx title="LoginForm.tsx" | ||
| <Login | ||
| flow={flow} | ||
| onSuccess={(event) => { | ||
| console.log("Flow submitted successfully!", event) | ||
| }} | ||
| /> | ||
| ``` | ||
|
|
||
| All success events emitted by Ory Elements contain the following properties: | ||
|
jonas-jonas marked this conversation as resolved.
Outdated
|
||
|
|
||
| | Property | Type | Description | | ||
| | ---------- | ---------- | -------------------------------------------------------------------- | | ||
| | `flowType` | `FlowType` | The type of the flow that was submitted (Login, Registration, etc.). | | ||
|
jonas-jonas marked this conversation as resolved.
|
||
| | `flow` | `Flow` | The current flow object. | | ||
| | `method` | `string` | The method, the user submitted (Password, Passkey, etc.) | | ||
|
jonas-jonas marked this conversation as resolved.
Outdated
|
||
|
|
||
| :::danger | ||
|
|
||
| The `flow` object may contain pontentially sensitive information, such as the user's email address or other personally | ||
| identifiable information (PII). Be cautious when logging or handling this data to avoid unintentional exposure of sensitive | ||
| information. | ||
|
|
||
| ::: | ||
|
|
||
| The **login** flow also contains the following additional properties: | ||
|
|
||
| | Property | Type | Description | | ||
| | --------- | --------- | ----------------------------------------- | | ||
| | `session` | `Session` | The session object of the logged in user. | | ||
|
jonas-jonas marked this conversation as resolved.
Outdated
|
||
|
|
||
| The **registration** flow also contains the following additional properties: | ||
|
|
||
| | Property | Type | Description | | ||
| | ---------- | ---------- | ---------------------------------------------------------------------------------------------------------------------- | | ||
| | `session` | `Session` | The session object of the registered user. Only included if Kratos is configured to issue a session after registration | | ||
|
jonas-jonas marked this conversation as resolved.
Outdated
|
||
| | `identity` | `Identity` | The identity object of the registered user. | | ||
|
jonas-jonas marked this conversation as resolved.
Outdated
|
||
|
|
||
| ## `onError` | ||
|
|
||
| The `onError` event is emitted when a form submission fails. The reason for this is exposed via the `error` property of the event. | ||
|
jonas-jonas marked this conversation as resolved.
Outdated
|
||
| You can use this event to track failed form submissions and the reasons for the failures. | ||
|
jonas-jonas marked this conversation as resolved.
Outdated
|
||
|
|
||
| Common reasons include: | ||
|
|
||
| - `flow_expired`: The flow has expired and a new flow needed to be created. | ||
|
jonas-jonas marked this conversation as resolved.
Outdated
|
||
| - `csrf_error`: The CSRF token is invalid or missing or the request was made from a different origin. This can happen if the user | ||
|
jonas-jonas marked this conversation as resolved.
Outdated
|
||
| has multiple tabs open or if the user is using a different browser. | ||
| - `flow_not_found`: The flow could not be found. | ||
| - `flow_replaced`: The flow was replaced by another flow. | ||
| - `consent_error`: An error occurred during the OAuth2 consent flow. | ||
|
|
||
| In addition, the `error` property may also contain a `body` property, which is the original error response from the Ory API. | ||
|
|
||
| ```tsx title="LoginForm.tsx" | ||
| <Login | ||
| flow={flow} | ||
| onError={(event) => { | ||
| console.error("Flow submission failed!", event.type, event.body) | ||
| }} | ||
| /> | ||
| ``` | ||
|
|
||
| ## `onValidationError` | ||
|
|
||
| The `onValidationError` event is emitted when a form submission fails due to validation errors. The event contains the current | ||
| flow object, which includes the validation messages. You can use this event to track validation errors and display them to the | ||
| user. | ||
|
|
||
| The actual error may be found in the `flow.ui.messages` property of the event object, or in the `flow.ui.nodes` property if the | ||
| validation error is related to a specific form field. | ||
|
|
||
| ```tsx title="LoginForm.tsx" | ||
| <Login | ||
| flow={flow} | ||
| onValidationError={(event) => { | ||
| console.error("Flow submission failed due to validation errors!", event.flow.ui.messages) | ||
| }} | ||
| /> | ||
| ``` | ||
|
|
||
| ## Examples | ||
|
|
||
| - A full example of handling events with Matomo can be found here in the | ||
|
jonas-jonas marked this conversation as resolved.
Outdated
|
||
| [Ory Elements with Matomo example](https://github.com/ory/elements/tree/main/examples/nextjs-app-router-matomo). | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This will exist after the feature is merged |
||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.