How to Build a Production-Ready Authentication & Authorization System with Angular and NestJS

Iniciado por joomlamz, Ontem às 19:15

Respostas: 0   |   Visualizações: 10

Tópico anterior - Tópico seguinte

0 Membros e 1 Visitante estão a ver este tópico.


                     How to Build a Production-Ready Authentication & Authorization System with Angular and NestJS
               




Tópico:
                     How to Build a Production-Ready Authentication & Authorization System with Angular and NestJS
               
Categoria: Tutoriais | FreeCodeCamp Premium
Idioma Principal: Português (Conteúdo de Tecnologia)

Conteúdo do Tutorial / Guia Passo a Passo:
-------------------------------------------------------------------------
Authentication is one of those topics that seems like it would be simple. A user submits a username and password, the server verifies the credentials, and the application returns a token.

But a secure authentication system involves more than just generating a JSON Web Token (JWT). You also need to think about password storage, access and refresh tokens, token rotation, session management, logout, concurrent requests, authorization, CSRF protection, rate limiting, input validation, CORS, secure cookies, database consistency, security monitoring, and...the list goes on.

In this handbook, we'll examine an authentication and authorization architecture for a full-stack application built with Angular, NestJS, MongoDB, and Redis. Our goals here are to understand the responsibilities of each component, how they work together, and which additional controls we need to verify before deploying a real application.

The design uses a short-lived access JWT held in Angular memory and an opaque refresh token delivered through an HttpOnly cookie. The backend stores a hash of the refresh token in MongoDB, uses session records to track logins, and uses Redis for rate limiting and other temporary state. The server (not the Angular UI) remains the security boundary for authentication and authorization.

Table of Contents

• Prerequisites

• 1. Authentication vs Authorization

• 2. The Architecture

• 3. What We Will Build

• 4. Why Not Store Everything in localStorage?

• 5. Project Structure

• 6. Install Dependencies

• 7. Environment Configuration

• 8. User Model

• 9. Password Hashing

• 10. Registration
• Database Constraints Are Still Required

• 11. Sessions

• 12. Refresh Tokens
• Generating Refresh Tokens

• Hashing the Refresh Token

• 13. Access JWT
• Creating the Access Token

• 14. Login
• The Login Flow

• Login Implementation

• Why HttpOnly?

• 15. Angular AuthService
• Angular Interceptor

• 16. Handling Expired Access Tokens
• The Concurrency Problem

• Angular Refresh Lock

• Refresh Endpoint

• 17. Refresh Token Rotation
• Reuse Detection

• 18. Why We Have Sessions

• 19. JWT Authentication in NestJS
• JwtAuthGuard

• Angular Guard Isn't Security

• 20. RBAC
• @Roles()

• RolesGuard

• Using RBAC

• 401 vs 403

• Public Routes

• 21. CSRF
• SameSite

• CSRF Token

• CSRF Guard

• 22. Redis
• Rate Limiting

• Don't Rate Limit Only by IP

• Endpoints Worth Protecting

• 23. CORS

• 24. Logout
• Logout All Devices

• 25. MongoDB Transaction for Rotation

• 26. Complete Authentication Flow
• Login

• Normal API Request

• Expired Access Token

• Refresh Token Theft

• Authorization Flow

• 27. Security Review

• 28. Things We Should Add Later

• 29. The Most Important Architectural Lessons

• 30. Final Architecture

• Explore the Source Code

Prerequisites

This tutorial will be approachable for you if you already know the basics of JavaScript and TypeScript and have built a small application with Angular or another frontend framework. Familiarity with HTTP requests, REST APIs, and basic database operations will make the examples easier to follow. You don't need to be a security specialist, but you should be comfortable reading backend TypeScript.

You'll also need Node.js and npm, the Angular CLI, a NestJS project, MongoDB, Redis, and a tool for sending HTTP requests such as Postman or
curl. Git and an editor such as VS Code are also useful. You can run MongoDB and Redis lo

... [O tutorial continua no link abaixo] ...


Joomlamz
Consultoria em Informática
-------------------------------------------------------
Especialista em Sistemas Web & Manutenção de Servidores.
A desenvolver o novo AplPortal com suporte a PHP 8.
Precisa de ajuda profissional? Contacte-me.

Tags: