Leaf and Kettle's account area, /account and every page under it like /account/orders/1002, is only for signed-in customers. Signing in sets a session cookie. Write proxy.ts so a visitor without that cookie is sent to sign in before the page starts to render.
Export the function as proxy, and a config whose matcher makes Next.js run it for /account and every path under it, and for nothing else: not /teas, not /sign-in, and not /account-help, the public help page.
A request without a session cookie, or with an empty one, gets a 307 redirect to /sign-in, with the path and query it asked for in a next search param, so the sign-in page can send the customer back afterwards. NextResponse.redirect needs a full URL, so build it from request.url.
A request with a session cookie goes on to its page: return NextResponse.next() (returning nothing does the same).
This is a quick check. It only asks whether the cookie is there, not whether the session is real, so every account page and Server Action must still check the session itself.
GET /account/orders?page=2 (no session cookie)
→ 307 Location: http://localhost:3000/sign-in?next=%2Faccount%2Forders%3Fpage%3D2
GET /account/orders?page=2 Cookie: session=c2Vzc2lvbi0x
→ goes on to app/account/orders/page.tsx
for (const path of ["/account", "/account/orders", "/account/orders/1002"]) expect(proxyRuns(path)).toBe(true);
for (const path of ["/", "/teas", "/sign-in", "/account-help"]) expect(proxyRuns(path)).toBe(false);
const response = await proxy(request("/account"));
expect(response.status).toBe(307);
const location = new URL(response.headers.get("location"));
expect(location.origin + location.pathname).toBe("http://localhost:3000/sign-in");
expect(location.searchParams.get("next")).toBe("/account");
const response = await proxy(request("/account/orders?page=2&sort=newest"));
const location = new URL(response.headers.get("location"));
expect(location.origin + location.pathname).toBe("http://localhost:3000/sign-in");
expect([...location.searchParams.keys()]).toEqual(["next"]);
expect(location.searchParams.get("next")).toBe("/account/orders?page=2&sort=newest");
const response = await proxy(request("/account/orders/1002", { headers: { cookie: "theme=dark; session=c2Vzc2lvbi0x" } }));
// NextResponse.next() marks its answer with x-middleware-next: 1; returning nothing does the same.
expect(response === undefined ? "1" : response.headers.get("x-middleware-next")).toBe("1");
for (const cookie of ["session=", "theme=dark", "sessionid=c2Vzc2lvbi0x"]) {
const response = await proxy(request("/account/orders", { headers: { cookie } }));
expect(response.status).toBe(307);
expect(new URL(response.headers.get("location")).searchParams.get("next")).toBe("/account/orders");
}
Run tests runs every test on your code, here in your browser. The problem is solved when they all pass.
Ctrl + Enter runs the tests (⌘ + Enter on a Mac).
I'm here to help with Send signed-out visitors to sign in. I give hints, never the solution, so you get to solve it yourself. Ask for a hint, select code to ask about it, or press Ask Tuto why on a failing test.
Your first 3 answers need no account. for free to keep asking.