Código executável completo demonstrando a integração de sistemas Full Stack.


🗺️ Fluxo de Integração

flowchart LR
    BROWSER["🌐 Navegador"] -->|"1. POST /login (credenciais)"| API["🏛️ Backend Auth Service"]
    API -->|"2. Retorna Access Token (Memória) + Refresh Token (Cookie HTTP-Only)"| BROWSER
    BROWSER -->|"3. Requisição Protegida (Header Bearer JWT)"| API
    API -->|"4. Valida Roles (RBAC: ADMIN / USER)"| DB["PostgreSQL"]
    
    style BROWSER fill:#e1f5fe,stroke:#03a9f4,stroke-width:1px
    style API fill:#e8f5e9,stroke:#4caf50,stroke-width:2px
    style DB fill:#fff3e0,stroke:#ff9800,stroke-width:1px

📄 Código de Demonstração (auth_middleware.ts)

// auth_middleware.ts (Verificação de RBAC)
import { Request, Response, NextFunction } from 'express';
import jwt from 'jsonwebtoken';

export interface UsuarioAutenticado {
  id: string;
  email: string;
  role: 'ADMIN' | 'GESTOR' | 'USUARIO';
}

export function autorizarRoles(...rolesPermitidas: string[]) {
  return (req: Request, res: Response, next: NextFunction) => {
    const authHeader = req.headers.authorization;
    if (!authHeader?.startsWith('Bearer ')) {
      return res.status(401).json({ erro: 'Token não fornecido' });
    }

    try {
      const token = authHeader.split(' ')[1];
      const payload = jwt.verify(token, process.env.JWT_SECRET || 'chave_secreta') as UsuarioAutenticado;

      if (!rolesPermitidas.includes(payload.role)) {
        return res.status(403).json({ erro: 'Acesso negado para o seu perfil' });
      }

      req.user = payload;
      next();
    } catch {
      return res.status(401).json({ erro: 'Token inválido ou expirado' });
    }
  };
}

🚀 Saída Esperada de Execução

[Auth Integration Test]
POST /api/login -> 200 OK (Set-Cookie: refreshToken; HttpOnly; Secure)
GET /api/admin/relatorios (Role: USUARIO) -> 403 Forbidden
GET /api/admin/relatorios (Role: ADMIN)   -> 200 OK

🧭 Navegação Rápida

| 📖 Teoria | 📊 Slides | 🧠 Quiz | 💻 Exemplos | 🧩 Exercícios | | :— | :— | :— | :— | :— | | Ler Tópico | Ver Slides | Fazer Quiz | Ver Código | Praticar |


⬅️ Voltar ao Sumário dos Projetos