Overview
Showdown Trivia implements a secure authentication system using bcrypt password hashing and cookie-based session management. Users must be authenticated to create or join game rooms.User Registration
Sign Up Flow
New users register through the signup form with the following fields:- Username - Unique identifier for the player
- Email - Must be unique across all users
- Password - Hashed using bcrypt before storage
Endpoints:
GET /signup- Display registration formPOST /signup- Process registration
internal/web/handlers/signup.go:15-83
Validation Rules
The signup form enforces validation on the client and server:Username requirements
Username requirements
- Must be unique
- Cannot be empty
- Used as display name in games
Email requirements
Email requirements
- Must be valid email format
- Must be unique across all users
- Used for account identification
Password requirements
Password requirements
- Hashed using bcrypt with default cost
- Never stored in plain text
- Verified during login
internal/web/form/user.go, internal/web/handlers/signup.go:47-51
User Login
Sign In Flow
Existing users authenticate through the signin form:
Endpoints:
GET /signin- Display login formPOST /signin- Process authentication
internal/web/handlers/signin.go:18-82
Authentication Errors
Error responses:- User not found: 401 Unauthorized
- Wrong password: 401 Unauthorized
- Invalid form data: 422 Unprocessable Entity
internal/web/handlers/signin.go:47-70
Session Management
Sessions are managed using the Gorilla Sessions library with encrypted cookies.Session Storage
When a user logs in successfully:user-session and stores:
- authenticated: Boolean flag indicating login status
- username: The user’s display name for game rooms
internal/web/handlers/signin.go:71-78
Session Security
Sessions use encrypted cookies with secure storage via Gorilla Sessions CookieStore.Configuration: Set in application initialization
Protected Routes
Certain routes require authentication and are protected by middleware:/home- User dashboard/create- Create game form/activegames- List of joinable games/wscreate- WebSocket room creation/wsjoin/{id}- WebSocket room joining
internal/web/middleware.go:12-28
How Authentication Works
TherequireAuth middleware checks every protected request:
Route protection:
internal/web/routes.go:34-42
Sign Out
Users can log out to end their session: Endpoint:POST /signout
The signout process:
- Retrieves current session
- Sets
authenticatedflag tofalse - Saves session
- Redirects to signin page
internal/web/handlers/signout.go:10-26
Signing out invalidates the session but doesn’t disconnect active WebSocket connections. Players in games will complete their current game.
User Model
The User entity stores:internal/core/user/domain.go:9-15
Password Security
Hashing
Passwords are hashed using bcrypt with default cost factor:- Algorithm: bcrypt
- Cost: Default (recommended security level)
- Storage: Hash stored as byte array
- Verification: Uses constant-time comparison
internal/web/handlers/hashpassword.go
Environment Configuration
Authentication requires environment variables:ENV- Environment mode (dev/prod)DB_URL- Database connection for user storageLOG_LEVEL- Logging verbosityPORT- Server port
internal/config/config.go:31-71
Integration with Games
Once authenticated, the username from the session is used to:- Identify players in WebSocket rooms
- Track scores during gameplay
- Display usernames to other players
- Determine room ownership for game controls
internal/web/handlers/game.go:26-29, internal/web/handlers/game.go:130-133