Skip to content

Latest commit

 

History

History
231 lines (191 loc) · 6.2 KB

File metadata and controls

231 lines (191 loc) · 6.2 KB

✅ College-Based Authentication System - Implementation Complete

📋 Summary of Changes

BACKEND

1. Database Schema Updates

File: backend/src/utils/database.ts

  • ✅ Added colleges table
  • ✅ Updated users table with new fields:
    • username, prn, batch, department, college_id
    • year_of_study, bio, avatar_url
    • verified, linkedin_id, approval_status

2. PRN Validation Utility

File: backend/src/utils/prnValidation.ts

  • ✅ Format validation (6-20 alphanumeric)
  • ✅ Duplicate check
  • ✅ Auto uppercase conversion

3. LinkedIn OAuth Setup

File: backend/src/config/passport.ts

  • ✅ Passport LinkedIn strategy
  • ✅ Auto-fetch college from LinkedIn education
  • ✅ Auto college record creation

4. Auth Controller Updates

File: backend/src/controllers/authController.ts

  • ✅ Enhanced registration with PRN & college fields
  • ✅ LinkedIn OAuth callback handler
  • ✅ Pending approvals endpoint
  • ✅ Approve/reject endpoint
  • ✅ Get colleges endpoint
  • ✅ Approval status checks in login

5. Routes

File: backend/src/routes/auth.ts

  • ✅ LinkedIn OAuth routes (/linkedin, /linkedin/callback)
  • ✅ Admin approval routes (/approvals/pending, /approvals/:id)
  • ✅ College search route (/colleges)

6. Server Configuration

File: backend/src/index.ts

  • ✅ Session middleware integration
  • ✅ Passport initialization

7. New Dependencies Installed

  • ✅ passport
  • ✅ passport-linkedin-oauth2
  • ✅ express-session
  • ✅ TypeScript type definitions

FRONTEND

1. Registration Form

File: frontend/src/app/auth/register/page.tsx

  • ✅ Username field (optional)
  • ✅ PRN field (optional)
  • ✅ Batch & Year dropdowns
  • ✅ Department field
  • ✅ College searchable dropdown
  • ✅ LinkedIn OAuth button
  • ✅ Real-time college search

2. Auth Context

File: frontend/src/contexts/AuthContext.tsx

  • ✅ Updated register function to accept userData object
  • ✅ Flexible registration data structure

3. Admin Dashboard

File: frontend/src/app/admin/approvals/page.tsx

  • ✅ View pending approval requests
  • ✅ Display all user details (PRN, college, batch, etc.)
  • ✅ Approve/Reject buttons
  • ✅ LinkedIn badge indicator
  • ✅ Protected route (admin only)

4. Pending Approval Page

File: frontend/src/app/auth/pending-approval/page.tsx

  • ✅ User-friendly pending status page
  • ✅ Next steps information
  • ✅ Token storage from LinkedIn callback

DOCUMENTATION

1. Authentication Guide

File: AUTHENTICATION_GUIDE.md

  • ✅ Complete setup instructions
  • ✅ API endpoints documentation
  • ✅ User flows explained
  • ✅ Database schema changes
  • ✅ Security features
  • ✅ Troubleshooting guide

2. Environment Variables

File: backend/env.example

  • ✅ LinkedIn OAuth credentials
  • ✅ Session secret
  • ✅ Frontend URL

🎯 Key Features Implemented

  1. PRN-based registration with validation
  2. LinkedIn OAuth with auto college detection
  3. Admin approval workflow for students
  4. Username system for profile sharing
  5. College searchable database
  6. Batch, department, year tracking
  7. Approval status checks on login
  8. Protected admin routes
  9. Real-time pending requests dashboard

🚀 Next Steps to Run

1. Set up LinkedIn OAuth

  • Go to linkedin.com/developers
  • Create a new app
  • Get Client ID and Client Secret
  • Update backend/.env:
    LINKEDIN_CLIENT_ID=your-client-id
    LINKEDIN_CLIENT_SECRET=your-client-secret
    LINKEDIN_CALLBACK_URL=http://localhost:5000/api/auth/linkedin/callback
    SESSION_SECRET=your-session-secret
    FRONTEND_URL=http://localhost:3000

2. Ensure PostgreSQL is Running

  • The application will auto-create tables on startup

3. Start Backend

cd backend
npm run dev

4. Start Frontend

cd frontend
npm run dev

5. Test the Flows!

Standard Registration Flow:

  1. Go to http://localhost:3000/auth/register
  2. Fill in all fields including PRN and college
  3. Submit → Should see pending approval message
  4. Create admin user (see below)
  5. Go to http://localhost:3000/admin/approvals
  6. Approve the request
  7. Login as the new user

LinkedIn OAuth Flow:

  1. Go to http://localhost:3000/auth/register
  2. Click "Continue with LinkedIn"
  3. Authorize LinkedIn
  4. Should redirect to pending approval or home

Create Admin User:

UPDATE users 
SET role = 'admin', approval_status = 'approved', verified = true 
WHERE email = 'your-email@example.com';

📝 API Endpoints Summary

Public

  • POST /api/auth/register - Register with optional college info
  • POST /api/auth/login - Login
  • GET /api/auth/linkedin - Start LinkedIn OAuth
  • GET /api/auth/colleges - Search colleges

Protected (User)

  • GET /api/auth/profile - Get profile
  • PUT /api/auth/profile - Update profile

Admin Only

  • GET /api/auth/approvals/pending - Get pending requests
  • PUT /api/auth/approvals/:id - Approve/reject
  • GET /api/auth/users - List users
  • PUT /api/auth/users/:id - Update user

Super Admin Only

  • DELETE /api/auth/users/:id - Delete user

🔒 Security Features

  • Password hashing with bcrypt (12 rounds)
  • JWT authentication (7-day expiry)
  • Session management for OAuth
  • Rate limiting (100 req/15min)
  • Input validation
  • SQL injection protection
  • CORS protection

📊 Database Schema

Colleges Table

CREATE TABLE colleges (
  id UUID PRIMARY KEY,
  name VARCHAR(255) UNIQUE NOT NULL,
  domain VARCHAR(255),
  city VARCHAR(100),
  state VARCHAR(100),
  created_at TIMESTAMP
);

Users Table (New Fields)

-- New fields added:
username VARCHAR(50) UNIQUE
prn VARCHAR(50) UNIQUE
batch VARCHAR(20)
department VARCHAR(100)
college_id UUID REFERENCES colleges(id)
year_of_study INTEGER
bio TEXT
avatar_url TEXT
verified BOOLEAN DEFAULT false
linkedin_id VARCHAR(255) UNIQUE
approval_status VARCHAR(20) -- 'pending', 'approved', 'rejected'
password_hash VARCHAR(255) -- Now optional for OAuth users

🎉 All Features Successfully Implemented!

The college-based authentication system is now fully functional with PRN verification, LinkedIn OAuth, and admin approval workflow.