From login to Save

Five minutes · Trace one admin write in your portfolio.

Follow the proof

  1. Log in. You send your password and authenticator code to the login handler. The handler verifies both and replies with a Set-Cookie header. The cookie holds a signed session that the server accepts for up to eight hours.
    Login handler excerpt
      const ok =
        email && password && code
          ? await checkCredentials({ email, password, code }, ip)
          : false;
    
      if (!ok) {
        return done(
          NextResponse.json({ error: "Invalid credentials" }, { status: 401 }),
        );
      }
    
      const res = NextResponse.json({ ok: true });
      res.cookies.set(SESSION_COOKIE, createSession(), sessionCookieOptions);
      return done(res);
  2. Click Save on a new project. The browser sends the project data and automatically attaches the session cookie to the same-origin request. The editor’s JavaScript can send this request even though HttpOnly prevents it from reading the cookie. Cookie behavior
  3. Decide whether to write. The create route calls checkAdminAPIAuth() before the database operation. The guard reads the cookie and checks the session signature, expiry, and revocation cutoff. Since this app has one owner, accepting that admin session grants permission to create a project. If the guard returns false, the route returns 401 immediately.
    Project creation handler excerpt
        // Check admin authentication first
        if (!(await checkAdminAPIAuth())) {
          return createUnauthorizedResponse();
        }
        // Parse the project data from the request
        const projectData: CreateProjectPayload = await request.json();

Predict what happens

You open the editor just before your session expires. You click Save just after it expires. Say what the API returns and whether Supabase receives an insert. Then reveal the answer.

Reveal feedback

The API returns 401, and Supabase receives no insert. The editor loaded while the session was valid. Save is a new request with its own authorization check. Whether the browser omits the expired cookie or the server rejects an expired token, the guard fails.

Now imagine you add a route to unpublish a project. Where would you put the permission check, and why?

Reveal feedback

Put the check in the new route before the database update. The page redirect decides what interface to show. The route decides whether this particular operation may run.

Quick reference · Primary reading from Next.js

Bring your reasoning or any follow-up question back to the agent. Explain it once from memory before looking at the code again.

Quick reference

Authentication
The login handler verifies the owner’s password and authenticator code before issuing a session.
Session
The cookie carries proof of the earlier login. The server verifies the signature and checks that the session is still valid.
Authorization
The handler checks permission on the current request before it reads protected data or performs a write.

Review prompt. Find the database operation. Follow the handler from its entry point and check that permission is required on every path to that operation.