# π Enterprise Authentication & Authorization System
A production-grade backend system implementing **JWT authentication, session management, RBAC β ABAC authorization, OTP login, Google OAuth, and advanced security protections**.
---
## π Overview
This project is a **secure, scalable authentication platform** designed for modern applications.
It supports:
- Multi-device sessions
- Role & attribute-based access control
- Audit logging & security monitoring
- Multiple login methods (Password, OTP, OAuth)
---
## π§ Architecture
Frontend (Next.js) β Backend (Node.js + Express) β Database (PostgreSQL + Prisma)
Security Layers:
- JWT + Refresh Tokens
- Session Store (DB)
- ABAC Policy Engine
- Audit Logs
---
## β¨ Features
### π Authentication
- JWT Access + Refresh Tokens
- Secure cookie-based session handling
- Refresh token rotation (anti-replay)
- Multi-device login support
### π Authorization
- RBAC (Admin / Handler / User)
- ABAC (attribute-based access control)
- Ownership & hierarchy enforcement
### π± Login Methods
- Email + Password
- OTP (phone-based)
- Google OAuth (restricted access)
### π‘οΈ Security
- HTTP-only cookies (XSS protection)
- CSRF protection
- Rate limiting (anti-brute force)
- Account lock after failed attempts
- Device & IP binding
- Token reuse detection
### π Monitoring
- Audit logs (login, logout, actions)
- Suspicious activity detection
- Admin security dashboard APIs
---
echo "# Authentication-RBAC" >> README.md
git init
git add README.md
git commit -m "first commit"
git branch -M main
git remote add origin https://github.com/Vipul99999/Authentication-RBAC.git
git push -u origin main
## π Project Structure
```
backend/
βββ prisma/
βββ src/
β βββ config/
β βββ middleware/
β βββ modules/
β β βββ auth/
β β βββ user/
β β βββ admin/
β β βββ otp/
β β βββ session/
β β βββ audit/
β β βββ location/
β βββ utils/
β βββ types/
β βββ constants/
βββ .env
βββ prisma.config.ts
βββ package.json
git clone <your-repo-url>
cd backendnpm installCreate .env:
PORT=5000
DATABASE_URL=postgresql://user:password@localhost:5432/db
JWT_ACCESS_SECRET=your_access_secret
JWT_REFRESH_SECRET=your_refresh_secret
ACCESS_TOKEN_EXPIRES=15m
REFRESH_TOKEN_EXPIRES=1d
GOOGLE_CLIENT_ID=your_google_client_id
GOOGLE_CLIENT_SECRET=your_google_secret
FRONTEND_URL=http://localhost:3000npx prisma generate
npx prisma migrate devnpm run dev1. User logs in (Password / OTP / OAuth)
2. Access Token issued (short-lived)
3. Refresh Token stored in HTTP-only cookie
4. Session stored in database
5. Token rotation on refresh
Admin β full access
Handler β limited access
User β minimal access
Access based on:
- User attributes (role, location)
- Resource attributes (owner, district)
- Context (IP, device)
Example:
Handler can access users only in same district
User can access only own profile
| Feature | Protection |
|---|---|
| JWT + Rotation | Prevent replay attacks |
| HTTP-only cookies | Prevent XSS |
| attacks | |
| Rate limiting | Prevent brute force |
| Account lock | Stop repeated failures |
| Device/IP binding | Prevent session hijacking |
| Token reuse detection | Detect stolen tokens |
| Endpoint | Description |
|---|---|
/admin/dashboard |
System stats |
/admin/security |
Security insights |
/admin/audit |
Audit logs |
/admin/sessions/:userId |
User devices |
/admin/security/suspicious |
Suspicious users |
| Endpoint | Description |
|---|---|
/otp/send |
Send OTP |
/otp/verify |
Verify OTP |
| Endpoint | Description |
|---|---|
/auth/register |
Create user |
/auth/login |
Login |
/auth/refresh |
Refresh token |
/auth/logout |
Logout |
/auth/google |
Google OAuth |
| Endpoint | Description |
|---|---|
/user/me |
Get profile |
/user/update |
Update profile |
/user/change-password |
Change password |
/user/set-pin |
Set PIN |
/user/change-pin |
Change PIN |
You can test APIs using:
- Postman
- Thunder Client
- curl
- Docker
- AWS EC2 / ECS
- Nginx (reverse proxy)
- HTTPS (SSL)
- Multi-factor authentication (MFA)
- WebAuthn / Passkeys
- Multi-OAuth providers (GitHub, Apple)
- Real-time security alerts (WebSockets)
- Policy DSL (AWS IAM style)
- Node.js + Express
- Prisma + PostgreSQL
- TypeScript
- Zod
- JWT + OAuth
Built as a production-grade authentication system for learning and real-world use.
Give it a star β and use it in your projects!