A Firebase-based access control system for campus security featuring QR code scanning, face-based device binding, role-based authentication, and gate-specific access policies.
sau-vigil/
βββ backend/ # Firebase backend
β βββ functions/ # Cloud Functions
β β βββ src/
β β β βββ index.ts # Functions entry point
β β β βββ scan.ts # QR scanning logic
β β β βββ qr.ts # QR generation
β β β βββ auth.ts # Authentication
β β β βββ guard.ts # Guard decisions
β β β βββ parcels.ts # Parcel management
β β β βββ visitors.ts # Visitor sessions
β β β βββ face.ts # Face embedding (NEW)
β β β βββ seed.ts # Database seeding
β β β βββ middleware/ # Validation middleware
β β βββ package.json
β β βββ tsconfig.json
β βββ firebase.json # Firebase config
β βββ firestore.rules # Security rules
β βββ firestore.indexes.json # Database indexes
β
βββ vigilx/ # Frontend Expo app
β βββ app/ # Expo Router screens
β β βββ (auth)/ # Authentication screens
β β β βββ login.tsx
β β β βββ face-capture.tsx # Face registration (NEW)
β β βββ (student)/ # Student screens
β β β βββ qr.tsx # QR display
β β βββ (faculty)/ # Faculty screens
β β β βββ qr.tsx
β β βββ (guard)/ # Guard screens
β β β βββ scanner.tsx # QR scanner
β β βββ (worker)/ # Worker screens (NEW)
β β β βββ qr.tsx
β β βββ (visitor)/ # Visitor screens
β βββ services/ # API & Firebase services
β β βββ firebase.ts # Firebase initialization
β β βββ api.ts # API service layer
β βββ context/ # React context
β β βββ AuthContext.tsx # Auth state management
β βββ components/ # Reusable components
β βββ constants/ # App constants
β βββ hooks/ # Custom hooks
β βββ assets/ # Images, fonts
β βββ package.json
β βββ tsconfig.json
β
βββ docs/ # Documentation
β βββ API.md # API documentation
β βββ ARCHITECTURE.md # System architecture
β βββ DEPLOYMENT.md # Deployment guide
β βββ SECURITY.md # Security considerations
β βββ TESTING.md # Testing guide
β
βββ .kiro/ # Kiro spec files
β βββ specs/
β βββ firebase-backend-integration/
β βββ design.md
β βββ requirements.md
β βββ tasks.md
β
βββ README.md # This file
- QR Code Access Control: Time-limited QR codes with HMAC signatures
- Face-Based Device Binding: 128-dimensional face embeddings using face-api.js
- Role-Based Authentication: Student, Faculty, Guard, Worker, Visitor roles
- Gate-Specific Behavior: Different UI/logic for main gate vs hostel gates
- Offline Mode: Cached QR codes valid for 5 minutes
- Worker Subcategories: Mess workers (main gate only), Maintenance workers (all gates)
- Sub-2-Second Scans: Optimized for fast entry processing
- Parcel Management: Delivery notifications and tracking
- HMAC-SHA256 signature verification
- 60-second QR expiry
- Face embedding verification
- Device binding
- Role-based Firestore security rules
- Privacy-preserving embeddings (not reversible to photos)
- Node.js 18+
- Firebase CLI:
npm install -g firebase-tools - Expo CLI:
npm install -g expo-cli - Firebase project (dev and production)
- Navigate to backend directory:
cd backend- Install dependencies:
cd functions
npm install- Configure Firebase:
firebase login
firebase use --add # Select your Firebase project- Set environment variables:
firebase functions:config:set \
qr.secret="your-hmac-secret-key" \
app.region="asia-south1"- Deploy Firestore rules and indexes:
firebase deploy --only firestore:rules,firestore:indexes- Deploy Cloud Functions:
firebase deploy --only functions- Seed database (optional):
# Call the seedDatabase function via HTTP or Firebase console- Navigate to vigilx directory:
cd vigilx- Install dependencies:
npm install- Download face-api.js models:
# Download models from https://github.com/justadudewhohacks/face-api.js-models
# Place in vigilx/assets/models/- Configure Firebase:
- Update
vigilx/services/firebase.tswith your Firebase config
- Start development server:
npx expo start{
uid: string
name: string
email: string
role: "student" | "faculty" | "visitor" | "worker" | "guard"
workerType?: "mess" | "maintenance" | "other"
status: string
permissions: {
gates: string[] // For workers
}
deviceId?: string
faceEmbedding?: boolean
photoURL?: string // Firebase Storage URL
fcmToken?: string
}{
uid: string // Document ID
embedding: number[] // 128-dimensional vector
createdAt: Timestamp
deviceId: string
}{
logId: string
userId: string
userName: string
userRole: string
gateId: string
gateName: string
timestamp: Timestamp
systemDecision: "ALLOW" | "DENY" | "PENDING"
guardDecision?: "ALLOW" | "DENY"
flags: string[]
deviceId: string
faceVerified?: boolean
}{
logId: string
userId: string
gateId: string
guardId: string
expiresAt: Timestamp
createdAt: Timestamp
}{
parcelId: string
studentId: string
description: string
guardId: string
status: "pending" | "collected"
createdAt: Timestamp
collectedAt?: Timestamp
}{
gateId: string
name: string
type: "main" | "hostel"
location: string
}- User Photos: Stored in Firebase Storage at
user-photos/{uid}.jpg - Access: Public read (for guard display), authenticated write
- Purpose: Displayed to guards during QR scans
- Storage: Firestore collection
face_embeddings/{uid} - Format: 128-dimensional normalized vector (face-api.js)
- Privacy: Not reversible to original photo
- Access: Owner read/write only
- Purpose: Device binding verification
- Signature: HMAC-SHA256 with secret key
- Expiry: 60 seconds
- Offline Cache: Max 5 minutes
- Verification: Constant-time comparison
Run backend tests:
cd backend/functions
npm testRun frontend tests:
cd vigilx
npm testSee docs/TESTING.md for detailed testing guide.
- Students: Auto-allowed, minimal info displayed (name + photo only)
- Others: Pending decision, full details displayed, guard approval required
- All Users: Full details always displayed
- Decision: ALLOW/DENY/PENDING based on permissions
- Guard: Can approve/deny pending scans
- Access: Main gate only
- Permissions:
permissions.gates = ["main-gate"]
- Access: Main gate + all hostel gates
- Permissions:
permissions.gates = ["main-gate", "hostel-gate-1", "hostel-gate-2", ...]
- Access: Custom gate list
- Permissions: Configured per worker
- QR Display: Works offline with cached QR (max 5 minutes)
- QR Scanning: Requires network connectivity (no offline scanning)
- Auto-Refresh: QR refreshes automatically when network restored
- Indicator: Visual "Offline Mode" indicator shown to user
POST /scanQR- Scan QR code at gatePOST /generateQR- Generate QR code for userPOST /guardDecision- Submit guard decision for pending scanPOST /uploadFaceEmbedding- Upload face embedding and photoPOST /createParcel- Create parcel for studentPOST /collectParcel- Mark parcel as collectedPOST /createVisitorSession- Create visitor passPOST /validateVisitor- Validate visitor QR
See docs/API.md for detailed API documentation.
- Fork the repository
- Create a feature branch
- Make your changes
- Write tests
- Submit a pull request
[MIT License]
[Team-Logical Error]