Building Your ApplicationFrontend

Frontend

On this page

Introduction

Keyring is headless. It renders pages with props and answers JSON requests, and your app owns every page. The browser package, @keyringjs/keyring, handles WebAuthn, enrolling sign-in methods, and the Vue and React bindings:

Terminal
npm install @keyringjs/keyring

The crate answers the same requests with the same JSON as the PHP package, so the package works with it unchanged, and everything the PHP frontend docs show in TypeScript, Vue and React holds. The contract suite simulates the package's requests (see contract/src/browser.ts) rather than running it, so check the package against your application as well. The starter kit uses it for every page, in React.

What differs is the server side: how pages are rendered, and how props are shared. This page covers that, and links to the PHP page for the rest.

Building the Pages

With the crate's inertia feature, and laravel-rust's Inertia middleware in the web group, Keyring renders the components auth/login, auth/challenge and so on, with the PHP package's props:

Rust
use laravel_keyring::{KeyringConfig, KeyringServiceProvider}; laravel::Application::configure()    .with_middleware(|middleware, _| {        middleware.web(|web| {            web.append(laravel::inertia::middleware(HandleInertiaRequests));        });    });

HandleInertiaRequests is your Inertia middleware, which shares props with every page (see Flash Messages and Alerts). Keyring only renders through Inertia for a request that ran it, where the PHP package uses Inertia whenever it's installed, and a component that doesn't exist isn't checked for. Without the feature, Keyring renders the views auth.login, auth.challenge and so on, given the props as their data. pages.page() renames a page, and pages.render_using() answers it your own way (see Pages).

The starter kit's resources/js/pages/auth has every page Keyring renders, and resources/js/pages/settings the settings pages for sign-in methods, sessions, devices, the password and the email address.

As in the PHP docs: email-verification, email-change and device-report each get their link's values as props, and post them back in the body.

The Challenge Page

As in the PHP docs: useChallenge() from @keyringjs/keyring/vue or /react does the work, for the challenge and confirm pages alike.

Vue

See the PHP docs.

React

See the PHP docs, and the starter kit's resources/js/pages/auth/challenge.tsx.

Factor Components

Each factor key needs a component in your factor map, as in the PHP docs. The starter kit's is resources/js/keyring/factors.tsx: add a line there for a factor of your own, or for a key in webauthn.keys.

Signing In With a Passkey

usePasskeyLogin() works as in the PHP docs, with GET /login/passkey/options and POST /login/passkey.

Signing In With an Identity Provider

The login page's providers prop, and signInWithProvider(), work as in the PHP docs. See Single Sign-On.

Challenge Helpers Without Vue or React

@keyringjs/keyring/inertia works as in the PHP docs.

Managing Authenticators

enroll(), beginEnrollment(), confirmEnrollment(), renameAuthenticator(), makeDefault() and removeAuthenticator() work as in the PHP docs. The authenticator routes all use {factor} in the crate, but the URLs are the PHP package's, so the helpers don't change. Pass the page what laravel_keyring::sign_in_methods(&user) gives (see Your Settings Page).

Frozen Changes

proveWithKey() works as in the PHP docs.

Sessions

The sessions helpers work as in the PHP docs. The starter kit's sessions page loads GET /user/sessions and GET /user/devices itself.

Handling Step-Up in JavaScript

As in the PHP docs: a JSON request that needs a fresh verification gets a 423 with a redirect to the confirm page. Put RequireFreshVerification on the page's own route to ask up front, as the starter kit's settings pages do.

JSON Requests

json() works as in the PHP docs. A request wants JSON when it expects JSON and isn't an Inertia visit, as http::wants_json() says.

WebAuthn Helpers

As in the PHP docs. The crate never offers EdDSA, so the browser never creates an Ed25519 key for it.

Changing Routes

If you set prefix, tell the helpers with configureRoutes({ prefix }), as in the PHP docs.

Flash Messages and Alerts

Keyring's non-JSON responses flash a status slug to the session, and any data that came with it encrypted. Share them from your Inertia middleware's share(), with the "was this you?" alerts and the impersonation banner:

Rust
use laravel::http::Request;use laravel::inertia::{self, Props, props};use serde_json::Value; pub struct HandleInertiaRequests; impl inertia::Middleware for HandleInertiaRequests {    async fn share(&self, request: &Request) -> laravel::Result<Props> {        let session = request.session()?.clone();        let status = session.get::<String>("status");        let message = status.as_deref().and_then(laravel_keyring::http::status_message);         Ok(props! {            status => status.clone(),            statusMessage => message,            keyringResult => laravel_keyring::http::result(&session, None).unwrap_or(Value::Null),            keyringAlerts => session.get::<Value>("keyring.alerts").unwrap_or_else(|| Value::Array(Vec::new())),            impersonation => laravel_keyring::impersonation::banner(&session),        })    }}

http::status_message() gives a slug's message, in English, and None for anything else. http::result(&session, Some("continue")) reads one key of the result, such as the page a user who was sent to enroll was going to. The starter kit's src/inertia.rs is this middleware.

Frontends on Another Origin

configureRequests() works as in the PHP docs. The server side is in Headless Frontends.

Cookies and CSRF

As in the PHP docs: the session cookie needs credentials: 'include' and CORS that allows credentials, and a route that sets the XSRF-TOKEN cookie for csrfCookie. See Headless Frontends.

Headless Vue and React

As in the PHP docs.

Headless Helpers

As in the PHP docs.

Rendering on the Server

As in the PHP docs.