How to understand the authentication flow in a codebase
Find how users sign in, how sessions or tokens are created and checked, and where authorisation is enforced, in any web app's code.
Authentication is one of the first things worth understanding in any web codebase, because it touches almost every request and mistakes in it are serious. It's also often spread across many files: a login route, middleware, a session store, helper functions and checks scattered through handlers.
Separate the three questions
- Authentication: how does the app know who the user is?
- Session or token handling: how is that identity carried between requests?
- Authorisation: what is this user allowed to do?
Codebases often mix these together. Untangling them is most of the work.
Find the sign-in path
Start from the login form or the OAuth callback and trace it:
- Where are credentials checked (password hash comparison, OAuth code exchange, magic link verification)?
- What is created on success: a session record, a signed cookie, a JWT?
- What is returned to the browser, and how (an HTTP-only cookie, a token in the response body)?
Libraries like Auth.js, Lucia-style session code, Passport, Clerk, Supabase Auth or Firebase Auth each have recognisable entry points. Find the library first, then its configuration file.
Find where identity is loaded on each request
Look for middleware or hooks that run on every request and attach a user:
- Express:
app.usemiddleware settingreq.user - Next.js:
proxy.ts(previouslymiddleware.ts), plus helpers called from layouts, pages and actions - SvelteKit:
handleinhooks.server.tssettingevent.locals - NestJS: guards
Note what happens when the session is missing or expired: redirect, 401, or anonymous access.
Find where authorisation is enforced
This is where bugs hide. Search for role checks, ownership checks (post.authorId === user.id), permission helpers and policy functions. Then ask, for each sensitive operation (deleting, paying, exporting, changing roles): where exactly is this checked?
Two patterns to watch:
- Checks only in the UI. Hiding a button doesn't stop a direct API call.
- Checks only at the edge. Middleware that protects
/adminpages may not protect the API those pages call.
Map the token lifecycle
If the app uses tokens: where are they issued, how long do they last, how are they refreshed, and how are they revoked on logout or password change? Long-lived tokens without revocation are a common weakness.
Write it down
A small table helps:
| Operation | Endpoint | Auth required | Authorisation check |
|---|---|---|---|
| View order | GET /api/orders/:id | Yes | Owner or admin (services/orders.ts:40) |
| Refund order | POST /api/orders/:id/refund | Yes | Admin only (routes/orders.ts:72) |
Ask with citations
Ask RepoGrasp "how is a user authenticated on each request, and where are admin-only actions checked?" You'll get the path from sign-in to per-request checks, with file:line citations. Authorisation is security-critical, so verify every cited check, and search for sensitive operations it didn't mention.