← Back to Course

Project 04: Authentication & Authorization System

Phase 1: Solidifying Fundamentals
Why This Project?

Almost every application needs user accounts. Authentication (proving who you are) and authorization (what you're allowed to do) are fundamental skills for any web developer.

Security is critical here. You'll learn how to store passwords safely using hashing, which means even if someone steals your database, they can't read the passwords. You'll also learn about JWT tokens for managing user sessions.

This project covers the complete user lifecycle - registration, email verification, login, password reset, and role-based access. These are features you'll implement over and over in your career.

Why This Project?

Almost every application needs user accounts. Authentication (proving who you are) and authorization (what you're allowed to do) are fundamental skills for any web developer.

Security is critical here. You'll learn how to store passwords safely using hashing, which means even if someone steals your database, they can't read the passwords. You'll also learn about JWT tokens for managing user sessions.

This project covers the complete user lifecycle - registration, email verification, login, password reset, and role-based access. These are features you'll implement over and over in your career.

Overview

Implement a complete authentication and authorization system with user roles, permissions, password reset, email verification, and protected routes. This project is crucial for understanding security fundamentals and building applications that handle user accounts safely.

You'll build a robust auth system that can be reused in future projects, learning industry-standard practices for handling passwords, tokens, and sensitive user data.

Learning Objectives

Understand authentication vs authorization

Implement secure password hashing with bcrypt

Use JWT tokens for stateless authentication

Build role-based access control (RBAC)

Implement password reset flows securely

Handle email verification

Protect routes on both frontend and backend

Core Requirements

User registration with email and password

Email verification with expiring tokens

User login with JWT token generation

Password hashing with bcrypt (10 rounds minimum)

JWT-based authentication for protected routes

Role-based access control (admin, user roles)

Password reset flow with email tokens

Logout functionality (token invalidation)

Protected API routes that require authentication

Frontend route guards for authenticated pages

User Data Model

id, email (unique), password (hashed), username

role (enum: user, admin), isVerified (boolean)

verificationToken, verificationExpires

resetPasswordToken, resetPasswordExpires

createdAt, updatedAt, lastLogin

API Endpoints

POST /api/auth/register - Create new user account

POST /api/auth/login - Authenticate and get JWT token

POST /api/auth/logout - Invalidate current session

GET /api/auth/verify/:token - Verify email address

POST /api/auth/forgot-password - Request password reset

POST /api/auth/reset-password/:token - Reset password

GET /api/auth/me - Get current user profile (protected)

PUT /api/auth/me - Update user profile (protected)

GET /api/users - Get all users (admin only)

DELETE /api/users/:id - Delete user (admin only)

Security Requirements

Never store passwords in plain text (use bcrypt)

Use strong JWT secrets stored in environment variables

Set reasonable JWT expiration (1 hour for access tokens)

Validate all inputs (email format, password strength)

Rate limit authentication endpoints (max 5 attempts per 15 min)

Use HTTPS in production (note this in documentation)

Implement CORS properly for frontend domain

Clear sensitive data from responses (no password field)

Frontend Features

Registration form with validation (email, password requirements)

Login form with "remember me" option

Forgot password form

Reset password form (accessed via email link)

User profile page showing account details

Protected routes that redirect to login if not authenticated

Admin dashboard visible only to admin users

Persistent login (token stored in localStorage or httpOnly cookie)

Automatic logout when token expires

Display user info in navigation bar when logged in

Technical Challenges

Create authentication middleware for protected routes

Implement role-checking middleware for authorization

Generate secure random tokens for email verification and password reset

Send emails (use nodemailer with test SMTP like Ethereal Email)

Handle JWT expiration and refresh tokens (optional but valuable)

Create React context or Redux slice for auth state

Implement private route component for React Router

Handle authentication errors gracefully (expired token, invalid credentials)

Password Reset Flow

User requests password reset by entering email

Server generates unique token and expiration time (1 hour)

Server sends email with reset link containing token

User clicks link and is taken to reset password form

Server validates token is not expired

User enters new password

Server hashes new password and clears reset token

User can now login with new password

Bonus Features

Implement refresh tokens for extended sessions

Add two-factor authentication (2FA) with email codes

Track login history (IP, device, timestamp)

Add social login (Google OAuth)

Implement account lockout after failed login attempts

Add password strength meter on registration

Send email notification on password change

Implement "remember this device" feature

Helpful Resources

bcrypt: Search for "bcrypt explained" or "password hashing tutorial". Understand why we hash passwords.

JWT: jwt.io has an introduction and debugger. Learn what's inside a JWT token.

Authentication vs Authorization: Search for articles explaining the difference. It's important to understand both.

Nodemailer: Nodemailer docs show how to send emails. Use Ethereal Email for testing (it's free).

Express Middleware: Learn about middleware authentication patterns. They protect your routes.

Security Best Practices: Search for "web authentication security" or "OWASP authentication guidelines".

Helpful Resources

bcrypt: Search for "bcrypt explained" or "password hashing tutorial". Understand why we hash passwords.

JWT: jwt.io has an introduction and debugger. Learn what's inside a JWT token.

Authentication vs Authorization: Search for articles explaining the difference. It's important to understand both.

Nodemailer: Nodemailer docs show how to send emails. Use Ethereal Email for testing (it's free).

Express Middleware: Learn about middleware authentication patterns. They protect your routes.

Security Best Practices: Search for "web authentication security" or "OWASP authentication guidelines".

Success Criteria

Users can register and receive verification email

Only verified users can login successfully

Protected routes return 401 for unauthenticated requests

Admin routes return 403 for non-admin users

Password reset flow works end-to-end

Tokens expire correctly and are validated properly

Passwords are never exposed in API responses

Frontend gracefully handles all auth states

Learning Resources

Study JWT structure and claims

Research bcrypt and password hashing algorithms

Learn about OWASP authentication best practices

Explore nodemailer for sending emails

Understand CORS and its security implications

Hint: Store JWT in httpOnly cookies instead of localStorage for better security against XSS attacks. If you use localStorage, be extra careful with third-party scripts.

Always validate tokens on the server side. Never trust the frontend to enforce authorization. A malicious user can modify frontend code, but they can't modify your server's validation logic.

Submit Your Project

Once you've completed this project, submit your GitHub repository link below: