Ory Elements

repository·main·Indexed 41 days ago

https://github.com/ory/elements

A React-based component library for building authentication user interfaces—including login, registration, recovery, verification, and account management—that integrate with Ory identity services. It provides a set of themed components via the @ory/elements-react package and supports customization through CSS variables and component overrides.

Tokens
26.7K
Snippets
90
Records
133
Agent score
51%

What's inside Ory Elements

  1. Overview of Ory Elements

    main

    Ory Elements is a component library designed to simplify building authentication user interfaces (login, registration, account settings, verification, recovery, and consent) for Ory.

    Key features include:

    • Reduced Development Time: Pre-built components for complex flows like multi-factor authentication (MFA) and account recovery.
    • Customization: Components are themeable and replaceable.
    • Ecosystem Compatibility: Built for the React ecosystem, including support for Next.js.
    • Dynamic Adaptation: The UI automatically adapts to your Ory identity schema and flow configurations.
  2. Project Structure for Next.js App Router Example

    main

    The example uses the Next.js app/ directory structure to organize authentication and settings flows:

    • app/auth: Contains page files for all user authentication flows (Login, Registration, etc.).
    • app/settings: Contains the page file for the user settings flow.
    • app: Contains the root layout and the main landing page.
  3. Override components in Ory Elements

    main

    You can replace specific sub-components within an Ory Element by using the components prop. This allows you to inject custom UI while maintaining the flow logic. You can also use the useOryFlow hook inside your custom components to access the current flowType.

    import { Registration } from "@ory/elements-react/theme"
    
    function CustomCardHeader() {
      const { flowType } = useOryFlow()
      return <div>My Custom {flowType} Card header</div>
    }
    
    export default async function RegistrationPage({ searchParams }: PageProps) {
      const flow = await getOrCreateRegistrationFlow(searchParams)
    
      return (
        <Registration
          flow={flow}
          config={oryConfiguration}
          components={{
            Card: {
              CardHeader: CustomCardHeader,
            },
          }}
        />
      )
    }
  4. Theme Ory Elements using CSS variables

    main

    You can customize the appearance of Ory Elements by overriding CSS variables in your global stylesheet. The variables are organized into a hierarchy:

    1. Primitives: Base colors (e.g., --ui-100, --ui-500, --ui-black).
    2. Brand: Brand-specific colors mapped from primitives (e.g., --brand-500).
    3. Interface: Semantic groups for backgrounds, borders, and foregrounds (e.g., --interface-background-brand-primary).
    4. Specific Elements: Component-level overrides (e.g., --button-primary-background-default).

    Example of overriding primitives and brand colors:

    :root {
      /* primitives */
      --ui-100: #f1f5f9;
      --ui-500: #64748b;
      --ui-900: #0f172a;
      /* ... */
    
      /* brand */
      --brand-100: var(--ui-100);
      --brand-500: var(--ui-500);
      --brand-900: var(--ui-900);
      /* ... */
    }
  5. Key management and translation rules

    main

    To ensure the translation system works correctly, follow these rules:

    • Source of Truth: Always add or modify keys in en.json. Never modify keys directly in individual language files.
    • Placeholders: The system automatically preserves placeholders (e.g., {provider}, {clientName}, {contactSupportEmail}) and date formats (e.g., {used_at, date, long}).
    • HTML Tags: HTML tags (e.g., <a>, <strong>) are preserved during translation.
    • Key Order: The script maintains the key order defined in en.json. Extra keys found in language files are removed during updates.
    • Fallback: If a translation fails, the script adds an English fallback and logs the occurrence.
  6. Track analytics in Ory Elements using Matomo

    main

    You can send analytics events from Ory Elements authentication flows to a Matomo instance by utilizing the onSuccess, onValidationError, and onError event handler callbacks available on every Ory Elements flow component.

    To implement this, create a wrapper component that intercepts these callbacks and calls a tracking function (e.g., using Matomo's HTTP Tracking API via fetch). This allows you to track user behavior across flows like Login, Registration, and Recovery without adding heavy dependencies.

    // Example of wrapping a flow component to add analytics
    import OryFlowWithMatomo from "@/components/ory-flow-with-matomo"
    
    // In your Next.js page component:
    return <OryFlowWithMatomo flowType="login" flow={flow} />
  7. Create new translations using AI assistants

    main

    To generate a new language file from the English source, use an AI assistant with a specialized "Create New Translation Prompt".

    Requirements for the AI

    When using this method, ensure the prompt instructs the AI to:

    • Output Format: Provide a complete JSON file that can be saved directly as [language-code].json.
    • Translate ONLY values: Never translate the keys.
    • Preserve technical tokens: Keep all {placeholders}, <HTML tags>, and {date, format} strings exactly as they appear in the source.
    • Maintain structure: Keep the exact same key order and JSON hierarchy as en.json.
    • Natural phrasing: Use native, professional phrasing rather than literal translations.

    Best Practices

    • Attach en.json first, then the target language files.
    • Explicitly specify the full language name and its ISO code.
    • Request the complete JSON output first, followed by a summary of translation decisions or potential UI length issues.
    // Example of the Create New Translation Prompt to use with AI:
    I've attached en.json which contains English UI text for a web application. Please create a complete translation to [TARGET LANGUAGE] with the following requirements...