This document explains how to set up and use the authentication system for Campus Bridge.
The authentication system includes:
- User registration and login
- Session management
- Protected routes
- Database storage for user credentials
Since Node.js is not available on this system, you'll need to manually create the database tables.
Open MySQL Workbench or Terminal and run:
CREATE DATABASE lms;
USE lms;
CREATE TABLE learning_resources (
id INT AUTO_INCREMENT PRIMARY KEY,
title VARCHAR(255) NOT NULL,
file_path VARCHAR(255) NOT NULL
);
CREATE TABLE users (
id INT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(255) NOT NULL,
email VARCHAR(255) UNIQUE NOT NULL,
password VARCHAR(255) NOT NULL,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
-- Insert a test user
INSERT INTO users (name, email, password) VALUES ('Test User', 'test@example.com', 'password123');If Node.js becomes available later, you can use the initialization script:
npm run init-dbnpm startOr for development:
npm run dev- Endpoint:
POST /api/register - Body:
{ "name": "User Name", "email": "user@example.com", "password": "userpassword" } - Response:
{ "success": true, "message": "User registered successfully" }
- Endpoint:
POST /api/login - Body:
{ "email": "user@example.com", "password": "userpassword" } - Response:
{ "success": true, "message": "Login successful", "sessionId": "session_identifier", "user": { "id": 1, "name": "User Name", "email": "user@example.com" } }
- Endpoint:
POST /api/logout - Body:
{ "sessionId": "session_identifier" } - Response:
{ "success": true, "message": "Logged out successfully" }
- Endpoint:
GET /api/auth/check - Headers:
X-Session-Id: session_identifier - Response:
{ "success": true, "message": "User is authenticated", "user": { "id": 1, "name": "User Name", "email": "user@example.com" } }
-
Login Process:
- User submits credentials via the login form
- Credentials are sent to the backend
/api/loginendpoint - If successful, the session ID is stored in
localStorage - User is redirected to the courses page
-
Session Management:
- Session ID is stored in
localStorageassessionId - On page load, protected pages check authentication status
- Session ID is sent in the
X-Session-Idheader for authenticated requests
- Session ID is stored in
-
Protected Routes:
- Pages like
courses.htmlcheck for a valid session on load - Unauthorized users are shown an access denied message
- Pages like
-
Session Storage:
- Sessions are stored in memory using a
Mapdata structure - Each session contains user information and creation timestamp
- Sessions can be destroyed on logout
- Sessions are stored in memory using a
-
Authentication Middleware:
requireAuthmiddleware checks for valid sessions- Used to protect API endpoints that require authentication
- Passwords are stored in plain text (NOT secure for production)
- Sessions are stored in memory (will be lost on server restart)
- No password strength validation
-
Hash passwords using bcrypt:
const bcrypt = require('bcrypt'); // Hash password before storing const hashedPassword = await bcrypt.hash(password, 10); // Compare hashed password during login const isValid = await bcrypt.compare(password, hashedPassword);
-
Use a proper session store like Redis:
const session = require('express-session'); const RedisStore = require('connect-redis')(session);
-
Add CSRF protection
-
Implement rate limiting for login attempts
-
Add input validation and sanitization
-
Use HTTPS in production
-
Implement password strength requirements
The database initialization script creates a test user:
- Email: test@example.com
- Password: password123
- Navigate to
http://localhost:8080/studentlogin.html - Enter the test credentials
- You should be redirected to the courses page
- Try accessing
http://localhost:8080/courses.htmldirectly (should work if logged in) - Try logging out and accessing protected pages (should show access denied)
-
Cannot log in with test credentials:
- Ensure you've run
npm run init-db - Check the database connection in
db.js - Verify the users table was created
- Ensure you've run
-
Access denied to protected pages:
- Make sure you're logged in
- Check browser console for errors
- Verify session ID is being stored in localStorage
-
Database connection errors:
- Check database credentials in
db.js - Ensure MySQL server is running
- Verify the database exists
- Check database credentials in
- Check browser developer tools Network tab to see API requests
- Check server console for error messages
- Use database client to verify data in tables
- Check localStorage for session ID after login
For questions about this implementation, contact the Campus Bridge development team.