Vibe Coding Securely, Ch. 3: Auth You Didn't Write
Part of Vibe Coding Securely, a short, hands-on book about shipping the app an AI built without getting owned.
Logging in feels like the hard part, so it is where people relax once it works. But a login only proves one thing: that a moment ago, someone clicked “log in.” The harder question comes on every request after that. When the browser asks your server to do something, how does the server know who is asking, and how does it decide what they are allowed to do? Get that wrong and the login is theatre. As an AI tool often writes it, becoming the admin is as easy as editing one value in your browser.
Put the app at the start of this chapter:
git checkout ch3-start
Vibe Notes now has a login (log in as alice or bob from the bar at the top) and an admin page that lists everyone’s notes. alice is an admin, bob is not.
The feature, and how it decides who you are
When a request comes in, the app works out who you are in lib/auth.ts:
export async function getUser(): Promise<User | null> {
const uid = (await cookies()).get("uid")?.value;
if (!uid) return null;
const user = getDb()
.prepare("SELECT id, username, role FROM users WHERE id = ?")
.get(uid) as User | undefined;
return user ?? null;
}
It reads a cookie called uid and looks up that user. A cookie is just a small piece of data the browser stores for a site and sends back on every request. And where did this uid cookie come from? Login set it, in app/api/login/route.ts:
// ch3-start: hand the browser a plain `uid` cookie it can read and change.
(await cookies()).set("uid", String(user.id), { path: "/" });
So after alice logs in, her browser holds uid=1. On each request, the server reads that cookie and says, this is user 1, alice, the admin. It looks reasonable. It runs. It is also completely broken, because that cookie belongs to the visitor, not to you. They can change it to anything.
Seeing it
Let me be user bob, or really, let me be nobody at all, and just claim to be the admin. I will not even log in. I will send a request to the admin-only endpoint with a uid cookie I set by hand to 1:
curl http://localhost:3000/api/admin/notes -H "cookie: uid=1"
[{"id":2,"title":"Groceries","body":"Oat milk, coffee, the good bread.",...},
{"id":1,"title":"Welcome","body":"This is Vibe Notes...",...}]
There they are: everyone’s notes, from the admin endpoint, with no login. I did not break in. I did not guess a password, because there was nothing to guess. I set a cookie, which anyone can do, and the server believed it. This chapter’s check reports the same thing:
npm run check:ch3
FOUND: a hand-set "uid=1" cookie, with no login, returned 2 notes
from the admin-only endpoint. The server trusted the cookie as proof of
who you are, and anyone can set a cookie. See chapter 3.
The mistake has a plain name: the server is trusting the client to tell it who they are. A visitor controls everything the browser sends, cookies included. So anything the browser tells you about who it is, it could be lying about.
The fix: a session the browser cannot forge
The browser can still carry a cookie. It just cannot be allowed to carry your id in the clear, where it can edit it. Instead, when you log in, the server mints a long random token, stores on the server which user that token belongs to, and hands the browser only the token. The id stays on the server.
Login becomes this:
// Fixed (chapter 3): mint a long random token, store it against the user, and
// hand the browser only that token in an httpOnly cookie.
const token = randomBytes(32).toString("hex");
getDb().prepare("INSERT INTO sessions (token, user_id) VALUES (?, ?)").run(token, user.id);
(await cookies()).set("session", token, { path: "/", httpOnly: true, sameSite: "lax" });
And getUser stops reading an id and starts looking up the token:
export async function getUser(): Promise<User | null> {
const token = (await cookies()).get("session")?.value;
if (!token) return null;
const user = getDb()
.prepare(
`SELECT u.id, u.username, u.role
FROM sessions s JOIN users u ON u.id = s.user_id
WHERE s.token = ?`
)
.get(token) as User | undefined;
return user ?? null;
}
Two words earn their keep here. The token is 32 random bytes, so a visitor cannot guess a valid one or edit theirs into someone else’s; there is nothing to edit but a long random string that only matches the user the server already tied it to. And httpOnly tells the browser not to let JavaScript read the cookie at all, which closes a different door we meet in chapter 5. The server never again reads a user id or a role from the client. It reads a token and looks up the truth itself.
Jump to the fixed app if you like:
git checkout ch3-done
Proving it
Run the same check:
npm run check:ch3
BLOCKED: the forged cookie was ignored; admin access needs a real session.
The forged uid=1 cookie now gets a flat refusal, a 403, while a real logged-in alice still reaches the admin page and bob still cannot. The login is no longer theatre. It hands out a token that means something, and the server checks it.
The part that generalizes
Two honest notes. First, this login has no password; you just pick a user. That is a simplification so the chapter can stay on one idea, and it is not something you would ship. In a real app the strongest move is not to write login yourself at all. Your host and the big auth libraries have solved passwords, resets, and sessions, carefully, and you should lean on them. Writing your own auth is a lot of ways to be subtly wrong.
Second, and this is the lesson that outlives this chapter: never trust the client to tell you who it is or what it may do. The browser is in someone else’s hands. Decide identity and permission on the server, from something the visitor cannot fake, every time.
Where this leaves us
Now the server knows who you really are. That is necessary, and it is not enough. In the next chapter the app knows you are bob, and still hands you alice’s private note, because you asked for it by its number and nobody checked whose it was. That is chapter 4.