Authentication vs Authorization — Auth

The server answers two questions for every request that needs a login. Authentication answers "who are you?": you log in, then the browser sends a login mark…

Siti is logged in to School Portal. Her report card opens without a problem. Then she tries to enter a grade, and the server (the computer that runs School Portal) refuses. Her login is valid. Why is she still refused?

The answer: for every request to a page that needs a login, the server answers two questions, in order:

  1. Authentication (AuthN for short): "who are you?"
  2. Authorization (AuthZ): "what may you do?"

Picture a school. The guard at the gate checks the student card: that is authentication. Inside, only teachers may enter the staff room: that is authorization. Siti's student card is genuine, so she passes the gate. But the card doesn't let her into the staff room. School Portal works the same way: Siti's login is valid, but entering grades is a teacher's permission.

The example used across this track: School Portal

Every lesson in the Auth track uses one small app, School Portal, at https://portal.school.test. It has three users:

id Name Email Role
1 Siti [email protected] student
2 Mr. Andi [email protected] teacher
3 Mrs. Dewi [email protected] admin

It has three routes too. A route is an address on the server for one action, such as viewing a report. The word in front of each address (GET, POST, DELETE) is the kind of action: read, send, delete. A role is a user's position in the app (student, teacher or admin), and a permission is the right to do one kind of action:

Route What it does Permission needed Who has it
GET /students/1/grades view Siti's report grades:read Siti (her own report), teachers, admins
POST /grades enter a grade grades:write teachers, admins
DELETE /users/4 delete the account of Rizky, an alumnus users:delete admins

Authentication: who are you?

Authentication proves an identity. The steps:

  1. You send a credential (proof of who you are, usually an email and a password) to POST /login.
  2. The server compares the credential with the data it has stored.
  3. If they match, the server gives the browser a login mark. It is a session cookie or a token: both are small pieces of text that the browser keeps.
  4. The browser sends that login mark with every later request. So the server knows "this is Siti" without asking for the password again.

How the login mark is stored and read is covered in the Session, JWT and PASETO lessons.

A request with no login mark, or with one that is no longer valid, fails the first question.

Authorization: what may you do?

