# Frontend

Source: https://keyringauth.com/docs/0.x/rust/frontend

## Introduction

Keyring is headless. It renders [pages](https://keyringauth.com/docs/0.x/rust/sign-in-flow#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:

```bash
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](https://keyringauth.com/docs/0.x/frontend) show in TypeScript, Vue and React holds. The contract suite simulates the package's requests (see [`contract/src/browser.ts`](https://github.com/keyringauth/keyring/blob/main/contract/src/browser.ts)) rather than running it, so check the package against your application as well. The [starter kit](https://github.com/keyringauth/keyring/blob/main/rust/starter-kit/README.md) 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](https://keyringauth.com/docs/0.x/rust/frontend#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](https://keyringauth.com/docs/0.x/rust/sign-in-flow#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.

## The Pages Emailed Links Open

As in the [PHP docs](https://keyringauth.com/docs/0.x/frontend#the-pages-emailed-links-open): `email-verification`, `email-change` and `device-report` each get their link's values as props, and post them back in the body. `sign-in-link`, once email codes [send a link](https://keyringauth.com/docs/0.x/rust/factors#signing-in-from-the-email), has a button only in the browser that started the sign in, which posts its `code` prop to its `url` prop, and anywhere else asks for the code to be typed where the sign in started. The starter kit's `auth/sign-in-link.tsx` shows each case.

## The Challenge Page

As in the [PHP docs](https://keyringauth.com/docs/0.x/frontend#the-challenge-page): `useChallenge()` from `@keyringjs/keyring/vue` or `/react` does the work, for the `challenge` and `confirm` pages alike.

### Vue

See the [PHP docs](https://keyringauth.com/docs/0.x/frontend#vue).

### React

See the [PHP docs](https://keyringauth.com/docs/0.x/frontend#react), 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](https://keyringauth.com/docs/0.x/frontend#factor-components). 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](https://keyringauth.com/docs/0.x/frontend#signing-in-with-a-passkey), 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](https://keyringauth.com/docs/0.x/frontend#signing-in-with-an-identity-provider). See [Single Sign-On](https://keyringauth.com/docs/0.x/rust/sso#login-buttons).

## Challenge Helpers Without Vue or React

`@keyringjs/keyring/inertia` works as in the [PHP docs](https://keyringauth.com/docs/0.x/frontend#challenge-helpers-without-vue-or-react).

## Managing Authenticators

`enroll()`, `beginEnrollment()`, `confirmEnrollment()`, `renameAuthenticator()`, `makeDefault()` and `removeAuthenticator()` work as in the [PHP docs](https://keyringauth.com/docs/0.x/frontend#managing-authenticators). 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](https://keyringauth.com/docs/0.x/rust/quick-start#your-settings-page)).

### Frozen Changes

`proveWithKey()` works as in the [PHP docs](https://keyringauth.com/docs/0.x/frontend#frozen-changes).

## Sessions

The sessions helpers work as in the [PHP docs](https://keyringauth.com/docs/0.x/frontend#sessions). 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](https://keyringauth.com/docs/0.x/frontend#handling-step-up-in-javascript): 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](https://keyringauth.com/docs/0.x/frontend#json-requests). 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](https://keyringauth.com/docs/0.x/frontend#webauthn-helpers). 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](https://keyringauth.com/docs/0.x/frontend#changing-routes).

## 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](https://keyringauth.com/docs/0.x/frontend#frontends-on-another-origin). The server side is in [Headless Frontends](https://keyringauth.com/docs/0.x/rust/headless).

### Cookies and CSRF

As in the [PHP docs](https://keyringauth.com/docs/0.x/frontend#cookies-and-csrf): 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](https://keyringauth.com/docs/0.x/rust/headless#csrf).

### Headless Vue and React

As in the [PHP docs](https://keyringauth.com/docs/0.x/frontend#headless-vue-and-react).

### Headless Helpers

As in the [PHP docs](https://keyringauth.com/docs/0.x/frontend#headless-helpers).

### Rendering on the Server

As in the [PHP docs](https://keyringauth.com/docs/0.x/frontend#rendering-on-the-server).
