Passport.js TypeScript Lab Writeup
Course Information
- Course: ACIT 2520
- Student: Ashen Perera
- Student ID: A01455518
- Date: August 2025
https://github.com/wolf-569/acit2520-passport
Overview
This lab demonstrates the implementation of a secure web server using Express.js with Passport.js for authentication and session management. The application is built entirely in TypeScript, showcasing modern web development practices with proper type safety.
The primary objectives of this lab are to:
- Understand HTTP session handling and session persistence
- Implement middleware-based authentication flows
- Configure multiple authentication strategies using Passport.js
- Manage user authentication states and role-based access control
- Secure routes with authentication and authorization middleware
Project Architecture
Technology Stack
- Runtime: Node.js
- Framework: Express.js 4.21.2
- Authentication: Passport.js 0.7.0
- Language: TypeScript
- Template Engine: EJS (Embedded JavaScript)
- Session Store: Express-session (in-memory)
- Authentication Strategies:
- Local Strategy (Email/Password)
- GitHub OAuth2 Strategy
Directory Structure
├── app.ts # Main application entry point
├── controllers/
│ └── userController.ts # User business logic and validation
├── middleware/
│ ├── checkAuth.ts # Authentication guard middleware
│ ├── PassportConfig.ts # Passport strategy configuration
│ ├── passportMiddleware.ts # Express-Passport integration
│ └── passportStrategies/
│ ├── localStrategy.ts # Email/password authentication
│ └── githubStrategy.ts # OAuth2 GitHub authentication
├── models/
│ └── userModel.ts # User data model with mock database
├── routes/
│ ├── authRoute.ts # Authentication routes
│ └── indexRoute.ts # Application routes
├── interfaces/
│ └── index.ts # TypeScript interfaces
├── views/
│ ├── login.ejs # Login page
│ ├── dashboard.ejs # User dashboard
│ ├── admindashboard.ejs # Admin dashboard
│ ├── admin-sessions.ejs # Session management view
│ └── layout.ejs # Layout template
└── public/ # Static assets
Key Components
1. Authentication Middleware (middleware/checkAuth.ts)
Three guard middleware functions control access to protected routes:
ensureAuthenticated() // Redirects to login if not authenticated
forwardAuthenticated() // Redirects to dashboard if already authenticated
ensureAdmin() // Ensures user is authenticated AND has admin role
These middleware functions check req.isAuthenticated() and req.user?.admin properties set by Passport.js.
2. User Model (models/userModel.ts)
The user model provides:
- An in-memory mock database with 4 sample users
findOne(email)- Retrieve user by emailfindById(id)- Retrieve user by ID
Sample users include admin and regular users with credentials for testing.
3. Local Authentication Strategy (middleware/passportStrategies/localStrategy.ts)
Implements email/password authentication:
- Accepts email and password fields from login form
- Validates credentials against the user model
- Provides specific error messages for failed attempts (invalid email, wrong password)
- Uses
serializeUser()anddeserializeUser()to manage session state
Key Functions:
serializeUser()- Stores user ID in session (minimal data)deserializeUser()- Retrieves user from database using session ID
4. User Controller (controllers/userController.ts)
Business logic layer that:
- Validates email/password combinations
- Checks user existence by email
- Verifies password correctness
- Retrieves user information by ID
5. Routes
Authentication Routes (routes/authRoute.ts)
GET /auth/login- Display login formPOST /auth/login- Process login with PassportGET /auth/logout- Destroy session and logoutGET /auth/github- Initiate GitHub OAuthGET /auth/github/callback- Handle GitHub OAuth callback
Index Routes (routes/indexRoute.ts)
GET /- Welcome pageGET /dashboard- User dashboard (requires authentication)GET /admin/dashboard- Admin dashboard (requires authentication + admin role)GET /admin/sessions- View all active sessions (admin only)
6. Session Management (app.ts)
Express-session configuration:
- Uses in-memory store (suitable for development/testing)
- 24-hour session expiration
- HttpOnly cookies for security
- Session ID logged for debugging
Authentication Flow
Local Authentication Flow
- User submits login form with email/password
- Passport’s local strategy validates credentials
- On success,
req.logIn()creates session and serializes user ID - User redirected to appropriate dashboard (admin vs regular)
- On subsequent requests,
deserializeUser()reconstructs user object from session
Session Persistence
- Session ID stored in httpOnly cookie
- Session data persists in memory store during server runtime
- Admin users can view all active sessions via
/admin/sessions
Security Features
- Session Security
- HttpOnly cookies prevent XSS attacks
- Secure flag can be enabled for HTTPS
- Session expiration (24 hours)
- Authentication Layers
- Multiple authentication strategies
- Email validation before password check
- Role-based access control (admin flag)
- Route Protection
- Middleware guards prevent unauthorized access
- Redirects to login for unauthenticated requests
- Admin routes require both authentication and admin status
Testing & Credentials
Sample Users (from models/userModel.ts)
1. Admin User
Email: jimmy123@gmail.com
Password: jimmy123!
2. Regular User
Email: johnny123@gmail.com
Password: johnny123!
3. Regular User
Email: jonathan123@gmail.com
Password: jonathan123!
4. Admin User (Test)
Email: test@mail.com
Password: test
Testing Local Authentication
- Navigate to
/auth/login - Enter test credentials from above
- On success, redirected to appropriate dashboard
- Admin users see admin dashboard with session management
Learning Outcomes
This lab demonstrates:
✅ Express.js Middleware Pattern
- Custom middleware for authentication guards
- Express-session for state management
✅ Passport.js Architecture
- Strategy pattern for pluggable authentication methods
- Serialization/deserialization for session handling
- Multiple authentication strategies
✅ TypeScript Best Practices
- Custom interfaces for type safety
- Extending Express types via declaration merging
- Type-safe middleware functions
✅ Security Concepts
- Session-based authentication
- Role-based access control (RBAC)
- Secure cookie handling
- Input validation
✅ MVC Architecture
- Controllers for business logic
- Models for data access
- Routes for request handling
- Middleware for cross-cutting concerns
Potential Enhancements
- Database Integration: Replace in-memory model with actual database (PostgreSQL, MongoDB)
- Password Hashing: Implement bcrypt for secure password storage
- CSRF Protection: Add express-csrf middleware
- Rate Limiting: Implement login attempt throttling
- Email Verification: Add email confirmation for registration
- Refresh Tokens: Implement token-based authentication alongside sessions
- Logging: Add structured logging for authentication events
- Error Handling: Centralized error handling middleware
- Testing: Expand Jest test suite with integration tests
- Production Deployment: Use persistent session store (Redis, MongoDB)
Conclusion
This Passport.js TypeScript lab successfully demonstrates enterprise-grade authentication patterns. The application showcases proper separation of concerns, type safety, and security best practices. The implementation serves as a solid foundation for building secure web applications with Node.js and Express.js.