Once the server knows who you are, it checks permissions. The most common way: each role has a list of permissions. In the JavaScript below, PERMISSIONS is that list and authorize checks one permission. handle answers one request with a status code (a number in the server's answer, such as 200 for success):

const PERMISSIONS = {
  student: ["grades:read"],
  teacher: ["grades:read", "grades:write"],
  admin: ["grades:read", "grades:write", "users:delete"],
};

function authorize(user, permission) {
  if (!user) return false; // not logged in: no permissions at all
  const allowed = PERMISSIONS[user.role] || [];
  return allowed.includes(permission);
}

function handle(user, permission) {
  if (!user) return 401; // 1. authentication failed
  if (!authorize(user, permission)) return 403; // 2. authorization failed
  return 200;
}

const siti = { id: 1, name: "Siti", role: "student" };
const andi = { id: 2, name: "Mr. Andi", role: "teacher" };
console.log(handle(null, "grades:write")); // 401
console.log(handle(siti, "grades:write")); // 403
console.log(handle(andi, "grades:write")); // 200

This table holds the result of every sender and every route, exactly as the Lab answers it:

Sender GET /students/1/grades POST /grades DELETE /users/4
Guest (not logged in) 401 401 401
Siti (student) 200 403 403
Mr. Andi (teacher) 200 201 403
Mrs. Dewi (admin) 200 201 204

A request that passes is answered by the route itself, with different status codes:

The handle function above simplifies this: every request that passes gets 200.

401 or 403

So which one does Siti get when she tries to enter a grade? She is logged in, so she passes the first question. She fails the second question, because the student role doesn't have the grades:write permission. The answer is 403.

In Laravel

Laravel is a PHP framework: ready-made code for building server programs in the PHP language. Laravel separates the two questions clearly. A middleware is code that runs before a request reaches its route. The auth middleware answers "who are you?" and refuses with 401. Gates and Policies, two places to write permission rules, answer "what may you do?" and refuse with 403. This example creates one permission named delete-user with a Gate (only an admin may delete a user), then puts it on the delete-account route:

// app/Providers/AppServiceProvider.php, in the boot() method
Gate::define('delete-user', fn (User $user) => $user->role === 'admin');

// routes/api.php
Route::delete('/users/{user}', [UserController::class, 'destroy'])
    ->middleware(['auth', 'can:delete-user']);

Inside a middleware of your own, check the permission with cannot:

if ($request->user()->cannot('delete-user')) {
    abort(403);
}

BelajarWeb.dev itself works the same way. Routes that need a login use auth:api. Its guard (the part of Laravel that reads the login mark) reads a PASETO token from the Authorization header.

📌 Map of this track

The next lessons build the pieces of a login system one by one. First, storing passwords safely (Password Hashing). Then remembering who is logged in: through a session on the server, or through a token the client carries (JWT and PASETO). After that come refresh tokens, OAuth, and more detailed roles.

For a React SPA (a web app that runs as a single page) with a Laravel API and no Google login, a good setup usually uses two tokens. The first is a short access token (say PASETO, valid for 15 minutes) in the app's memory, sent with every request. The second is a long refresh token (say 30 days) used to ask for a new access token, kept in an HttpOnly cookie that JavaScript can't read. The refresh token is opaque, meaning a random string with nothing readable inside. If the access token leaks, it is only valid for 15 minutes. A 30-day JWT in localStorage is the opposite: any script on the page can steal it, and it stays valid for a long time.

How people attack a login is covered in the Security track, in the Password Hashing & Storage, Session Management, Cookie Security and JWT Security lessons. This Auth track focuses on building it right.

⚠️ Where do permission checks usually leak?

TL;DR: The server answers two questions for every request that needs a login. Authentication answers "who are you?": you log in, then the browser sends a login mark (a small piece of text the browser keeps) with every request. Authorization answers "what may you do?": the server checks the role (student, teacher or admin), the permission (the right to do one kind of action) and whose data it is. The server checks who first, then the permission. Unknown to the server: status code 401 Unauthorized. Known but without the permission: 403 Forbidden. In Laravel (a PHP framework), the auth middleware (code that runs before the request is handled) answers the first question, and permission rules in Gates/Policies (checked with can and cannot) answer the second. Every check happens on the server.

⚡ Two questions, in order

Meaning: Every request to a page that needs a login gets two questions from the server. Authentication: "who are you?". In a school, this is the guard at the gate who checks the student card. Authorization: "what may you do?". In a school, this is the rule that only teachers may enter the staff room.

⚡ Check who first, then the permission

Write:

function handle(user, permission) {
  if (!user) return 401; // 1. authentication failed
  if (!authorize(user, permission)) return 403; // 2. authorization failed
  return 200;
}

Meaning: handle answers one request with a status code (a number in the server's answer). user is the logged-in user, or null for a guest who is not logged in. You can't check the permissions of someone you don't know yet. So handle asks "who" first (fail: 401), then "what may they do" through the authorize function (fail: 403). For a guest, authorize(null, ...) must return false, not stop with an error.

⚡ 401 or 403

Guess first: Siti is logged in as a student and tries to enter a grade with POST /grades. 401 or 403?

403 Forbidden. The server already knows she is Siti, so she passes the first question. She fails the second question, because the student role doesn't have the permission to enter grades (grades:write).

Meaning: 401 Unauthorized means the server doesn't know who you are, so the client (such as a browser) sends the user to the login page. 403 Forbidden means the server knows who you are, but you don't have the permission. Logging in again won't help.

⚡ Every check happens on the server

Meaning: Hiding the "Delete" button from students only changes the screen. A request can still be sent without the button, so the server must check the permission on every request.

What you will learn