Documentation Index
Fetch the complete documentation index at: https://mintlify.com/Greens-Organization/pz-packs/llms.txt
Use this file to discover all available pages before exploring further.
Overview
PZ Packs uses Better Auth with Discord OAuth2 for secure, passwordless authentication. Instead of managing separate credentials, you authenticate using your existing Discord account, which provides a seamless and secure login experience.PZ Packs only requests basic Discord profile information (username, avatar, and user ID). We do not access your Discord messages, servers, or any sensitive data.
Authentication Architecture
Technology Stack
The authentication system is built on Better Auth, a modern authentication framework for TypeScript applications:Client-Side Integration
The web application uses the Better Auth React client for seamless authentication hooks:OAuth2 Flow
Initiate Sign In
When you click “Sign In” on the PZ Packs website, the client initiates an OAuth2 flow with Discord:This redirects you to Discord’s authorization page at:
Discord Authorization
Discord presents an authorization screen showing:
- PZ Packs application requesting access
- Requested permissions: Read your username and avatar
- Options to Authorize or Cancel
Token Exchange
Discord redirects back to PZ Packs with an authorization code:The PZ Packs backend exchanges this code for access tokens:
Session Management
Session Lifecycle
Sessions in PZ Packs are designed for security and performance:- Duration: Sessions last for 30 days (2,592,000 seconds)
- Cookie Cache: 5-minute cache in Redis for fast authentication checks
- Storage: Primary session data in PostgreSQL, cached copy in Redis
- HTTP-Only Cookies: Session tokens cannot be accessed via JavaScript (XSS protection)
- Secure Flag: Cookies only transmitted over HTTPS in production
- SameSite:
Laxsetting prevents CSRF attacks while allowing OAuth redirects
Checking Authentication Status
In your React components, use theuseSession hook:
Signing Out
To end a session, use thesignOut method:
- Delete the session from the database
- Clear the session cache in Redis
- Remove the session cookie from the browser
- Redirect you to the homepage
API Authentication
Authenticated Requests
When making API requests, the session cookie is automatically included:Backend Authentication Middleware
The API uses Better Auth macros to protect endpoints:Permission-Based Authorization
PZ Packs implements role-based access control (RBAC):Environment Configuration
Required Environment Variables
To set up authentication in your own deployment:Creating a Discord OAuth Application
To create your own Discord OAuth app:Visit Discord Developer Portal
Navigate to Discord Developer Portal and click “New Application”.
Configure OAuth2 Settings
In your application settings:
- Go to OAuth2 → General
- Add your redirect URIs:
- Development:
http://localhost:3000/auth/callback/discord - Production:
https://yourdomain.com/auth/callback/discord
- Development:
- Copy your Client ID and Client Secret
Set Required Scopes
PZ Packs only requires the
identify scope to read basic profile information (username and avatar).CORS Configuration
The API must allow requests from your frontend origin:Security Considerations
Best Practices
HTTPS Only in Production
Session cookies have the
Secure flag in production, requiring HTTPS. Never deploy without SSL/TLS certificates.HTTP-Only Cookies
Session tokens are stored in HTTP-only cookies, making them inaccessible to JavaScript and preventing XSS attacks.
SameSite Protection
SameSite=Lax prevents CSRF attacks by restricting cookie transmission to same-site requests and safe cross-site navigation.Token Encryption
OAuth tokens (access and refresh tokens) are encrypted before storage in the database using Better Auth’s built-in encryption.
Session Security Features
Rate Limiting
API endpoints are protected by rate limiting to prevent abuse:Troubleshooting
401 Unauthorized on API Requests
401 Unauthorized on API Requests
Discord OAuth Redirect Loop
Discord OAuth Redirect Loop
Possible causes:
- Callback URL mismatch in Discord app settings
- Session not being created after OAuth callback
- Browser blocking cookies
- Verify Discord app redirect URI matches exactly:
https://yourdomain.com/auth/callback/discord - Check browser console for cookie errors
- Ensure
ORIGIN_ALLOWEDincludes your frontend URL - Clear browser cookies and try again
Session Lost After Page Refresh
Session Lost After Page Refresh
Possible causes:
- Session cookie not persisted (check cookie settings)
- Redis cache cleared but PostgreSQL still has session
- Browser privacy settings blocking persistent cookies
- Check cookie
Max-Ageis set (30 days = 2,592,000 seconds) - Verify
HttpOnlyandSecureflags are appropriate for environment - Test in incognito mode to rule out browser extensions
Cannot Access Protected Routes
Cannot Access Protected Routes
Possible causes:
- Insufficient permissions for your role
- Permission middleware not properly configured
- Session exists but user role not loaded
Advanced Topics
Token Refresh
Better Auth automatically handles token refresh for Discord OAuth:Multiple Sessions
Users can have multiple active sessions (e.g., desktop and mobile):Custom User Fields
The user schema includes a role field with additional metadata support:Related Resources
Better Auth Documentation
Learn more about the authentication framework powering PZ Packs
Discord OAuth2 Documentation
Official Discord OAuth2 implementation guide
Quick Start Guide
Get started with PZ Packs in under 5 minutes
API Reference
Explore authenticated API endpoints