Skip to content

A secure collaborative platform to share and view lab solutions with OTP authentication, version control, and code file management.

Notifications You must be signed in to change notification settings

TryOmar/LabShare

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

LabShare - ITI Share Solutions

A collaborative platform for ITI students to share and view lab solutions. Built with Next.js 16, React 19, TypeScript, Supabase, and Tailwind CSS.

πŸš€ Features

  • Secure Authentication: OTP-based email authentication with JWT tokens and server-side sessions (device-bound with fingerprinting)
  • Terms & Conditions: First-time users must accept terms before accessing the platform
  • Lab Management: Browse labs by course and track with many-to-many course-track relationships
  • Solution Sharing: Upload and share lab solutions with code files and file attachments
  • Dual File Support:
    • Code Files: Direct code editing with syntax highlighting (JavaScript, TypeScript, Python, C++, Java, etc.)
    • File Attachments: Upload PDFs, images, and other files stored in Supabase Storage
  • Comments System: Comment on submissions with markdown support (bold, code blocks, inline code)
  • View Tracking: Track how many times your solutions are viewed
  • Access Control: Lab unlocking system - submit your solution to unlock others
  • Date & Time Display: All dates show both date and time (hours and minutes)
  • Server-Side Security: All database operations validated server-side with proper authorization

πŸ› οΈ Tech Stack

  • Framework: Next.js 16 (App Router)
  • Language: TypeScript
  • UI Library: React 19
  • Styling: Tailwind CSS 4
  • UI Components: Radix UI
  • Database: Supabase (PostgreSQL)
  • Storage: Supabase Storage (for file attachments)
  • Authentication: Custom OTP flow with JWT tokens and server-side sessions (device fingerprinting)
  • Email: Resend
  • Syntax Highlighting: react-syntax-highlighter
  • Deployment: Vercel (recommended)

πŸ“‹ Prerequisites

  • Node.js 18+
  • npm, yarn, or pnpm
  • Supabase account and project
  • Resend account (for email OTP)
  • PostgreSQL database (via Supabase)

πŸ”§ Setup

1. Clone the repository

git clone <repository-url>
cd LabShare

2. Install dependencies

npm install
# or
yarn install
# or
pnpm install

3. Environment Variables

Create a .env.local file in the root directory:

# Supabase
NEXT_PUBLIC_SUPABASE_URL=your_supabase_project_url
NEXT_PUBLIC_SUPABASE_ANON_KEY=your_supabase_anon_key
SUPABASE_SERVICE_ROLE_KEY=your_supabase_service_role_key  # Required for storage operations

# Resend (for email OTP)
RESEND_API_KEY=your_resend_api_key

# JWT Authentication
JWT_SECRET=your_jwt_secret_key  # Generate a secure random string (e.g., openssl rand -hex 32)
JWT_EXPIRES_IN=7d  # JWT expiration time (default: 7d, supports formats like "1h", "30d", etc.)

# Database (optional - for migration script)
DATABASE_URL=postgresql://postgres.[PROJECT-REF]:[PASSWORD]@[HOST]:[PORT]/postgres

# Node Environment
NODE_ENV=development

4. Supabase Storage Setup

Create a storage bucket in Supabase:

  1. Go to Supabase Dashboard β†’ Storage
  2. Create a new bucket named submission-attachments
  3. Set it to Public (or configure RLS policies as needed)
  4. Enable the bucket for file uploads

5. Database Setup

Run the database migrations:

npm run db:migrate

This will execute the SQL scripts in the scripts/ directory in order:

  • 001_create_schema.sql - Creates database schema with all tables and RLS policies
  • 002_seed_data.sql - Seeds initial data (tracks, courses, labs, students)
  • 003_fix_timestamps_utc.sql - Fixes timestamp timezone issues
  • 004_remove_versions_separate_code_attachments.sql - Migration to separate code files from attachments
  • 005_add_performance_indexes.sql - Adds performance indexes for common queries
  • 006_create_sessions_table.sql - Creates sessions table for JWT-based authentication

Note: See scripts/README.md for detailed migration instructions and alternative methods.

6. Run the development server

npm run dev

Open http://localhost:3000 in your browser.

πŸ“ Project Structure

LabShare/
β”œβ”€β”€ app/                    # Next.js App Router
β”‚   β”œβ”€β”€ api/               # API routes (server-side)
β”‚   β”‚   β”œβ”€β”€ auth/         # Authentication endpoints
β”‚   β”‚   β”‚   β”œβ”€β”€ accept-terms/  # Terms acceptance
β”‚   β”‚   β”‚   β”œβ”€β”€ check-terms/   # Terms verification
β”‚   β”‚   β”‚   β”œβ”€β”€ logout/       # Logout
β”‚   β”‚   β”‚   β”œβ”€β”€ status/       # Auth status
β”‚   β”‚   β”‚   β”œβ”€β”€ request-otp/  # Request OTP
β”‚   β”‚   β”‚   β”œβ”€β”€ send-otp/     # Send OTP email
β”‚   β”‚   β”‚   └── verify-otp/   # Verify OTP code
β”‚   β”‚   β”œβ”€β”€ dashboard/    # Dashboard data
β”‚   β”‚   β”œβ”€β”€ lab/          # Lab endpoints
β”‚   β”‚   β”œβ”€β”€ labs/         # Labs listing
β”‚   β”‚   └── submission/   # Submission endpoints
β”‚   β”‚       └── [id]/
β”‚   β”‚           β”œβ”€β”€ comments/  # Comment management
β”‚   β”‚           └── route.ts   # Submission CRUD
β”‚   β”œβ”€β”€ dashboard/         # Dashboard page
β”‚   β”œβ”€β”€ lab/              # Lab detail pages
β”‚   β”‚   └── [id]/
β”‚   β”‚       β”œβ”€β”€ locked/   # Locked lab page
β”‚   β”‚       └── page.tsx  # Lab detail page
β”‚   β”œβ”€β”€ labs/             # Labs listing page
β”‚   β”œβ”€β”€ login/             # Login page
β”‚   β”œβ”€β”€ terms/             # Terms & conditions page
β”‚   β”œβ”€β”€ submission/        # Submission detail pages
β”‚   β”‚   └── [id]/page.tsx
β”‚   β”œβ”€β”€ last-updates/      # Changelog page
β”‚   β”œβ”€β”€ layout.tsx         # Root layout
β”‚   └── page.tsx           # Home page (redirects)
β”œβ”€β”€ components/            # React components
β”‚   β”œβ”€β”€ ui/               # Reusable UI components (Radix UI)
β”‚   β”œβ”€β”€ navigation.tsx    # Navigation bar
β”‚   β”œβ”€β”€ comments-section.tsx  # Comments component
β”‚   β”œβ”€β”€ last-updates.tsx  # Changelog component
β”‚   └── theme-provider.tsx
β”œβ”€β”€ lib/                   # Utility libraries
β”‚   β”œβ”€β”€ auth.ts           # Authentication utilities
β”‚   β”œβ”€β”€ auth/             # Authentication modules
β”‚   β”‚   β”œβ”€β”€ jwt.ts        # JWT signing and verification
β”‚   β”‚   β”œβ”€β”€ sessions.ts  # Session management
β”‚   β”‚   └── fingerprint.ts # Device fingerprinting
β”‚   β”œβ”€β”€ storage.ts        # Storage operations (Supabase)
β”‚   β”œβ”€β”€ supabase/         # Supabase clients
β”‚   β”‚   β”œβ”€β”€ client.ts     # Client-side client
β”‚   β”‚   β”œβ”€β”€ server.ts     # Server-side client
β”‚   β”‚   β”œβ”€β”€ middleware.ts # Middleware client
β”‚   β”‚   └── service.ts    # Service role client
β”‚   └── utils.ts          # General utilities (formatDateTime, etc.)
β”œβ”€β”€ scripts/               # Database migration scripts
β”‚   β”œβ”€β”€ 001_create_schema.sql
β”‚   β”œβ”€β”€ 002_seed_data.sql
β”‚   β”œβ”€β”€ 003_fix_timestamps_utc.sql
β”‚   β”œβ”€β”€ 004_remove_versions_separate_code_attachments.sql
β”‚   β”œβ”€β”€ 005_add_performance_indexes.sql
β”‚   β”œβ”€β”€ 006_create_sessions_table.sql
β”‚   β”œβ”€β”€ run-migrations.js  # Migration runner
β”‚   └── README.md         # Migration guide
β”œβ”€β”€ data/                  # Static data
β”‚   └── lastUpdates.ts     # Changelog data
β”œβ”€β”€ hooks/                 # React hooks
β”œβ”€β”€ public/               # Static assets
└── styles/               # Global styles

πŸ” Security Features

  • Server-Side Validation: All database queries run server-side with authentication checks
  • JWT Authentication: Secure JWT tokens with server-side session validation
  • Device Fingerprinting: Sessions are bound to device fingerprints to detect token theft
  • Session Management: Server-side session revocation and multi-device support
  • HttpOnly Cookies: Secure cookie-based token storage (httpOnly, secure, sameSite)
  • Terms Acceptance: First-time users must accept terms before accessing the platform
  • Authorization Checks: Users can only access/modify their own data
  • Input Validation: Server-side validation for all API routes
  • SQL Injection Protection: Using Supabase client with parameterized queries
  • Row Level Security (RLS): All tables have RLS policies enabled
  • File Upload Security: Filename sanitization and path validation

🎯 API Routes

All API routes require authentication via JWT tokens in httpOnly cookies (except login/OTP endpoints):

