Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
102 changes: 102 additions & 0 deletions docs/elements/guides/20_events.mdx
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.
Comment thread
jonas-jonas marked this conversation as resolved.
Outdated
Comment thread
jonas-jonas marked this conversation as resolved.
Outdated
You can listen to these events to perform custom actions in your application.
Comment thread
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
Comment thread
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:
Comment thread
jonas-jonas marked this conversation as resolved.
Outdated

| Property | Type | Description |
| ---------- | ---------- | -------------------------------------------------------------------- |
| `flowType` | `FlowType` | The type of the flow that was submitted (Login, Registration, etc.). |
Comment thread
jonas-jonas marked this conversation as resolved.
| `flow` | `Flow` | The current flow object. |
| `method` | `string` | The method, the user submitted (Password, Passkey, etc.) |
Comment thread
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. |
Comment thread
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 |
Comment thread
jonas-jonas marked this conversation as resolved.
Outdated
| `identity` | `Identity` | The identity object of the registered user. |
Comment thread
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.
Comment thread
jonas-jonas marked this conversation as resolved.
Outdated
You can use this event to track failed form submissions and the reasons for the failures.
Comment thread
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.
Comment thread
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
Comment thread
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
Comment thread
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).
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will exist after the feature is merged

Loading
Loading