SSN CODING CLUBFull Stack Wing
THE SSN CHRONICLE
Full Stack Wing

What actually happens when you click a button

Follow one Login click through a full-stack app: from the React frontend to the API, backend and database, then back again, plus what can go wrong along the way.

You click Login. A second later a dashboard shows up. It feels like nothing happened, but your browser, a server and a database just passed a message back and forth. I want to walk through that path using a login button, because it's the easiest example to follow.

In this post

The frontend

The frontend is the part you actually see and touch: the buttons, forms and layout. It shows content, collects what you type, and tries not to feel clunky. HTML, CSS, JavaScript, React and Next.js are the usual tools.

Say someone types their email and password into a login form and hits Login. In React, that could look like this. The form values are kept in state with useState, and the request is sent with the browser's fetch function:

import { useState } from "react";

function Login() {
  const [email, setEmail] = useState("");
  const [password, setPassword] = useState("");

  const handleLogin = async (e) => {
    e.preventDefault();

    const response = await fetch("/api/login", {
      method: "POST",
      headers: { "Content-Type": "application/json" },
      body: JSON.stringify({ email, password }),
    });

    const data = await response.json();
    console.log(data);
  };

  return (
    <form onSubmit={handleLogin}>
      <input
        type="email"
        value={email}
        onChange={(e) => setEmail(e.target.value)}
        placeholder="Email"
      />
      <input
        type="password"
        value={password}
        onChange={(e) => setPassword(e.target.value)}
        placeholder="Password"
      />
      <button type="submit">Login</button>
    </form>
  );
}

When the button is pressed, the handler runs and sends those details toward the backend. The next question is how they get there.

The API

The frontend and backend usually live in different places, so they need an agreed way to talk. That agreement is the API (Application Programming Interface).

Here, the browser sends an HTTP POST request to /api/login with the data as JSON:

{
  "email": "user@example.com",
  "password": "password123"
}

These are the HTTP methods you'll see most:

  • GET reads data
  • POST creates or submits something
  • PUT replaces a resource
  • PATCH updates part of a resource
  • DELETE removes a resource

Since both sides only need to follow the same API, a React frontend can talk to a backend written in Node.js, Java or Python without caring which.

One note: in a real app this request goes over HTTPS, so the password is encrypted on the way.

The backend

The backend makes the decisions. It runs the business logic, checks who you are, and talks to the database. A login route in Node.js with Express might look like this:

app.post("/api/login", async (req, res) => {
  const { email, password } = req.body;

  // Find the user in the database
  const user = await User.findOne({ email });

  if (!user) {
    return res.status(401).json({ message: "Invalid credentials" });
  }

  // Compare the password with the stored hash
  const isValid = await bcrypt.compare(password, user.password);

  if (!isValid) {
    return res.status(401).json({ message: "Invalid credentials" });
  }

  return res.json({ message: "Login successful" });
});

This is a simplified version. A real app also needs input validation, error handling, protection against repeated login attempts, and proper session or token handling.

Notice that both failure cases return the same "Invalid credentials" message. That's on purpose. If the message said "no such user" for one case and "wrong password" for the other, an attacker could use it to find out which emails are registered. OWASP's Authentication Cheat Sheet recommends generic messages for this reason.

The important habit is that the backend never trusts what the frontend sends. Anyone can edit a request, so the server checks everything itself.

The database

Apps have to remember things after you close the tab: accounts, products, orders, payments. That's what the database is for. PostgreSQL, MySQL, MongoDB and SQLite are common choices.

For our login, the backend asks for the user with that email. Roughly, the query is:

SELECT * FROM users
WHERE email = 'user@example.com';

In real code you'd pass the email as a query parameter instead of building the string yourself, so user input can't change the query.

If the user exists, the backend gets back the stored password hash and compares it with what was submitted, for example with bcrypt. Passwords should never be stored as plain text. OWASP's Password Storage Cheat Sheet explains why, and which hashing algorithms to use.

The response

When the work is done, the backend sends an HTTP response. For a successful login it can be as small as { "message": "Login successful" }, along with a status code that tells the frontend how it went:

| Status code | Meaning | |---|---| | 200 | OK | | 201 | Created | | 400 | Bad Request | | 401 | Unauthorized | | 403 | Forbidden | | 404 | Not Found | | 500 | Internal Server Error |

The frontend reads this and decides what you see next, either a redirect to your dashboard or an error message if the credentials were wrong. That's one full round trip.

The whole trip in one picture

request-flow-diagram


All of this normally takes a few milliseconds. A slow network, a busy server or a heavy query can stretch it out.

When things go wrong

Real apps are messier than a demo. The connection drops, the server goes down, or two people try to buy the last item at the same moment. A few things developers have to think about:

  • Error handling: what does the user see when a request fails?
  • Auth: is this person logged in, and are they allowed to do this?
  • Validation: is the incoming data actually valid?
  • Concurrency: what happens when many people change the same data at once?
  • Performance: does it stay fast as traffic grows?
  • Security: how do we keep user data away from the wrong people?

Handling these well is what separates a weekend project from something people can depend on.

Wrapping up

A button click looks tiny, but it sets off a chain of work. The frontend catches the action, the API carries the request, the backend decides what to do, the database holds the data, and the response brings the answer back.

Once you can picture that loop, a lot of full-stack development gets easier to reason about. Next time you click Login, you'll know roughly what's happening behind it.

Sources

ssn@codingclub:~$ discussion_

// Questions & conversations

Ask a question, share an idea or leave a reply.

THE MARGIN NOTES

Sign in with GitHub to join the conversation.

Opening the discussion…