Overview
The authentication system uses JWT (JSON Web Tokens) with bcrypt password hashing. Tokens are stored in HTTP-only cookies for security.Architecture
Authentication is handled by theauth package with the following components:
auth.go- JWT token generation and cookie managementcontroller.go- Business logic for login and registrationroutes.go- HTTP handlers for auth endpointsconfigs.go- Session store configuration
JWT Token Generation
Claims Structure
The JWT token contains custom claims defined inauth/auth.go:22-26:
auth/auth.go
User’s display name
Unique user identifier (UUID)
Standard JWT claims including expiration time
Token Generation Flow
TheGenerateTokensAndSetCookies function in auth/auth.go:29-39 orchestrates token creation:
auth/auth.go
Access Token Creation
Tokens are valid for 24 hours (auth/auth.go:41-46):
auth/auth.go
Despite the comment saying “1h”, the actual expiration is set to 24 hours.
Core Token Generation
The main JWT signing logic inauth/auth.go:49-68:
auth/auth.go
- Algorithm: HS256 (HMAC with SHA-256)
- Secret: Retrieved from
SESSION_KEYenvironment variable - Claims: User ID and expiration timestamp
Cookie Management
Bearer Token Cookie
The JWT token is stored in an HTTP-only cookie (auth/auth.go:71-81):
auth/auth.go
Cookie name for the JWT token
Prevents JavaScript access to the cookie, mitigating XSS attacks
Cookie available across the entire domain
User Cookie
A separate cookie stores the user ID for client-side access (auth/auth.go:84-91):
auth/auth.go
This cookie is NOT HttpOnly, making it accessible to JavaScript for UI personalization.
User Registration
Registration Endpoint
POST/register - Creates a new user account
Handler in auth/routes.go:29-39:
auth/routes.go
User’s email address
Plain text password (will be hashed)
Password Hashing
Passwords are hashed with bcrypt using cost factor 14 (auth/controller.go:30-48):
auth/controller.go
- Bcrypt cost 14 - High security with ~180ms hashing time
- UUID v4 - Cryptographically random user IDs
- Pending status - New users require activation
New users are created with
status: "pending". They cannot log in until their status is changed to "active".User Login
Login Endpoint
POST/login - Authenticate user and receive token
Handler in auth/routes.go:13-27:
auth/routes.go
User’s email address
Plain text password
Login Validation
TheprocessUserLogin function validates credentials (auth/controller.go:13-28):
auth/controller.go
- Fetch user by email from database
- Check if user status is
"active" - Compare password hash using bcrypt
- Return user ID on success
Session Store
The session store uses Gorilla Sessions with cookie-based storage (auth/configs.go:9):
auth/configs.go
The session store is initialized but not actively used in the current authentication flow. JWT tokens in cookies serve as the primary session mechanism.
Security Best Practices
Current Implementation
✅ HTTP-only cookies - Prevents XSS token theft✅ Bcrypt hashing - Strong password protection
✅ UUID user IDs - Non-sequential identifiers
✅ Status checks - Inactive users cannot log in
Recommended Improvements
- Secure flag on cookies - Require HTTPS in production
- SameSite attribute - Prevent CSRF attacks
- Token refresh mechanism - Short-lived access tokens with refresh tokens
- Rate limiting - Prevent brute force attacks
- Email verification - Validate email addresses before activation
- Password requirements - Enforce minimum complexity
Environment Configuration
Authentication requires theSESSION_KEY environment variable:
auth/auth.go
Secret key for JWT signing. Must be a strong, random string (minimum 32 characters recommended).
Testing Authentication
Register a New User
Login
Bearer cookie and can be used for subsequent authenticated requests.
Next Steps
User Model
Learn about the User struct and database operations