Overview
The Go React Scaffold uses JWT (JSON Web Tokens) for authentication. Tokens are signed using HMAC-SHA256 (HS256) algorithm and stored in HTTP-only cookies.Token Generation
JWT tokens are generated during login and contain user claims with an expiration time.Generation Function
backend/auth/auth.go:29
This function:
- Generates an access token with 24-hour expiration
- Sets the token in an HTTP-only cookie named
Bearer - Sets a user ID cookie for client-side access
- Returns the token string
Token Structure
Claims
JWT tokens contain the following claims:The unique user identifier (UUID v4 format)
User’s name (currently not populated in login flow)
Expiration timestamp in Unix epoch format
Claims Structure (Go)
backend/auth/auth.go:22-26
Token Expiration
Access Token
- Duration: 24 hours
- Set at: Login time
- Calculation:
time.Now().Add(24 * time.Hour)
backend/auth/auth.go:43
Token Signing
Algorithm
- Method: HMAC-SHA256 (HS256)
- Secret: Retrieved from
SESSION_KEYenvironment variable
Secret Key
backend/auth/auth.go:18-20
Token Generation Process
Step-by-Step
- Create Claims
- Create Token with Algorithm
- Sign Token
- Set Cookies
backend/auth/auth.go:49-68
Cookie Configuration
Bearer Cookie (Token Storage)
Cookie name for JWT token
Signed JWT token string
24 hours from generation
Cookie path (root)
HTTP-only flag to prevent XSS access
User Cookie (Client Access)
Cookie name for user ID
User ID (UUID)
24 hours from generation
Cookie path (root)
Not HTTP-only, accessible to JavaScript
backend/auth/auth.go:71-91
Token Validation
JWT Error Handler
backend/auth/auth.go:94-97
This function is executed when:
- Token is expired
- Token signature is invalid
- Token is missing
- Token claims are malformed
Usage in Protected Routes
To protect routes with JWT authentication in Echo framework:Security Considerations
HTTP-Only Cookies
- Tokens stored in HTTP-only cookies are not accessible via JavaScript
- Prevents XSS attacks from stealing tokens
- Automatically sent with requests to the same domain
Token Expiration
- 24-hour expiration limits exposure window
- Expired tokens must re-authenticate
- No refresh token mechanism (yet)
Signing Secret
- Use strong, random
SESSION_KEY(minimum 32 characters) - Rotate secrets periodically in production
- Never commit secrets to version control
Algorithm Choice
- HS256 (HMAC-SHA256) provides:
- Symmetric signing (same key for sign/verify)
- Fast performance
- Sufficient security for internal APIs
Token Lifecycle
- Generation: User logs in successfully
- Storage: Token stored in HTTP-only
Bearercookie - Usage: Automatically sent with each request
- Validation: Echo JWT middleware validates on protected routes
- Expiration: After 24 hours, token is no longer valid
- Re-authentication: User must log in again
Example Token Payload
Decoded JWT payload (claims):Source Code References
- Token generation:
backend/auth/auth.go:29-46 - Claims structure:
backend/auth/auth.go:22-26 - Cookie setting:
backend/auth/auth.go:71-91 - Error handler:
backend/auth/auth.go:94-97 - Secret retrieval:
backend/auth/auth.go:18-20
Environment Configuration
Required environment variable in.env: