# Frontend

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

## Introduction

Keyring is headless. It renders [pages](https://keyringauth.com/docs/0.x/go/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
```

Keyring for Go 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/go/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

Keyring renders its pages through Laravel Go's Inertia package, as the components `auth/login`, `auth/challenge` and so on, with the PHP package's props. Add Inertia's middleware to the `web` group, as the starter kit's `bootstrap/app.go` does:

```go
laravel.Options{
    Middleware: func(m *laravel.Middleware, _ *config.Config) {
        m.Web.Append(inertia.Middleware{Share: share})
    },
}
```

`share` is your function of shared props (see [Flash Messages and Alerts](https://keyringauth.com/docs/0.x/go/frontend#flash-messages-and-alerts)). There's no Blade-style view to fall back on, and no way to rename a page or render it your own way: a page is always the Inertia component `auth/{name}`, or `{ "page", "props" }` for a request that wants JSON. With `Config.Pages` off, anything but a JSON request for a page is a `404`. See [Pages](https://keyringauth.com/docs/0.x/go/sign-in-flow#pages) and [Differences From PHP](https://keyringauth.com/docs/0.x/go/differences#pages-and-responses).

The starter kit's `resources/js/pages/auth` has every page Keyring renders but the sign-in-link page (which only matters with `WithLink()` on), 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. So does `sign-in-link`, once emailed codes [send a link](https://keyringauth.com/docs/0.x/go/factors#signing-in-from-the-email), in the browser that started the sign in: its button posts `{ code }` to its `url` prop. Anywhere else it has no button, and asks for the code from the email to be typed where the sign in started. The [PHP docs](https://keyringauth.com/docs/0.x/frontend#the-pages-emailed-links-open) show the page. The starter kit has no `sign-in-link` page, as it doesn't turn the link on.

## 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 [code factor of your own](https://keyringauth.com/docs/0.x/go/factors#writing-a-code-factor), under the `Key` you gave it.

## 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/go/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 URLs are the PHP package's, so the helpers don't change. Pass the page what `Keyring.SignInMethods(ctx, user)` gives (see [Your Settings Page](https://keyringauth.com/docs/0.x/go/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). A settings page can load `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 `keyring.RequireFreshVerification[models.User]{}` on the page's own route to ask up front.

## 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 (`r.ExpectsJSON()`) and isn't an Inertia visit, as in the PHP package.

## WebAuthn Helpers

As in the [PHP docs](https://keyringauth.com/docs/0.x/frontend#webauthn-helpers). Keyring for Go checks ES256 and EdDSA keys, and RS256 when you list it in `WebAuthn.Algorithms`.

## Changing Routes

If you set `Config.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 under `keyring.result`, as JSON encrypted with `crypt.EncryptString` whatever the session driver. A sign in flashes its "was this you?" alerts under `keyring.alerts`. Share them from your Inertia middleware's `Share`, with the impersonation banner:

```go
import (
    "go.laravel.com/inertia"
    "go.laravel.com/web"

    keyring "keyringauth.com/go"

    "app/models"
)

func share(r *web.Request) (inertia.Props, error) {
    session := r.Session()

    var status, message any
    if slug, _ := session.Get("status").(string); slug != "" {
        status, message = slug, slug
        if text, ok := keyring.StatusMessage(slug); ok {
            message = text
        }
    }

    alerts := session.Get("keyring.alerts")
    if alerts == nil {
        alerts = []any{}
    }

    return inertia.Props{
        "status":        status,
        "statusMessage": message,
        "keyringResult": keyring.FlashedResult(r),
        "keyringAlerts": alerts,
        "impersonation": keyring.From[models.User](r).Impersonation(r),
    }, nil
}
```

`keyring.StatusMessage(slug)` gives a slug's message, in English, and `false` for anything that isn't Keyring's, so your own statuses can have messages of yours. `keyring.FlashedResult(r)` decrypts the result, or gives `nil` when there's none: an object, such as the page a user who was sent to enroll was going to, under `continue`, or a secret being set up. The JSON answers carry the same message as `message`. See [Status Responses](https://keyringauth.com/docs/0.x/go/sign-in-flow#status-responses). The starter kit's `bootstrap/inertia.go` is this function, without the banner.

`impersonation` is `null` unless the browser is [impersonating](https://keyringauth.com/docs/0.x/go/impersonation#showing-a-banner) someone, and the browser package's `Impersonation` type describes it.

## 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/go/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/go/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).
