Learn how to create custom TypeScript SPA with Ory Elements
Ory Elements is a component library designed to make building login, registration, and account pages for Ory quick and easy. Ory Elements is modular and customizable, which means that you can use only the views you need and style them to match your implementation's design.
The UI created with Ory Elements changes dynamically to adapt to your Ory Network configuration. For example, the sign-up form changes to after you edit the Identity Schema without the need for any additional action. The same is true for other options available in Ory Network, such as social sign-in connections or passwordless login.
The two available examples provide a closer look at integrating Ory Elements with Preact and React TypeScript single-page applications (SPA). By following the instructions you will:
- Get the example app code from the Ory Elements repository.
- Run the application on your machine.
- Use Ory CLI to connect the application running on your machine to your Ory Network project's APIs.
- Have a closer look at the code that's behind the dynamic Ory Elements UI.
Prerequisites
Before you start, make sure to:
- Create an Ory Network project.
- Install Ory CLI - a tool that will help you connect the app running on your machine to Ory Network APIs.
Run the sample application
Follow these steps to get the sample application code and run it on your machine:
Clone the example and prepare the code to run it:
- React
- Preact
git clone --depth 1 git@github.com:ory/elements.git
cd elements
npm run initialize
cd examples/react-spagit clone --depth 1 git@github.com:ory/elements.git
cd elements
npm run initialize
cd examples/preact-spaExport environment variable:
- Unix systems
- Windows Powershell
export VITE_ORY_SDK_URL=http://localhost:4000
$Env:VITE_ORY_SDK_URL = "https://{your-project-slug-here}.projects.oryapis.com"
Start the application locally:
# Start the app on port 3000
npm run dev -- --port 3000In a new terminal window, run Ory Tunnel (part of the Ory CLI) to connect the application to your Ory Network project:
# Run Ory Tunnel to connect to Ory APIs.
# To get your project slug, go to 'Access & APIs' in Ory Console
# at https://console.ory.sh/
ory tunnel http://localhost:3000 --project <project-slug> --devIn your browser, go to http://localhost:3000 to access the application. From there, you can try registering and signing-in using the Ory Elements-powered pages.
Closer look at the code
With the application running, let's have a closer look at the code and the way the application integrates with Ory Elements and Ory SDK.
Ory SDK instance
The sdk.ts
file is responsible for creating a new instance of the Ory SDK. This instance is used to interact with Ory APIs.
- React
- Preact
Application entry point
This is the main entry point for the application. This file contains routes and themes for other pages used by the app. The
application is wrapped with Ory Elements using <ThemeProvider />
. You can change the theme colors by passing in a Theme
object
to the themeOverrides
property on the <ThemeProvider />
component.
- React
- Preact
Landing page
This file defines the landing page of the application. If you want to protect access to an app, set up authentication to secure
this page. In this implementation, the Dashboard
component checks if the user has a session. If the user doesn't have a session,
they are redirected to the login page. Additionally, the logout URL is initialized, which allows for a functional Log out
button.
- React
- Preact
Login and registration pages
The login.tsx
and register.tsx
pages contain components that interact with Ory APIs to allow registering accounts and
performing the login flow.
These pages are dynamic and will show relevant fields based on your Ory Network project configuration. To see the pages change, edit your project's Identity Schema or try changing adjusting project settings and enable passwordless flows.
Registration
The registration page, defined in the register.tsx
file:
- Gets the registration flow (if it exists in the URL) or fallback to start the registration flow to get the
flow
data on page load. - Submits the registration form data the user provides when they click the Submit button,
To learn more about the self-service registration flow, read this document
- React
- Preact
Login
The login page, defined in the login.tsx
file:
- Gets the login flow (if it exists in the URL) or falls back to start the registration flow to get the
flow
data on page load. - Submits the login form data the user provides when they click the Submit button,
To learn more about the self-service login flow, read this document
- React
- Preact
Settings page
The settings.tsx
file defines the page of the application where users can change their account settings. This page changes
dynamically based on the configuration of the Ory Network project.
For example, if you enable passwordless flows, users see the option to register a new hardware key. If passwordless flows are disabled, the option to add hardware keys is not visible in the UI.
- React
- Preact
Verification page
The verification.tsx
file defines the page of the application where users verify newly created accounts.
- React
- Preact
Recovery page
The recovery.tsx
file defines the page displayed to user that try to recover their accounts after forgetting a password or
losing access to a second authentication factor.
- React
- Preact
Learn more
To learn more about Ory Elements, contribute to the project, and integrate with your application, visit the project's repository.