Skip to content

  • Projects
  • Groups
  • Snippets
  • Help
    • Loading...
    • Help
    • Submit feedback
    • Contribute to GitLab
  • Sign in
U
upgrade-data-crawler-fe
  • Project
    • Project
    • Details
    • Activity
    • Releases
    • Cycle Analytics
  • Repository
    • Repository
    • Files
    • Commits
    • Branches
    • Tags
    • Contributors
    • Graph
    • Compare
    • Charts
  • Issues 0
    • Issues 0
    • List
    • Board
    • Labels
    • Milestones
  • Merge Requests 0
    • Merge Requests 0
  • CI / CD
    • CI / CD
    • Pipelines
    • Jobs
    • Schedules
    • Charts
  • Wiki
    • Wiki
  • Snippets
    • Snippets
  • Members
    • Members
  • Collapse sidebar
  • Activity
  • Graph
  • Charts
  • Create a new issue
  • Jobs
  • Commits
  • Issue Boards
  • ThinhNC
  • upgrade-data-crawler-fe
  • Merge Requests
  • !4

Merged
Opened Sep 06, 2026 by ThinhNC@ThinhNC
  • Report abuse
Report abuse

feat(auth): implement Auth & RBAC system with Next.js BFF and HttpOnly cookies

Feature: Authentication & Role-Based Access Control (Auth & RBAC) with Next.js BFF

Summary

Implemented a production-ready Authentication and Role-Based Access Control (RBAC) architecture for data-crawler-fe, adhering to Next.js 16 App Router standards and OWASP security recommendations. Upgraded the session mechanism to a Next.js Backend-For-Frontend (BFF) pattern with HttpOnly, Secure, SameSite=Lax cookies, completely eliminating localStorage token storage to achieve 100% immunity against Cross-Site Scripting (XSS) token exfiltration.


Key Highlights & Changes

1. Security & Next.js BFF Architecture

  • HttpOnly Cookies: All authentication tokens (auth_token, refresh_token, user_role) are issued and managed exclusively on the server side via Next.js Route Handlers. JavaScript running in the browser cannot access or exfiltrate tokens.
  • Auth Route Handler (/api/auth/[action]): Handles server-side authentication operations (login, logout, me, refresh, etc.), dynamically sets SameSite=Lax cookies, and adapts Secure based on HTTPS/localhost protocols.
  • API Proxy Handler (/api/proxy/[...path]): Acts as a reverse proxy for internal crawler and dashboard API endpoints. Automatically injects Authorization: Bearer <token> and handles transparent, server-to-server silent token refreshes.
  • Zero Client-Side Token Storage: Removed all localStorage.getItem("auth_token") references from api-client.ts and auth-storage.ts.

2. Auth Context & Session Management

  • useAuth Hook & AuthProvider: Centralized authentication state (user, isAuthenticated, isLoading, role, isAdmin).
  • TanStack Query Integration: Profile fetching cached with query key ["auth", "me"] and automatic eviction upon receiving auth:unauthorized events.

3. Route Protection & RBAC Middleware

  • Next.js Edge Middleware (middleware.ts):
    • Guards internal application routes (redirects unauthenticated users to /login?redirect=...).
    • Automatically redirects authenticated users away from auth pages back to /.
    • Enforces role requirements on protected administrative endpoints (/admin, /users, /audit-logs, /roles), routing unauthorized roles to a dedicated /forbidden (403) page.
    • Sanitized redirect parameters to prevent redirect loops.

4. Authentication Pages & UI/UX

  • Full Auth Suite (src/app/(auth)/...):
    • /login: Form with show/hide password toggle, remember-me checkbox, and natural tab key navigation sequence.
    • /register: Real-time password strength checklist (8+ chars, uppercase, lowercase, numbers, special characters).
    • /forgot-password: Password recovery request with a 60-second cooldown resend timer.
    • /reset-password: Token extraction from query parameters and secure password reset.
    • /verify-email: 4-state account activation flow (verifying, success, error, manual token entry).
  • AppShell Layout Separation: Completely hides the main dashboard Navbar and footer on auth pages, providing a focused full-screen experience with minimal Brand links and Language/Theme controls.
  • Design System & Accessibility:
    • Fixed input text visibility in Light Mode by migrating hardcoded styles to standard CSS variable tokens (text-foreground, bg-background, border-input).
    • Full bilingual support (Vietnamese & English) via useLanguage.
    • Seamless support for 3 theme modes: Light, Dark, and System.

Check out, review, and merge locally

Step 1. Fetch and check out the branch for this merge request

git fetch origin
git checkout -b feat/auth-rbac-bff-system origin/feat/auth-rbac-bff-system

Step 2. Review the changes locally

Step 3. Merge the branch and fix any conflicts that come up

git fetch origin
git checkout origin/develop
git merge --no-ff feat/auth-rbac-bff-system

Step 4. Push the result of the merge to GitLab

git push origin develop

Note that pushing to GitLab requires write access to this repository.

Tip: You can also checkout merge requests locally by following these guidelines.

  • Discussion 0
  • Commits 1
  • Changes 24
Assignee
No assignee
Assign to
None
Milestone
None
Assign milestone
Time tracking
0
Labels
None
Assign labels
  • View project labels
Reference: ThinhNC/upgrade-data-crawler-fe!4

Revert this merge request

This will create a new commit in order to revert the existing changes.

Switch branch
Cancel
A new branch will be created in your fork and a new merge request will be started.

Cherry-pick this merge request

Switch branch
Cancel
A new branch will be created in your fork and a new merge request will be started.