Headless Frontends
On this page
- Introduction
- Choosing a Layout
- Configuring Laravel
- Pages and Redirects
- Session Cookies
- Sanctum
- CORS
- CSRF
- Passkeys
- Emailed Links
- Single Sign-On
- Local Development
- One Origin
- Pages as JSON
- Where Each Answer Goes
- Building the Frontend With Nuxt
- Calling Laravel
- Signing In
- Step-Up
- The Pages Emailed Links Open
- Rendering on the Server
- What Doesn't Work
Introduction
This guide is for a frontend that Laravel for Go doesn't serve, such as a Nuxt app on app.example.com calling the application on api.example.com. Keyring for Go's routes and JSON are the PHP package's, so the frontend side of the PHP guide holds unchanged. This page covers the server side, in Laravel Go's terms.
Choosing a Layout
As in the PHP guide: 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 Config.Pages 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:
keyring.New[models.User](func(cfg *keyring.Config) { cfg.Pages = false cfg.Home = "https://app.example.com/dashboard" cfg.EnrollmentURL = "https://app.example.com/settings/security" cfg.PasswordChangeURL = "https://app.example.com/settings/password"})KEYRING_HOME sets Home without code.
Session Cookies
Set the session cookie's domain to the site, so every subdomain gets it, with Laravel Go's session settings: SESSION_DOMAIN=.example.com, SESSION_SECURE_COOKIE=true, and SESSION_SAME_SITE left at lax. Keyring's own cookies take the session cookie's path and domain, and over HTTPS they're Secure and get the __Host- or __Secure- prefix, unless you named them yourself.
Sanctum
Not needed: there's no Sanctum in Laravel Go. 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. Laravel Go's middleware.HandleCORS is in the global stack, opening api/* to everyone by default. Replace it with one for the frontend:
laravel.Options{ Middleware: func(m *laravel.Middleware, _ *config.Config) { m.Global.Replace(middleware.HandleCORS{ Paths: []string{"*"}, AllowedOrigins: []string{"https://app.example.com"}, AllowCredentials: true, }) },}List every path the frontend calls, Keyring's included. The PHP guide's advice on which origins to allow holds.
CSRF
Keyring's routes keep Laravel Go's CSRF protection, middleware.ValidateCSRFToken in the web group. It 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 a GET route of your own in the web group that answers 204.
A request from app.example.com to api.example.com comes with Sec-Fetch-Site: same-site, which the check doesn't take on its own unless you set AllowSameSite. Keep the token: Keyring takes that header away from a request whose session was ended under it, so the token decides, as PHP's 419 does.
Passkeys
A WebAuthn ceremony runs in the frontend's page, so the browser checks keys against the frontend's origin. Set WebAuthn.RelyingPartyID to the site, and list the frontend's origin in WebAuthn.Origins:
cfg.WebAuthn.RelyingPartyID = "example.com"cfg.WebAuthn.Origins = []string{"https://app.example.com"}KEYRING_WEBAUTHN_RP_ID sets the relying party ID without code. A relying party ID that isn't each origin's host or a parent domain of it stops Register, so the application doesn't boot.
Emailed Links
Keyring builds emailed links from APP_URL. Start them at the frontend instead, with Config.LinkRoot:
cfg.LinkRoot = func(ctx context.Context) string { return "https://app.example.com"}The links are signed relative to the site, so the same link opens wherever it's put. An emailed code's sign-in link isn't signed: it carries its code encrypted, and its page only shows the code in the browser that holds the sign in, so start it at the host people sign in on. The frontend then serves the pages those links open, at Keyring's paths (/login/challenge/email-code/link for the sign-in link's), and passes them on as the PHP guide shows.
Single Sign-On
With LinkRoot 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 describes.
Local Development
As in the PHP guide: 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: the frontend can proxy the application under a path of its own, such as /backend. Set Config.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. Page props are maps, so their keys can come in any order.
Where Each Answer Goes
The answers are the PHP package's, as its guide 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 works against Keyring for Go unchanged, apart from the CSRF cookie route above.
Calling Laravel
See the PHP guide.
Signing In
See the PHP guide.
Step-Up
See the PHP guide.
The Pages Emailed Links Open
See the PHP guide. The headers Keyring sends on those pages are the PHP package's.
Rendering on the Server
See the PHP guide.
What Doesn't Work
As in the PHP guide:
- Different sites. The frontend and the application must be on one site.
- Bearer tokens and mobile apps. Pending logins and step-up live in the session, so Keyring only signs in with Laravel Go'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 the route named
dashboard, elsehome, else the site's root, 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-Dataonly clears what the browser keeps for the application's origin. Clear what the frontend kept itself. - The Inertia helpers. Use
vue-headless,react-headlessorheadlessinstead.