Why and How?
Before writing any login code, it helps to understand why a web app needs authentication at all and how the frontend and backend share the job. Authentication answers one question: who is making this request? Once the server knows that, it can decide what the person is allowed to see or change.
Why a server cannot just trust the browser
HTTP is stateless. Each request arrives at the server on its own, with no built-in memory of the previous one. A request that says "show me order 42" carries no proof of who sent it unless you add that proof yourself. Anyone can open developer tools or a tool like Postman and send any request they like, so the server must verify identity on every single protected request.
That is also why hiding a button in React is never enough. The interface can guide people, but only the server can actually enforce rules.
Two common ways to remember a user
| Approach | Where the proof lives | How each request is checked |
|---|---|---|
| Server sessions | A session ID in a cookie; the session data stays on the server | Server looks up the ID in its session store |
| Tokens (for example JWT) | A signed token kept by the client | Server verifies the token's signature and expiry |
Sessions work very well for traditional server-rendered sites. Single page applications that talk to a separate REST API often use tokens instead, because the API can verify a token without keeping a session table, and the same token works for a mobile app too.
How the token flow works
- The user submits an email and password from the React login form.
- The backend finds the user, compares the password against the stored hash, and if it matches, creates a signed token.
- The token goes back in the response. The frontend stores it, for example in memory and
localStorage. - Every later request to a protected endpoint includes the token, usually in an
Authorizationheader. - Middleware on the server verifies the token before the route handler runs. Invalid or missing token means a 401 response.
// Frontend: attaching the token to a request
const response = await fetch("/api/orders", {
headers: {
Authorization: `Bearer ${token}`,
},
});
if (response.status === 401) {
// token missing or expired: send the user to login
}
What the frontend is responsible for
- Collecting credentials and sending them over HTTPS.
- Keeping the token and attaching it to requests.
- Showing the right screens and menu items for logged-in and logged-out users.
- Logging the user out when the token expires or the server returns 401.
A misunderstanding to avoid early
Many beginners think a JWT is encrypted and therefore safe to fill with private data. A standard JWT is only signed, not encrypted. Anyone holding it can decode the payload with a base64 decoder and read it. The signature only proves the server created it and that nobody changed it. Keep payloads small and limited to things like a user ID and role, and never put passwords or personal details inside.
A second mix-up is between authentication and authorisation. Authentication proves identity; authorisation decides permissions. A logged-in user is authenticated, but that does not mean they may delete another user's posts. Your backend needs both checks.
Takeaway: the server must verify identity on every protected request because HTTP forgets everything between calls; tokens carry that proof from the browser, and the frontend's role is to store, send and react to them.
Ready to master real-world web development?
Learn HTML, CSS3, Bootstrap and JavaScript hands-on with mentor-led sessions.