Overview
BillBuddy implements a secure authentication system using JSON Web Tokens (JWT) for session management and bcrypt for password hashing. All authenticated routes require a valid JWT token passed in the request headers.Authentication Flow
User Registration
Users create an account by providing their name, email, and password. Passwords are automatically hashed using bcrypt before being stored in the database.
Token Generation
Upon successful registration or login, the system generates a JWT token that expires in 30 days.
Authenticated Requests
The token must be included in the
Authorization header as a Bearer token for all protected endpoints.User Model
The User model is defined inbackend/models/User.js:5-35 and includes the following fields:
Security Feature: The password field has
select: false, which means it’s automatically excluded from database queries unless explicitly requested. This prevents accidental password exposure.Password Hashing with bcrypt
BillBuddy uses bcrypt to hash passwords with a salt round of 10, providing strong protection against rainbow table and brute-force attacks.The
pre('save') middleware automatically hashes passwords before saving to the database. It only rehashes if the password field has been modified.JWT Token Generation
JWT tokens are signed with a secret key and expire after 30 days. The token payload contains the user’s ID.JWT Token Method
API Endpoints
Register a New User
POST /api/auth/register
Create a new user account and receive an authentication token.
Login
POST /api/auth/login
Authenticate with email and password to receive a token.
backend/routes/auth.js:44-71):
Get Current User
GET /api/auth/me
Retrieve the currently authenticated user’s profile.
Authentication Middleware
Theprotect middleware (defined in backend/middleware/auth.js:4-31) verifies JWT tokens on protected routes:
Using Authentication in Your App
- JavaScript
- cURL
Security Best Practices
Environment Variables
Environment Variables
Always store your
JWT_SECRET in environment variables, never in code. Use a long, random string (at least 32 characters).Token Storage
Token Storage
Store tokens securely in your frontend:
- Use
httpOnlycookies for web apps (prevents XSS attacks) - Use secure storage APIs for mobile apps
- Avoid storing in localStorage if possible (vulnerable to XSS)
Password Requirements
Password Requirements
The minimum password length is 6 characters. Consider adding additional validation for:
- Uppercase and lowercase letters
- Numbers and special characters
- Checking against common password lists
Token Expiration
Token Expiration
Tokens expire after 30 days. Implement token refresh logic in production or reduce the expiration time for enhanced security.
Error Handling
| Status Code | Message | Cause |
|---|---|---|
| 400 | User already exists | Email is already registered |
| 400 | Invalid credentials | Email or password is incorrect |
| 401 | Not authorized, no token | No Bearer token provided |
| 401 | Not authorized | Token is invalid or expired |
| 500 | Server error | Internal server error |