How a login works
Login looks like one form. Behind it, the browser, the server and the database pass a dozen messages, and checks along the way keep an account safe. This is the basic pattern every developer on our team learns first.
Our co-op student's first project was a website of her own that no customer depends on. It teaches our file structure, our naming conventions and the patterns we use to bundle components and utilities. It also gives her a base to build on.
The pages and routing came together quickly. Login was the first hard problem. It is complex for anyone who hasn't built one before, and a diagram helps. This is how we approached it.
Login looks like one form. Behind it, the browser, the server and the database pass a dozen messages, and checks along the way keep an account safe.
This is the basic pattern for login, and every developer on our team learns it first.
The backbone
Browser: sends the login details to the server.
Server: looks up the user.
Database: returns the stored record.
Server: verifies the login details against the stored record and creates a session with an expiry.
Database: stores the session.
Server: returns a token to the browser in a cookie.
Browser: sends that cookie with every request that follows.
Server: checks the session before it returns any data.
Database: confirms the session exists and is still live.
Server: rejects an expired session and ends the session when the user logs out.
Database: deletes the session record either way.
Key checkpoints
The server decides on every request
The server decides who the user is and what they can reach. The browser presents the login details or the token. The server validates them, checks the session record in the database on every request and replies with what that user may see. A request without a live session gets a 401 and no data.
Store a slow hash of each password
The server stores a slow, one-way hash of each password with a unique salt. The password itself is never stored. At login the server hashes the submitted password the same way and compares the two results. A stolen database holds only salted hashes, and every guess against them is slow.
Keep the token in a protected cookie
The browser attaches a cookie to every request to your site automatically, and it can keep the cookie out of reach of scripts on the page. Set three flags on the cookie. HttpOnly keeps scripts on the page from reading it. Secure sends it only over HTTPS. SameSite keeps other sites from attaching it to the forms and background requests they send to yours.
Check access on every data request
A live session proves who the user is. Each call that returns data also checks that this user may see the record requested. Each customer's session reaches only that customer's data.
Give every session an expiry
The server stores each session in the database with its expiry. On every request the server checks that expiry before it returns data. A session ends at a set time or after a period of inactivity, and the server deletes its record from the database. A short expiry and an inactivity timeout limit how long a copied token works.
Log out on the server
The browser asks the server to log out. The server deletes the session record from the database and then clears the cookie. A copied token stops working the moment the record is gone.
Answer every failed login the same way
"Incorrect username or password" covers every failure. Separate messages for an unknown user and a wrong password tell an attacker which accounts exist. The server also slows repeated attempts for each account and each address.
The code, by workflow
The diagram shows a dozen messages. The code behind them follows the workflow. One call handles several steps, and the logic the steps share lives in one place.
| Workflow | Example | Covers |
|---|---|---|
| Log in | POST /api/login | Hashing, the protected cookie and the single failure message |
| Every request | A check that runs before each protected route | The server's decision, the live session and expiry |
| Data calls | Each endpoint that returns records | Data access for the record requested |
| Log out | POST /api/logout | Ending the session on the server and in the database |
| Shared logic | One module the calls above share | Password hashing, sessions, cookie flags and expiry |
| Browser side | One helper every page uses | Login, logout and the current user |
The login call runs the whole login in one request. It looks up the user, compares the hashes and sets the cookie. It returns the same error for every failure.
The request check runs before every protected route. It reads the cookie, checks the session in the database and answers 401 when the session is gone.
The shared module holds the logic the calls above share. Hashing, the cookie flags and the expiry rule each live there once. Every call applies them the same way.
The browser helper gives every page the same login, logout and current user. It shapes what the page shows. The server decides what data leaves.
Building on it
The pattern is extensible, and that is its main benefit. Once the fundamentals are in place, each piece becomes something you can switch out. Single sign-on through OpenID Connect or SAML changes how a user proves who they are. So do passkeys. Middleware on the server and in the browser hooks into the same steps. Every one of them starts from the basics above.