Skip to content

0xShad/Attendify_Frontend

Repository files navigation

Attendify Frontend

Version: 1.2.0
Status: Production Ready
Last Updated: October 5, 2025

A modern, secure attendance management system built with Next.js 15.5.4 featuring HTTP-only cookie authentication, 3-layer JWT validation, and server-side route protection.

πŸš€ Quick Start

1. Install Dependencies

npm install

2. Configure Environment Variables

Copy the example environment file and update it with your backend API URL:

cp .env.local.example .env.local

Then edit .env.local:

NEXT_PUBLIC_AUTH_API_URL=http://localhost:8000/api/v1/auth
NEXT_PUBLIC_API_URL=http://localhost:8000/api/v1

You can optionally disable Google Cloud Vision if you don't want to use that integration (for example, to avoid charges while developing). Add to `.env.local`:

GOOGLE_VISION_ENABLED=false

3. Run the Development Server

npm run dev

Open http://localhost:3000 with your browser to see the result.

4. Start Building

You can start editing the page by modifying app/page.tsx. The page auto-updates as you edit the file.


πŸ“ Project Structure (overview)

This project follows a modular Next.js App Router layout with clear separation between pages, API proxies, feature components, and shared utilities. The structure below is an overview of the repository contents and where to look for important code.

attendify-frontend/
β”œβ”€β”€ src/
β”‚   β”œβ”€β”€ app/                # Next.js App Router: pages, layouts and server-side app-route proxies (app/api/*)
β”‚   β”‚   β”œβ”€β”€ api/            # Same-origin proxy routes and lightweight server APIs
β”‚   β”‚   β”œβ”€β”€ auth/           # Authentication pages (login, signup, logout)
β”‚   β”‚   β”œβ”€β”€ admin/          # Admin area pages and tools
β”‚   β”‚   β”œβ”€β”€ faculty/        # Faculty-facing pages
β”‚   β”‚   β”œβ”€β”€ student/        # Student area (classes, attendance, dashboard)
β”‚   β”‚   └── layout.tsx      # Root layout and top-level providers
β”‚   β”‚
β”‚   β”œβ”€β”€ components/        # Reusable UI and feature components (shadcn UI wrappers, portals, camera components)
β”‚   β”‚   β”œβ”€β”€ student-portal/ # Student-facing components and sub-features (classes, attendance flow)
β”‚   β”‚   β”œβ”€β”€ ui/             # Generic UI building blocks
β”‚   β”‚   └── ...
β”‚   β”‚
β”‚   β”œβ”€β”€ lib/               # Shared libraries and API clients
β”‚   β”‚   β”œβ”€β”€ api/           # Client API helpers (classes, attendance, fetchClient)
β”‚   β”‚   β”œβ”€β”€ liveness-tests/ # Liveness detection utilities and helper algorithms
β”‚   β”‚   └── utils/         # Formatting, avatars, detection helpers
β”‚   β”‚
β”‚   β”œβ”€β”€ hooks/             # Custom React hooks (auth, toast, mobile helpers)
β”‚   β”œβ”€β”€ contexts/          # React context providers (AuthContext)
β”‚   β”œβ”€β”€ config/            # App configuration (routes, auth settings)
β”‚   └── styles/            # Global CSS and Tailwind config

β”œβ”€β”€ public/                # Static assets (CSV, images, keys used in dev)
β”œβ”€β”€ docs/                  # Project documentation and guides
β”œβ”€β”€ keys/                  # Local developer keys (not for production)
β”œβ”€β”€ README.md
β”œβ”€β”€ package.json
β”œβ”€β”€ next.config.ts
└── tsconfig.json

