Stop 13 · Authentication & Web SecurityTopics · 1/10

Authentication vs Authorization

Part of the Web Development Roadmap

In short

Two long words that sound alike and get mixed up in interviews all the time. One asks 'who are you?', the other asks 'what are you allowed to do?'. Every secure app needs both.

Authentication checks who a user is, for example by logging in, while authorization checks what that user is allowed to do, such as whether they can edit a post.

The jaadu holds up an ID card at a hostel gate while clutching a single brass key, standing in front of a corridor of closed numbered doors.

Think of it like this

The hostel ID card and the room key

At the hostel gate, the guard checks your college ID card. That proves who you are: authentication. But your ID doesn't open every room. Your key opens only room 214, and only the warden's key opens the store room. What you can open is authorization.

Watch it happen

Deleting someone else's note

Rohan
Backend

POST /login (email, password)

1/5Authentication: the backend checks the password hash.

The big ideas

What you need to know

Authentication = who

Proving identity: password, OTP, Google sign-in. It happens once at login, then a cookie or token carries it.

Authorization = what

Checking permission for each action: own notes only, admins can delete any post, free users can't download.

401 vs 403

401 Unauthorized means 'we don't know who you are, log in'. 403 Forbidden means 'we know you, but you can't do this'.

Always on the server

Hiding a button in React is for convenience. The real authorization check must happen in the API.

See it in code

Walk through the code

js
1// 1. Authentication: who is this?2function requireLogin(req, res, next) {3  if (!req.user) return res.status(401).json({ error: "Please log in" });4  next();5}6 7// 2. Authorization: may this user do this?8app.delete("/api/notes/:id", requireLogin, async (req, res) => {9  const note = await Note.findById(req.params.id);10  if (!note) return res.status(404).json({ error: "Note not found" });11 12  const isOwner = note.owner.toString() === req.user.id;13  if (!isOwner && req.user.role !== "admin") {14    return res.status(403).json({ error: "Not your note" });15  }16 17  await note.deleteOne();18  res.status(204).end();19});

1/4Middleware that stops anyone not logged in with a 401.

req.user is set by earlier middleware that reads the session or token. You'll build that in the next lessons.

Save yourself the pain

Common mistakes

Oops

Checking only that a user is logged in before editing or deleting data.

Do this instead

Also check that this user owns the record or has the right role. Otherwise any user can change anyone's data by guessing ids.

Oops

Relying on hidden buttons in the UI for security.

Do this instead

Anyone can send requests with Postman or DevTools. Enforce every rule on the server.

Your mission

Permission table

~15 min

Quick check

A logged-in user tries to open the admin dashboard and isn't an admin. What status should the API return?

Go deeper

  1. 1Use a college ID card and a hostel room key as a way to remember the difference.
  2. 2List which actions in a blog need authentication and which need authorization too.
  3. 3Learn why 401 means not logged in and 403 means not allowed.

Also part of these roadmaps

More topics at this stop

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