Frontend
On this page
- Introduction
- Building the Pages
- The Pages Emailed Links Open
- The Challenge Page
- Vue
- React
- Factor Components
- Signing In With a Passkey
- Signing In With an Identity Provider
- Challenge Helpers Without Vue or React
- Managing Authenticators
- Frozen Changes
- Sessions
- Handling Step-Up in JavaScript
- JSON Requests
- WebAuthn Helpers
- Changing Routes
- Flash Messages and Alerts
- Frontends on Another Origin
- Cookies and CSRF
- Headless Vue and React
- Headless Helpers
- Rendering on the Server
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:
npm install @keyringjs/keyringKeyring 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 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
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:
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). 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 and Differences From PHP.
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: 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, 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 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: 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 code factor of your own, under the Key you gave it.
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 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).
Frozen Changes
proveWithKey() works as in the PHP docs.
Sessions
The sessions helpers work as in the PHP docs. A settings page can load 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 keyring.RequireFreshVerification[models.User]{} on the page's own route to ask up front.
JSON Requests
json() works as in the PHP docs. 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. 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.
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:
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. The starter kit's bootstrap/inertia.go is this function, without the banner.
impersonation is null unless the browser is impersonating someone, and the browser package's Impersonation type describes it.
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.