# 🧩 Mini project: ghép luồng auth hoàn chỉnh cho sản phẩm

By Better Wise

> This lesson is premium content. Sign in and purchase the course to view the full content.

Bài trước bạn đã cầm checklist rà code auth do AI sinh ra: secret hardcode, bỏ qua `exp`, tin `role` client gửi lên. Hôm nay đổi vai: không rà nữa mà **ghép** đăng ký, đăng nhập, refresh token, phân quyền và Google login thành một luồng chạy được, rồi tự tay phá nó.

Câu hỏi dán lên màn hình suốt buổi: token này ai giữ, sống bao lâu, bị lộ thì sao?

## Sản phẩm bạn ghép hôm nay

App "Ghi chú" mini, stack nào cũng được (Express + Postgres là đủ). Ba nhóm người dùng: khách chưa đăng nhập, `user` đã đăng nhập (tạo và xóa note của mình), `admin` (xem danh sách user, xóa note bất kỳ).

```mermaid
flowchart TD
    PW["🔑<br/>Đăng nhập mật khẩu"] --> Issue["🎫<br/>Cấp cặp token"]
    GG["🔴<br/>Đăng nhập Google"] --> Verify["✅<br/>Xác minh id_token"]
    Verify --> Issue
    Issue --> AT["⏱️<br/>Access token 15 phút<br/>giữ trong memory"]
    Issue --> RT["🍪<br/>Refresh token 7 ngày<br/>cookie httpOnly"]
    AT --> API["📓<br/>Gọi /notes và /admin"]
    API --> RBAC["🛡️<br/>Middleware RBAC"]
    RT --> Refresh["🔄<br/>/auth/refresh rotate"]
    Refresh --> Issue
```

Điểm quan trọng nhất trên hình: hai cách đăng nhập phải **hội tụ về cùng một chỗ cấp token**. Nếu mật khẩu trả một kiểu token, Google trả một kiểu khác, bạn sẽ có hai bộ bug khác nhau và gấp đôi thời gian debug.

## Năm mốc, làm theo thứ tự này

## In this lesson

- Nghiệm thu: tự tay bấm và tự tay phá
- Thẻ ôn nhanh cho luồng auth
- Nghiệm thu: luồng của bạn đã đủ chưa


---

## Contents

Part of [🔐 Xác thực và phân quyền: session, JWT và OAuth](https://everwise.study/courses/xac-thuc-va-phan-quyen-session-jwt-va-oauth.md)

- **🍪 Nền tảng xác thực: session, cookie và mật khẩu**
  - [🍪 Session và cookie: chọn khi nào](https://everwise.study/courses/xac-thuc-va-phan-quyen-session-jwt-va-oauth/session-va-cookie-chon-khi-nao.md)
  - [🎫 JWT: cấu trúc, cách kiểm tra và các lỗi hay gặp](https://everwise.study/courses/xac-thuc-va-phan-quyen-session-jwt-va-oauth/jwt-cau-truc-cach-kiem-tra-va-loi-hay-gap.md) (sign-in required)
  - [🔑 Hash mật khẩu: bcrypt, argon2 và lỗi thường gặp](https://everwise.study/courses/xac-thuc-va-phan-quyen-session-jwt-va-oauth/hash-mat-khau-bcrypt-argon2-va-loi-thuong-gap.md)
  - [✅ Checkpoint: nền tảng xác thực](https://everwise.study/courses/xac-thuc-va-phan-quyen-session-jwt-va-oauth/checkpoint-nen-tang-xac-thuc.md)
- **♻️ Refresh token và phân quyền theo vai trò**
  - [🔄 Refresh token: luồng cấp mới, rotation và thu hồi](https://everwise.study/courses/xac-thuc-va-phan-quyen-session-jwt-va-oauth/refresh-token-luong-cap-moi-rotation-va-thu-hoi.md)
  - [🛡️ Phân quyền theo vai trò (RBAC) trong API](https://everwise.study/courses/xac-thuc-va-phan-quyen-session-jwt-va-oauth/phan-quyen-theo-vai-tro-rbac-trong-api.md)
  - [✅ Checkpoint: refresh token và phân quyền](https://everwise.study/courses/xac-thuc-va-phan-quyen-session-jwt-va-oauth/checkpoint-refresh-token-va-phan-quyen.md)
- **🌐 Đăng nhập Google, AI viết auth và cách kiểm tra**
  - [🔵 Đăng nhập bằng Google: OAuth 2.0 ở mức thực dụng](https://everwise.study/courses/xac-thuc-va-phan-quyen-session-jwt-va-oauth/dang-nhap-bang-google-oauth-2-thuc-dung.md)
  - [🤖 Khi để AI viết luồng auth: sai lầm và checklist kiểm tra](https://everwise.study/courses/xac-thuc-va-phan-quyen-session-jwt-va-oauth/ai-viet-luong-auth-sai-lam-va-checklist-kiem-tra.md)
  - [🧩 Mini project: ghép luồng auth hoàn chỉnh cho sản phẩm](https://everwise.study/courses/xac-thuc-va-phan-quyen-session-jwt-va-oauth/mini-project-ghep-luong-auth-hoan-chinh.md) (premium content) — this lesson
  - [✅ Checkpoint: đăng nhập Google và kiểm tra luồng auth](https://everwise.study/courses/xac-thuc-va-phan-quyen-session-jwt-va-oauth/checkpoint-dang-nhap-google-va-kiem-tra-luong-auth.md) (premium content)
- [Course cheatsheet](https://everwise.study/courses/xac-thuc-va-phan-quyen-session-jwt-va-oauth/cheatsheet.md) (premium content)