Authentication

  • POST /api/auth/request-otp - Request OTP code
  • POST /api/auth/send-otp - Send OTP email
  • POST /api/auth/verify-otp - Verify OTP and create session
  • GET /api/auth/status - Check authentication status
  • GET /api/auth/check-terms - Check if terms are accepted
  • POST /api/auth/accept-terms - Accept terms and conditions
  • POST /api/auth/logout - Logout user

Dashboard & Labs

  • GET /api/dashboard - Get dashboard data (student, track, courses, recent submissions)
  • GET /api/labs - Get labs for authenticated student (grouped by course)
  • GET /api/lab/[id] - Get specific lab data with submissions

Submissions

  • GET /api/submission/[id] - Get submission details (with code files and attachments)
  • POST /api/submission/upload - Upload/update submission (code files + attachments)
  • DELETE /api/submission/[id] - Delete submission (and associated files)

Comments

  • GET /api/submission/[id]/comments - Get comments for a submission
  • POST /api/submission/[id]/comments - Add a comment
  • DELETE /api/submission/[id]/comments/[commentId] - Delete a comment

πŸ“ Database Schema

Core Tables

  • students - Student information (name, email, track_id)
  • tracks - ITI tracks (code, name)
  • courses - Course information (name, description)
  • course_track - Many-to-many relationship between courses and tracks
  • labs - Lab assignments (course_id, lab_number, title, description)
  • auth_codes - OTP codes for authentication (student_id, code, expires_at, used)
  • sessions - User sessions for JWT authentication (user_id, fingerprint, last_seen, revoked)

Submission Tables

  • submissions - Student submissions (student_id, lab_id, title, view_count)
  • submission_code - Code files (submission_id, filename, language, content)
  • submission_attachments - File attachments stored in Supabase Storage (submission_id, filename, storage_path, mime_type, file_size)

Interaction Tables

  • comments - Comments on submissions (submission_id, student_id, content)
  • lab_unlocks - Track which labs students have unlocked (student_id, lab_id, unlocked_at)

Key Relationships

  • Students belong to one Track (many-to-one)
  • Courses can belong to multiple Tracks (many-to-many via course_track)
  • Labs belong to one Course (many-to-one)
  • Submissions belong to one Student and one Lab (one-to-one per student-lab)
  • Submissions have multiple Code Files and Attachments (one-to-many)
  • Submissions have multiple Comments (one-to-many)
  • Lab unlocks track which labs a student can access (many-to-many)

πŸ—„οΈ Storage

The application uses Supabase Storage for file attachments:

  • Bucket: submission-attachments
  • Structure: submissions/{submissionId}/{filename}
  • File Types: PDFs, images, documents, and other non-code files
  • Code Files: Stored directly in database (submission_code table) for syntax highlighting

πŸš€ Deployment

Vercel (Recommended)

  1. Push your code to GitHub
  2. Import project in Vercel
  3. Add environment variables in Vercel dashboard
  4. Deploy!

Environment Variables for Production

Make sure to set all environment variables in your deployment platform:

  • NEXT_PUBLIC_SUPABASE_URL
  • NEXT_PUBLIC_SUPABASE_ANON_KEY
  • SUPABASE_SERVICE_ROLE_KEY
  • RESEND_API_KEY
  • JWT_SECRET - Secure random string for signing JWTs
  • JWT_EXPIRES_IN - JWT expiration time (default: "7d")
  • NODE_ENV=production
  • DATABASE_URL (optional, for migrations)

πŸ“œ Available Scripts

  • npm run dev - Start development server
  • npm run build - Build for production
  • npm run start - Start production server
  • npm run lint - Run ESLint
  • npm run db:migrate - Run database migrations

πŸ”„ Migration History

The database has evolved through several migrations:

  1. 001_create_schema.sql - Initial schema creation
  2. 002_seed_data.sql - Initial data seeding
  3. 003_fix_timestamps_utc.sql - UTC timestamp fixes
  4. 004_remove_versions_separate_code_attachments.sql - Removed version system, separated code files from attachments
  5. 005_add_performance_indexes.sql - Added performance indexes for common queries
  6. 006_create_sessions_table.sql - Created sessions table for JWT-based authentication with device fingerprinting

🎨 UI Features

  • Syntax Highlighting: Code files support multiple languages with Prism.js
  • Markdown Support: Comments support bold (**text**), inline code (`code`), and code blocks ( code )
  • Date Formatting: All dates display with time (e.g., "12/25/2023, 3:45 PM")
  • Responsive Design: Mobile-friendly interface with Tailwind CSS
  • Dark Mode Ready: Theme provider included (can be enabled)

🀝 Contributing

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

Contributors are credited in the "Last Updates" section on the dashboard.

πŸ“„ License

This project is private and proprietary.

πŸ‘₯ Authors

  • ITI Development Team

πŸ™ Acknowledgments

πŸ“ž Support

For issues and questions, please open an issue in the repository or contact the development team.


Note: This is a private project for ITI students. Unauthorized access is prohibited.

About

A secure collaborative platform to share and view lab solutions with OTP authentication, version control, and code file management.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors 8