# Headless Frontends

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

## Introduction

This guide is for a frontend that Laravel for Rust doesn't serve, such as a Nuxt app on `app.example.com` calling the application on `api.example.com`. The crate's routes and JSON are the PHP package's, so the frontend side of the [PHP guide](https://keyringauth.com/docs/0.x/headless) holds unchanged. This page covers the server side, in laravel-rust's terms.

## Choosing a Layout

As in the [PHP guide](https://keyringauth.com/docs/0.x/headless#choosing-a-layout): the frontend and the application have to be on one site, such as `app.example.com` and `api.example.com`, so the browser sends the session cookie.

## Configuring Laravel

### Pages and Redirects

Turn `pages.enabled` off, so anything but a JSON request for one of Keyring's pages is a `404`, and point `home` and the other URLs Keyring sends people to at the frontend:

```rust
let keyring = config.section_mut::<laravel_keyring::KeyringConfig>();
keyring.pages.enabled = false;
keyring.home = "https://app.example.com/dashboard".to_owned();
keyring.enrollment_url = Some("https://app.example.com/settings/security".to_owned());
keyring.password_change_url = Some("https://app.example.com/settings/password".to_owned());
```

A page given with `render_using()` still answers.

### Session Cookies

Set the session cookie's domain to the site, so every subdomain gets it, with laravel-rust's session settings: `SESSION_DOMAIN=.example.com`, `SESSION_SECURE_COOKIE=true`, and `SESSION_SAME_SITE` left at `lax`. Keyring's own cookies follow the session cookie's domain, and are `__Secure-` prefixed when it's shared with subdomains.

### Sanctum

Not needed: there's no Sanctum in laravel-rust. Keyring's routes are in the `web` group and use the session cookie, and so should any route of your own the frontend calls while signed in, so `AuthenticateSession` checks it.

### CORS

The frontend's requests are cross-origin, so the application has to allow them with credentials, with laravel-rust's `cors` config:

```rust
use laravel::config::cors::Allowed;

laravel::Application::configure().with_config(|config, _| {
    config.cors.paths = vec!["*".to_owned()];
    config.cors.origins = Allowed::only(["https://app.example.com"]);
    config.cors.credentials = true;
});
```

List every path the frontend calls, Keyring's included. The PHP guide's [advice](https://keyringauth.com/docs/0.x/headless#cors) on which origins to allow holds.

### CSRF

Keyring's routes keep laravel-rust's CSRF protection. Its middleware sets the `XSRF-TOKEN` cookie on every response in the `web` group, and takes the token back in the `X-XSRF-TOKEN` header, so the frontend reads the cookie and sends it, as the browser package's `json()` does. There's no `/sanctum/csrf-cookie`: point `csrfCookie` in `configureRequests()` at any `GET` route in the `web` group, such as one of your own that answers `204`.

### Passkeys

A WebAuthn ceremony runs in the frontend's page, so the browser checks keys against the frontend's origin. Set `webauthn.relying_party_id` to the site, and list the frontend's origin in `webauthn.origins`:

```rust
let webauthn = &mut config.section_mut::<laravel_keyring::KeyringConfig>().webauthn;
webauthn.relying_party_id = Some("example.com".to_owned());
webauthn.origins = vec!["https://app.example.com".to_owned()];
```

A relying party ID that isn't each origin's host or a parent domain of it stops the application booting.

### Emailed Links

Keyring builds emailed links from `APP_URL`. Start them at the frontend instead, with the provider's `link_root_using()`:

```rust
KeyringServiceProvider::<Users>::new().link_root_using(|| "https://app.example.com".to_owned());
```

The frontend then serves the pages those links open, at Keyring's paths, and passes them on as the [PHP guide](https://keyringauth.com/docs/0.x/headless#the-pages-emailed-links-open) shows. That includes an emailed code's [sign-in link](https://keyringauth.com/docs/0.x/rust/factors#signing-in-from-the-email), at `/login/challenge/email-code/link`, once `with_link()` is on.

### Single Sign-On

With `link_root_using()` set, the callback URL Keyring gives identity providers starts at the frontend too. Register that URL with the provider, and have the frontend pass the callback's path and query on as JSON, as the [PHP guide](https://keyringauth.com/docs/0.x/headless#single-sign-on) describes.

### Local Development

As in the [PHP guide](https://keyringauth.com/docs/0.x/headless#local-development): with the frontend on `http://localhost:3000` and the application on `http://localhost:8000`, both are the same site. Set `APP_URL` to the application, and `webauthn.origins` to the frontend's origin.

## One Origin

As in the [PHP guide](https://keyringauth.com/docs/0.x/headless#one-origin): the frontend can proxy the application under a path of its own, such as `/backend`. Set Keyring's `prefix` to match the routes the proxy passes through, and tell the browser package with `configureRoutes({ prefix })`.

## Pages as JSON

As in the PHP package: a request for one of Keyring's pages that expects JSON, and isn't an Inertia visit, gets `{ "page", "props" }`, with the status and headers it would have had. `http::wants_json()` says whether a request does.

## Where Each Answer Goes

The answers are the PHP package's, as its [guide](https://keyringauth.com/docs/0.x/headless#where-each-answer-goes) lists them: go by `page` when there is one, then `redirect`, then stay where you are.

## Building the Frontend With Nuxt

The Nuxt frontend in the [PHP guide](https://keyringauth.com/docs/0.x/headless#building-the-frontend-with-nuxt) works against the crate unchanged, apart from the CSRF cookie route above.

### Calling Laravel

See the [PHP guide](https://keyringauth.com/docs/0.x/headless#calling-laravel).

### Signing In

See the [PHP guide](https://keyringauth.com/docs/0.x/headless#signing-in).

### Step-Up

See the [PHP guide](https://keyringauth.com/docs/0.x/headless#step-up).

### The Pages Emailed Links Open

See the [PHP guide](https://keyringauth.com/docs/0.x/headless#the-pages-emailed-links-open). The headers Keyring sends on those pages are the PHP package's.

### Rendering on the Server

See the [PHP guide](https://keyringauth.com/docs/0.x/headless#rendering-on-the-server).

## What Doesn't Work

As in the [PHP guide](https://keyringauth.com/docs/0.x/headless#what-doesnt-work):

- **Different sites.** The frontend and the application must be on [one site](https://keyringauth.com/docs/0.x/rust/headless#choosing-a-layout).
- **Bearer tokens and mobile apps.** Pending logins and step-up live in the session, so Keyring only signs in with laravel-rust's session guard.
- **Going back after signing in, confirming or enrolling.** Keyring only goes back to pages on its own origins, so keep the page the user was going to in the frontend.
- **Guest pages for a signed-in browser.** Keyring redirects a signed-in browser at its guest pages to `home`, a JSON request too, rather than answering it. Check whether the user is signed in first.
- **Clearing the browser's data on sign out.** `Clear-Site-Data` only clears what the browser keeps for the application's origin. Clear what the frontend kept itself.
- **The Inertia helpers.** Use `vue-headless`, `react-headless` or `headless` instead.
