diff --git a/docs/start/framework/react/guide/middleware.md b/docs/start/framework/react/guide/middleware.md index 6578b0d5ca..276d7ef064 100644 --- a/docs/start/framework/react/guide/middleware.md +++ b/docs/start/framework/react/guide/middleware.md @@ -79,6 +79,28 @@ const loggingMiddleware = createMiddleware().server(async ({ next }) => { }) ``` +The exception to always returning `next()` is throwing errors. By default, throwing an error will cause the router to short-circuit and return the data passed into the `Error` as a 500 HTTP error-code response. If you want to return another error code, like with an authentication middleware, you can throw the `json` helper. + +```tsx +import { createMiddleware, json } from "@tanstack/react-start"; + +const authMiddleware = createMiddleware().server(async ({ next, request }) => { + const client = await getMyDb(); + if (!client) { + throw new Error("Oh No!"); // returns a 500 HTTP response to the client + } + const sessionCookie = request.headers.get("Authorization"); + const user = await client.getUser(sessionCookie); + if (!user) { + throw json({ + message: "you shall not pass!" + }, {status: 401}) + } + + return await next({ context: { user }}) // pass the user's information to the next middlware or server function +}) +``` + ## Request Middleware Request middleware is used to customize the behavior of any server request that passes through it, including server routes, SSR and server functions.