Put session routes and guest walls in middleware

See what the web group already does, then wrap the notebook in Authenticate without writing the middleware yourself.

Middleware runs around a handler. Field Notes gets most of its browser protection from groups that already exist. You add Authenticate only where a guest must not pass.

Routes registered with web already run inside the web middleware group. That group provides sessions, encrypted cookies, and CSRF protection. A form posted from your own origin can include the token the group expects. The testing guide notes that the test client marks requests as same-origin, so CSRF allows them through in tests. A stranger posting to /notes from another site should not get that pass. You do not reimplement CSRF to learn this. You choose the web file so the group is already there.

Routes registered with api do not get that group. They are stateless, and their URIs start with /api. A JSON client that calls /api/notes will not have the session cookie unless you design that on purpose. Field Notes keeps the notebook on web routes. An API version can wait until you want stateless callers.

routes() is the third knob. Those routes get no session, no CSRF check, and no prefix. The routing guide’s webhook example attaches its own middleware:

route.middleware(verify_stripe_signature).group(|route| {
    route.post("/webhooks/stripe", webhooks::stripe);
});

That function is yours to write when you have a provider signature to check. This series does not invent a Stripe verifier.

The guest wall

Authenticate is framework middleware. From the middleware guide:

use laravel::auth::Authenticate;

route.middleware(Authenticate).group(|route| {
    route.get("/notes", notes::index);
});

A guest hitting that route is redirected to the login page once you have marked a handler with #[laravel::auth::page(login)]. A request that expects JSON gets 401 instead. Both outcomes come from the authentication guide’s description of Authenticate. Until the login page exists, the redirect target is whatever the starter does when that page is missing. The check in this lesson is the status or the redirect, not a finished form.

Several middleware can share a route. The guide runs them in the order you list:

use laravel::auth::{Authenticate, RequireVerifiedEmail};

route.get("/billing", billing::show).middleware((Authenticate, RequireVerifiedEmail));

RequireVerifiedEmail belongs to the verification guide. Field Notes does not turn it on. The tuple is here so you can see that authentication and verification are separate walls.

Middleware directly on a route runs in addition to the group. laravel route:list -v --path=notes shows the stack. Confirm the web group is present because the route lives in web.rs, and that Authenticate is present because you added it. A route you only put in api.rs should not pick up the session group by accident.

Check your work

As a guest, request a route inside the Authenticate group and record whether you are redirected or get 401.

Back to roadmap