Full Stack JS

Chapter 15
0%

Chapter 15: Authentication

Learn how to verify who users are and protect your application from unauthorized access.

Auth vs AuthZ
Password Hashing
JWT Tokens
Login & Register
Auth Middleware
Quiz

Auth vs AuthZ

Think of a nightclub bouncer. Authentication is the bouncer checking your ID to verify you are who you claim to be. Authorization is the bouncer checking if your ticket allows you into the VIP section. First you prove WHO you are, then the system decides WHAT you can do.

Authentication (AuthN) = "Who are you?" — Verifying identity (login with email + password).

Authorization (AuthZ) = "What can you do?" — Checking permissions (admin vs regular user).

ConceptQuestionExample
AuthenticationWho are you?Login with email & password
AuthorizationWhat can you access?Only admins can delete users

Common Authentication Methods

In this course we will focus on JWT (JSON Web Token) authentication because it is the most common approach for REST APIs and single-page applications.

Password Hashing

Imagine feeding a document into a paper shredder. You can turn a document into shreds, but you can NEVER reassemble the shreds back into the original document. That is exactly what hashing does to passwords — it is a one-way transformation. Even if a hacker steals your database, they cannot reverse the hashed passwords back to plain text.
NEVER store passwords in plain text! If your database is ever compromised, every user's password would be exposed. Always hash passwords before saving them.

bcrypt — The Industry Standard

bcrypt is the most widely used password hashing library in Node.js. It automatically handles salting (adding random data before hashing) so that two users with the same password get different hashes.

Terminal — Install bcrypt
JavaScript — Hashing & Comparing Passwords
Salt rounds = 10 is the recommended default. Each increase doubles the computation time. 10 rounds takes ~100ms, which is fast enough for users but painfully slow for attackers trying millions of guesses.

JWT Tokens

A JWT is like a concert wristband. When you enter the venue (login), the bouncer checks your ticket and gives you a wristband. For the rest of the night, you just flash your wristband to access different areas — no need to show your ticket again. A JWT works the same way: after login, the server gives you a token, and you send it with every request to prove you are authenticated.

What is a JWT?

A JSON Web Token is a string with three parts separated by dots:

eyJhbGciOiJIUzI1NiJ9.eyJ1c2VySWQiOjEsInJvbGUiOiJhZG1pbiJ9.SflKxwRJSMeKKF2QT4fw

Header (algorithm)  ·  Payload (your data)  ·  Signature (verification)
Terminal — Install jsonwebtoken
JavaScript — Creating & Verifying JWTs
Never put sensitive data in the JWT payload! JWTs are encoded (Base64), NOT encrypted. Anyone can decode and read the payload. Only store IDs and roles — never passwords or personal data.

Login & Register

Now let's combine everything into a complete registration and login flow:

Registration Flow:
1. User sends email + password → 2. Server hashes password → 3. Server saves user to database → 4. Server returns success

Login Flow:
1. User sends email + password → 2. Server finds user by email → 3. Server compares password with hash → 4. Server generates JWT → 5. Server returns token
JavaScript — Register Route
JavaScript — Login Route
Notice the login route returns the same error message for both "user not found" and "wrong password." This is intentional — it prevents attackers from figuring out which emails are registered in your system.

Auth Middleware

Auth middleware is like a security checkpoint at an airport. Before you reach any gate (route), you must pass through security (middleware) and show your boarding pass (JWT token). If your pass is invalid, you get turned away before reaching the gate.
JavaScript — Auth Middleware

Using the Middleware

JavaScript — Protected Routes

How the Client Sends the Token

JavaScript — Frontend Sending JWT

📝 Chapter 15 Quiz

1. What is the difference between authentication and authorization?

They are the same thing
Authentication = who you are, Authorization = what you can access
Authentication = what you can access, Authorization = who you are
Authentication is only for admins

2. Why do we hash passwords before storing them?

To make them shorter
To encrypt them so we can decrypt later
So they cannot be reversed if the database is compromised
To make login faster

3. What are the three parts of a JWT?

Header, Payload, Signature
Username, Password, Token
Key, Value, Hash
Request, Response, Status

4. How does the client send a JWT token with each request?

In the URL as a query parameter
In the request body
As a cookie only
In the Authorization header as "Bearer <token>"

5. Why should the login route return the same error for "user not found" and "wrong password"?

To save code and keep it simple
To prevent attackers from discovering which emails are registered
Because the database cannot tell the difference
It does not matter, you can return different errors
← Chapter 14: REST API Chapter 16: Advanced Sequelize →