β”‚ β”‚ β”œβ”€β”€ auth/ # auth pages (login, signup) β”‚ β”‚ β”œβ”€β”€ admin/ # admin pages and subroutes β”‚ β”‚ β”œβ”€β”€ student/ # student area (classes, attendance) β”‚ β”‚ β”œβ”€β”€ faculty/ # faculty area β”‚ β”‚ β”œβ”€β”€ layout.tsx β”‚ β”‚ └── page.tsx β”‚ β”‚ β”‚ β”œβ”€β”€ components/ # UI components and feature components β”‚ β”‚ β”œβ”€β”€ ui/ # shadcn UI wrappers β”‚ β”‚ β”œβ”€β”€ student-portal/ # student portal components (classes, take attendance) β”‚ β”‚ β”œβ”€β”€ face-camera.tsx # camera + liveness components β”‚ β”‚ └── ... β”‚ β”‚ β”‚ β”œβ”€β”€ config/ # app configuration (routes, auth settings) β”‚ β”œβ”€β”€ contexts/ # React context providers (AuthContext) β”‚ β”œβ”€β”€ hooks/ # custom hooks (use-auth, use-toast, use-mobile) β”‚ β”œβ”€β”€ lib/ # utilities and API clients β”‚ β”‚ β”œβ”€β”€ api/ # client-side API helpers (classes, attendance, fetchClient) β”‚ β”‚ β”œβ”€β”€ middleware/ # server-side middleware helpers β”‚ β”‚ └── utils/ # misc helpers (date, format, liveness) β”‚ β”‚ β”‚ └── styles/ # global CSS β”‚ β”œβ”€β”€ docs/ # πŸ“š Documentation and developer guides β”‚ β”œβ”€β”€ AUTHENTICATION_GUIDE.md β”‚ β”œβ”€β”€ MIDDLEWARE_GUIDE.md β”‚ └── ARCHITECTURE_GUIDE.md β”‚ β”œβ”€β”€ .env.local.example β”œβ”€β”€ next.config.ts β”œβ”€β”€ tsconfig.json β”œβ”€β”€ package.json └── README.md


---

## πŸ” Authentication System

### Overview

Production-ready authentication with **HTTP-only cookies**, **2-step OTP verification**, and **3-layer JWT validation** for maximum security and performance.

### πŸ“š Complete Documentation

| Guide                                                         | Description                                       | Lines |
| ------------------------------------------------------------- | ------------------------------------------------- | ----- |
| **[AUTHENTICATION_GUIDE.md](./docs/AUTHENTICATION_GUIDE.md)** | Complete auth system guide - flows, API, examples | 1000+ |
| **[MIDDLEWARE_GUIDE.md](./docs/MIDDLEWARE_GUIDE.md)**         | 3-layer optimization, performance, security       | 900+  |
| **[ARCHITECTURE_GUIDE.md](./docs/ARCHITECTURE_GUIDE.md)**     | System architecture, modules, patterns            | 1100+ |

### Authentication Features

#### πŸ”’ Security

- **HTTP-only Cookies** - Tokens inaccessible to JavaScript (XSS protection)
- **CSRF Protection** - SameSite cookie policy
- **Server-Side Validation** - Middleware validates before page loads
- **JWT Signature Verification** - Backend confirms token authenticity
- **User Existence Check** - Validates user still exists in database

#### ⚑ Performance (v1.2.0)

- **3-Layer Validation System:**
  - **Layer 1:** Client decode - Check expiration (0ms)
  - **Layer 2:** In-memory cache - Recent validations (1ms, 90-99% hit rate)
  - **Layer 3:** Backend API - Full validation (200ms, 1-10% of requests)
- **Result:** 20-200x faster authentication, 90-99% fewer backend calls

#### 🎯 User Flows

- **Registration:** Two-step with email OTP verification
- **Login:** Flexible OTP (backend controlled) or direct token
- **Dashboard Access:** Automatic protection via middleware
- **Logout:** Secure cookie clearing

### Quick Usage Example

