Skip to the code

Send signed-out visitors to sign in

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

What the tests check

  1. runs for /account and every path under it, and nothing else
    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);
    
  2. sends a visitor without a session to sign in, with a 307
    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");
    
  3. next holds the whole path and query, as one param
    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");
    
  4. lets a visitor with a session cookie through
    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");
    
  5. an empty session cookie, or only other cookies, still means sign in
    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");
    }
    

Learn more

proxy.tsSaved in this browser

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).