Stop 13 · Authentication & Web SecurityProjects · 1/1

Sign-Up and Login System

Part of the Web Development Roadmap

In short

This project turns your notes app into a real multi-user product. Each person signs up, logs in and sees only their own notes, and it's one of the most valuable features to explain in an interview.

A complete authentication flow for your notes app: sign-up with validation, hashed passwords, login, logout and routes that only show each user their own notes.

The jaadu stands at a hotel reception desk receiving a key card across the counter beside a small brass bell, with a lift and a corridor of room doors behind.

Think of it like this

Checking into a hotel

At check-in, you show ID once and the front desk gives you a key card. For the rest of your stay you just tap the card, and it only opens your own room. At checkout, the card stops working. Sign-up and login are check-in, the HttpOnly cookie is the key card, protected routes are the door locks, and logout is checkout.

The big ideas

What you need to know

Never store plain passwords

bcrypt turns a password into a slow, salted hash. Even you, the developer, can't read users' passwords.

HttpOnly cookies

JavaScript on the page can't read an HttpOnly cookie, so an XSS bug can't easily steal it.

Vague error on login

Say 'Invalid email or password', not 'Email not found'. Specific errors tell attackers which emails exist.

Scope every query

Note.find({ owner: req.user.id }) makes sure each person only ever sees their own notes.

How it works

The full auth flow

  1. 1

    Sign up

    Validate email and password, hash the password with bcrypt, save the user.

  2. 2

    Log in

    Find the user, compare the password with the hash, create a token.

  3. 3

    Set cookie

    Send the token in an HttpOnly, Secure, SameSite cookie, not in localStorage.

  4. 4

    Protect routes

    Middleware reads the cookie, verifies the token and sets req.user.

  5. 5

    Own data only

    Every note query filters by owner: req.user.id.

  6. 6

    Log out

    Clear the cookie.

See it in code

Walk through the code

js
1import bcrypt from "bcrypt";2import jwt from "jsonwebtoken";3 4const cookieOptions = { httpOnly: true, secure: process.env.NODE_ENV === "production", sameSite: "lax" };5 6app.post("/api/auth/signup", async (req, res) => {7  const { email, password } = req.body;8  if (!email || !password || password.length < 8) {9    return res.status(400).json({ error: "Email and an 8+ character password are required" });10  }11  if (await User.findOne({ email })) return res.status(409).json({ error: "Email already registered" });12  const passwordHash = await bcrypt.hash(password, 12);13  const user = await User.create({ email, passwordHash });14  res.status(201).json({ id: user.id, email: user.email });15});16 17app.post("/api/auth/login", async (req, res) => {18  const user = await User.findOne({ email: req.body.email });19  const ok = user && (await bcrypt.compare(req.body.password, user.passwordHash));20  if (!ok) return res.status(401).json({ error: "Invalid email or password" });21  const token = jwt.sign({ id: user.id }, process.env.JWT_SECRET, { expiresIn: "7d" });22  res.cookie("token", token, { ...cookieOptions, maxAge: 7 * 24 * 60 * 60 * 1000 }).json({ email: user.email });23});24 25function requireAuth(req, res, next) {26  try {27    req.user = jwt.verify(req.cookies.token, process.env.JWT_SECRET);28    next();29  } catch {30    res.status(401).json({ error: "Please log in" });31  }32}33 34app.get("/api/notes", requireAuth, async (req, res) => {35  res.json(await Note.find({ owner: req.user.id }));36});37 38app.post("/api/auth/logout", (req, res) => {39  res.clearCookie("token", cookieOptions).status(204).end();40});

1/6Cookie settings: hidden from page JavaScript, HTTPS-only in production, and not sent on most cross-site requests.

The core of the system. Also install cookie-parser and add app.use(cookieParser()), and in React call fetch with credentials: "include" so the cookie is sent.

Save yourself the pain

Common mistakes

Oops

Saving the JWT in localStorage.

Do this instead

Any script on the page, including an injected one, can read localStorage. Use an HttpOnly cookie.

Oops

Hard-coding JWT_SECRET as "secret" in the code.

Do this instead

Generate a long random secret, keep it in .env locally and in your host's environment settings in production.

Your mission

Make the notes app multi-user

~240 min

Quick check

Why does the login route return 'Invalid email or password' even when only the email is wrong?

Go deeper

  1. 1Store sessions or tokens in HttpOnly cookies, not in localStorage.
  2. 2Show clear errors for a wrong password without saying whether the email exists.
  3. 3Add basic rate limiting to the login route.

Stuck on this topic? Ask an Insider

Get 1:1 guidance from people who've walked this exact path — on the InsideEdge app.

Download
InsideEdge