```tsx
"use client";

import { useAuth } from "@/hooks/use-auth";

export default function LoginPage() {
  const { loginInitiate, loginVerify, isLoading, error } = useAuth();

  // Step 1: Initiate login
  const handleLogin = async () => {
    const result = await loginInitiate({
      username_or_email: "user@example.com",
      password: "SecurePass123",
    });

    if (result.requiresOTP) {
      // Show OTP input
      setShowOTP(true);
    }
    // If no OTP required, automatically redirects to dashboard
  };

  // Step 2: Verify OTP (if required)
  const handleVerifyOTP = async (code: string) => {
    await loginVerify({
      username_or_email: "user@example.com",
      code: code,
    });
    // Automatically redirects to dashboard
  };

  return <div>{/* Your login form */}</div>;
}

Adding Protected Routes

It's as simple as adding a route to the array!

// 1. Open: src/config/routes.ts

export const PROTECTED_ROUTES = [
  "/dashboard",
  "/admin",
  "/profile",
  "/my-new-route", // ← Add here
] as const;

// 2. That's it! Route is now automatically protected by middleware

What happens automatically:

  • βœ… Middleware intercepts all requests to /my-new-route
  • βœ… Validates JWT token (3-layer system)
  • βœ… Redirects to /auth/login if not authenticated
  • βœ… Allows access if token is valid

πŸ› οΈ Technology Stack

Core

  • Next.js 15.5.4 - React framework with App Router
  • React 19.1.0 - UI library
  • TypeScript 5.x - Type safety

UI & Styling

  • Shadcn UI - Component library
  • Tailwind CSS 4.1.14 - Utility-first styling
  • Radix UI - Accessible primitives
  • Lucide React - Icon library

Authentication & Validation

  • JWT - Token-based authentication
  • Zod 4.1.11 - Schema validation
  • HTTP-only Cookies - Secure token storage

Development

  • ESLint 9.x - Code linting
  • Turbopack - Fast bundler
  • TypeScript Strict Mode - Maximum type safety

πŸ“– API Reference

Backend Endpoints Required

Your backend must implement these endpoints:

Method Endpoint Description
POST /api/v1/auth/register/initiate Send OTP to email
POST /api/v1/auth/register/verify Verify OTP, create account
POST /api/v1/auth/login/initiate Login or request OTP
POST /api/v1/auth/login/verify Verify OTP, get token
GET /api/v1/users/me Get current user (for validation)

Frontend API Routes

These are handled automatically:

Method Endpoint Description
POST /api/auth/set-tokens Set HTTP-only cookies
POST /api/auth/clear-tokens Clear cookies (logout)

useAuth Hook API

const {
  // State
  user, // Current user object or null
  isLoading, // Loading state
  isAuthenticated, // Authentication status
  error, // Error message or null

  // Methods
  loginInitiate, // Step 1: Login (username/email + password)
  loginVerify, // Step 2: Verify OTP code
  registerInitiate, // Step 1: Register (email + username + password)
  registerVerify, // Step 2: Verify OTP code
  logout, // Clear tokens and redirect to login
  refreshUser, // Reload user data
  clearError, // Clear error state
} = useAuth();

πŸš€ Configuration

Environment Variables

# Backend API URLs
NEXT_PUBLIC_AUTH_API_URL=http://localhost:8000/api/v1/auth
NEXT_PUBLIC_API_URL=http://localhost:8000/api/v1

# Optional: Override defaults
# NODE_ENV=production  # Enables secure cookies

Local dev: temporarily bypass auth checks

If you don't have the backend available but want to develop the frontend, you can opt-in to a safe, local-only bypass of the middleware authentication checks.

Add the following to your .env.local (only for local development):

# When set to "1" or "true" (case-insensitive) and NODE_ENV is not "production",
# middleware will treat requests as authenticated so protected pages are accessible.
NEXT_PUBLIC_DEV_AUTH_BYPASS=1

Notes and safety recommendations:

  • This bypass is strictly opt-in and will only apply when NODE_ENV !== 'production'.
  • Never enable this in production or CI environments.
  • Use this for local UI development only. For more realistic testing consider using a mock API or MSW (Mock Service Worker).
  • Remember to remove or set the variable to 0 before committing or deploying.

Auth Configuration

Edit src/config/auth.ts to customize:

// Cookie settings
export const COOKIE_CONFIG = {
  ACCESS_TOKEN: {
    name: "access_token",
    maxAge: 7 * 24 * 60 * 60, // 7 days
  },
  REFRESH_TOKEN: {
    name: "refresh_token",
    maxAge: 30 * 24 * 60 * 60, // 30 days
  },
};

// Validation settings
export const VALIDATION_CONFIG = {
  CACHE_TTL_MS: 60 * 1000, // Cache for 1 minute
  CACHE_MAX_SIZE: 1000, // Max 1000 entries
  FAILURE_CACHE_TTL_MS: 10 * 1000, // Cache failures for 10s
  REQUEST_TIMEOUT_MS: 5 * 1000, // 5 second timeout
  TOKEN_EXPIRY_BUFFER_SECONDS: 30, // 30 second buffer
};

Route Configuration

Edit src/config/routes.ts to customize:

// Routes that require authentication
export const PROTECTED_ROUTES = [
  "/dashboard",
  "/admin",
  "/profile",
  "/settings",
] as const;

// Routes that redirect authenticated users (login, signup)
export const AUTH_ROUTES = ["/auth/login", "/auth/signup"] as const;

// Routes accessible to everyone
export const PUBLIC_ROUTES = ["/", "/about", "/contact"] as const;

πŸ“œ Available Scripts

# Development
npm run dev          # Start development server (http://localhost:3000)

# Production
npm run build        # Build for production
npm run start        # Start production server

# Code Quality
npm run lint         # Run ESLint
npm run type-check   # Check TypeScript (if configured)

πŸ” Performance Metrics

Authentication Speed (v1.2.0)

Scenario Before (v1.1.0) After (v1.2.0) Improvement
Cache Hit 200ms 1ms 200x faster
First Request 200ms 200ms Same
100 Requests 20 seconds 0.3 seconds 66x faster
Backend Calls 100 1-10 90-99% reduction

How It Works

  1. First Request: Validates with backend (200ms), caches result
  2. Subsequent Requests (within 1 min): Returns cached result (1ms)
  3. After Cache Expires: Re-validates with backend, updates cache

πŸ”’ Security Features

HTTP-Only Cookies

  • βœ… Tokens stored in HTTP-only cookies (not localStorage)
  • βœ… JavaScript cannot access tokens (XSS protection)
  • βœ… Automatic transmission with requests
  • βœ… Secure flag in production (HTTPS only)

Server-Side Protection

  • βœ… Next.js middleware validates before page loads
  • βœ… Cannot bypass with browser DevTools
  • βœ… Validates JWT signature with backend
  • βœ… Confirms user still exists

CSRF Protection

  • βœ… SameSite cookie policy (lax)
  • βœ… Prevents cross-site request forgery
  • βœ… Cookies not sent on cross-origin POST

Additional Security

  • βœ… Password validation (min 8 chars, uppercase, lowercase, number)
  • βœ… OTP verification for sensitive operations
  • βœ… Token expiration handling
  • βœ… Automatic logout on invalid token

πŸ“š Learn More

Next.js Resources

Project Documentation


πŸš€ Deployment

Deploy on Vercel

The easiest way to deploy is using the Vercel Platform:

  1. Push your code to GitHub
  2. Import repository in Vercel
  3. Add environment variables:
    NEXT_PUBLIC_AUTH_API_URL=https://your-backend.com/api/v1/auth
    NEXT_PUBLIC_API_URL=https://your-backend.com/api/v1
    
  4. Deploy!

Important: Ensure your backend:

  • βœ… Implements required API endpoints
  • βœ… Configures CORS for your frontend domain
  • βœ… Has /api/v1/users/me endpoint for validation

Production Checklist

  • Environment variables configured
  • Backend API accessible
  • CORS configured for frontend domain
  • HTTPS enabled (required for secure cookies)
  • /api/v1/users/me endpoint working
  • OTP email delivery configured
  • Error tracking setup (Sentry, etc.)
  • Performance monitoring (Vercel Analytics, etc.)

πŸ“ Version History

v1.2.0 (Current - October 5, 2025)

  • βœ… HTTP-only cookie authentication
  • βœ… 3-layer JWT validation (20-200x faster)
  • βœ… Server-side route protection
  • βœ… Modular configuration layer
  • βœ… Consolidated documentation (3 comprehensive guides)
  • βœ… Fixed login redirect bug

v1.1.0 (October 5, 2025)

  • βœ… Initial production release
  • βœ… Two-step authentication (OTP)
  • βœ… Registration with email verification
  • βœ… localStorage-based token management
  • βœ… Protected routes
  • βœ… Comprehensive documentation

πŸ‘₯ Contributing

Contributions are welcome! Please follow these steps:

  1. Fork the repository
  2. Create a feature branch (git checkout -b feature/amazing-feature)
  3. Commit your changes (git commit -m 'Add amazing feature')
  4. Push to the branch (git push origin feature/amazing-feature)
  5. Open a Pull Request

πŸ“„ License

This project is licensed under the MIT License.


🀝 Support

  • Documentation: Check the guides in /docs
  • Issues: Open an issue on GitHub
  • Email: Contact the development team

Built with ❀️ by the Attendify Team

About

Attendify - A facial recognition attendance system.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages