A robust, secure authentication system built with the MEAN stack (MongoDB, Express.js, Angular, and Node.js). This project implements industry-standard security practices for user authentication and authorization.
- Secure Authentication Flow: Complete user registration, email verification, login, password reset
- JWT-based Authorization: Access and refresh token implementation
- Advanced Security Measures:
- HTTP-only cookies
- CSRF protection
- Argon2id password hashing
- Token rotation
- Rate limiting
- MongoDB schema validation
- Responsive UI: Mobile-friendly design using modern Angular techniques
- TypeScript: End-to-end type safety for both frontend and backend
- MongoDB (v7.0.16): NoSQL database with schema validation
- Express.js (v4.21.2): Backend web framework with TypeScript
- Node.js (v22.11.0): JavaScript runtime environment
- Angular (v19.2.1): Frontend framework using standalone components and signals
- Angular Material (v19.2.2): UI component library
- NgRx (v19.0.1): State management with Redux pattern
- TypeScript: Static typing for improved code quality and developer experience
- argon2: Modern password hashing algorithm (superior to bcrypt)
- jsonwebtoken: JWT implementation for access and refresh tokens
- helmet: Security headers for Express
- zod: Runtime schema validation
- express-rate-limit: API rate limiting to prevent abuse
project/
├── backend/ # Express.js server
│ ├── src/
│ │ ├── config/ # Configuration files
│ │ ├── controllers/ # Route controllers
│ │ ├── middleware/ # Express middleware
│ │ ├── models/ # MongoDB schemas
│ │ ├── routes/ # API routes
│ │ ├── services/ # Business logic
│ │ ├── utils/ # Utility functions
│ │ ├── app.ts # Express app setup
│ │ └── server.ts # Server entry point
│ ├── package.json
│ └── tsconfig.json
│
└── frontend/ # Angular client
├── src/
│ ├── app/
│ │ ├── core/ # Auth guards, services, etc.
│ │ ├── domains/ # Feature modules
│ │ ├── layout/ # Layout components
│ │ └── shared/ # Reusable components
│ ├── environments/ # Environment config
│ └── assets/ # Static assets
├── angular.json
└── package.json
- Node.js v22 or higher
- MongoDB v7.0 or higher
- npm or yarn
-
Clone the repository
git clone https://github.com/yourusername/mean-auth-system.git cd mean-auth-system -
Install backend dependencies
cd backend npm install -
Create a
.envfile in the backend directory with the following variables:NODE_ENV=development PORT=5000 MONGO_URI=mongodb://localhost:27017/auth-app JWT_SECRET=your-secret-key JWT_EXPIRES_IN=15m REFRESH_TOKEN_SECRET=your-refresh-secret-key REFRESH_TOKEN_EXPIRES_IN=7d COOKIE_SECRET=your-cookie-secret CLIENT_URL=http://localhost:4200 # Email settings EMAIL_HOST=smtp.example.com EMAIL_PORT=587 EMAIL_SECURE=false EMAIL_USER=user@example.com EMAIL_PASSWORD=password EMAIL_FROM_NAME=MEAN Auth App EMAIL_FROM_ADDRESS=noreply@example.com -
Start the backend server
npm run dev
-
Install frontend dependencies
cd ../frontend npm install -
Update environment files if needed The
environment.tsfile contains configuration settings like the API URL. -
Start the Angular development server
ng serve
-
Navigate to
http://localhost:4200in your browser
- Access Token: Short-lived (15 minutes), stored as HTTP-only cookie
- Refresh Token: Longer-lived (7 days), HTTP-only cookie with restricted path
- CSRF Protection: CSRF token implementation to prevent cross-site request forgery
- Token Rotation: Refresh tokens are rotated with each use to prevent replay attacks
- Argon2id: Modern password hashing algorithm with enhanced security parameters
- Strong Password Policy: Enforced on both frontend and backend
- Account Lockout: Progressive delays and lockout after multiple failed attempts
- Rate Limiting: Prevents brute force attacks on login and sensitive endpoints
- Input Validation: Comprehensive validation using Zod schemas
- Schema Validation: MongoDB schema validation ensures data integrity
POST /api/auth/register- User registrationPOST /api/auth/login- User loginPOST /api/auth/refresh- Refresh access tokenPOST /api/auth/logout- User logoutGET /api/auth/verify-email/:token- Email verificationPOST /api/auth/forgot-password- Request password resetPOST /api/auth/reset-password- Process password resetGET /api/auth/me- Get current user profile
Contributions are welcome! Please feel free to submit a Pull Request.
- Fork the repository
- Create a feature branch (
git checkout -b feature/amazing-feature) - Commit your changes (
git commit -m 'Add some amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - Open a Pull Request
This project is licensed under the MIT License - see the LICENSE file for details.
While this project implements numerous security best practices, security is an ongoing process:
- Keep all dependencies updated to protect against vulnerabilities
- Use environment variables for all sensitive information
- In production, ensure all cookies use the Secure flag
- Implement proper logging and monitoring for suspicious activities
- Consider additional security measures such as Two-Factor Authentication for enhanced security