{"success":true,"name":"check-auth-on-the-server-for-every-protected-next-js-route","description":"Adding protected pages or user data in a Next.js app","category":"ai-brimind","path":"ai-brimind/check-auth-on-the-server-for-every-protected-next-js-route/SKILL.md","file":"SKILL.md","content":"---\nname: check-auth-on-the-server-for-every-protected-next-js-route\ndescription: \"Adding protected pages or user data in a Next.js app\"\n---\n\n# Check Auth on the Server for Every Protected Next.js Route\n\n## When to use\nProject is Next.js, and you are about to protect pages or data using only a client-side check or only middleware.\n\n## Example request\nhiding the admin link when user is not logged in → the page and its API still answer if someone types the URL; hiding is not protecting. — UI hiding is not security. | putting the auth check only in middleware → middleware can be bypassed or misconfigured; the page and route handler must verify the session themselves. — Defense in depth. | checking auth in useEffect and redirecting → the protected data already left the server before the redirect fired. — Client checks run too late. | reading the session in a Server Component or Server Action before returning data → the check runs where the data lives. — Server-side gatekeeping.\n\n## Instructions\nAct like a security architect who assumes every URL will be typed directly by a stranger. Before building any protected area in Next.js: 1) List what is being protected: pages, Server Actions, route handlers, and the data each one returns. Every item on that list needs its OWN server-side check — reading the session/cookie on the server before returning anything. 2) Place the layers correctly: middleware is a convenient early filter for redirecting logged-out users, but it is never the only guard; the Server Component, Server Action, or route handler must re-verify, because those can be reached in ways middleware does not cover. 3) Never rely on client-side state for security: hiding buttons, useEffect redirects, or 'isAdmin' in React state only shape the UI — the server must refuse, not the browser. 4) Prefer the platform's session mechanisms (httpOnly cookies read on the server) over custom token juggling in localStorage, which scripts can steal. 5) If you are unsure whether a given path re-checks auth, trace one request from URL to data by hand instead of assuming the middleware caught it. Only ship once every protected page, action, and handler is confirmed to verify the session itself, and explain for each one where its check runs and why that location cannot be skipped — not just that the login redirect 'seems to work'.\n\n## Metadata\n- role: developer\n- level: tactical\n- places: vibeCoding\n- origin: ai.brimind.pro skill #496\n","linked_files":[],"origin":"ai.brimind.pro","owner_id":null,"source":"router"}