Authentication
TrackGeek uses Better Auth for modern, secure authentication with support for email/password, social providers, and magic links.Authentication Methods
TrackGeek supports multiple authentication methods:- Email & Password - Traditional authentication
- Social Providers - Google, GitHub, Discord
- Magic Links - Passwordless email authentication
- Bearer Tokens - For API clients and mobile apps
Configuration
Authentication is configured insrc/modules/auth/config/auth.config.ts and uses the following environment variables:
.env
Email & Password Authentication
Sign Up
Create a new account with email and password:When a user signs up, TrackGeek automatically:
- Generates a unique username from their email
- Creates a user profile with default settings
- Sets session cookies for immediate authentication
Sign In
Authenticate with existing credentials:Sign Out
End the current session:Social Authentication
TrackGeek supports OAuth 2.0 authentication with Google, GitHub, and Discord.Supported Providers
OAuth 2.0 authentication with Google accounts
GitHub
OAuth 2.0 authentication with GitHub accounts
Discord
OAuth 2.0 authentication with Discord accounts
Setting Up Social Providers
Configure Google OAuth
- Go to Google Cloud Console
- Create a new project or select existing
- Enable Google+ API
- Create OAuth 2.0 credentials
- Add authorized redirect URI:
http://localhost:40287/auth/callback/google - Copy Client ID and Client Secret to
.env:
Configure GitHub OAuth
- Go to GitHub Developer Settings
- Click “New OAuth App”
- Set Authorization callback URL:
http://localhost:40287/auth/callback/github - Copy Client ID and generate Client Secret
- Add to
.env:
Configure Discord OAuth
- Go to Discord Developer Portal
- Create a new application
- Go to OAuth2 settings
- Add redirect:
http://localhost:40287/auth/callback/discord - Copy Client ID and Client Secret to
.env:
Social Sign In Flow
To initiate social authentication, redirect users to:After successful authentication, users are redirected back to
WEB_URL with session cookies set.Magic Link Authentication
Magic links provide passwordless authentication via email.Request Magic Link
Magic links are sent using Resend. Configure
RESEND_API_KEY and RESEND_FROM in your .env file.Bearer Token Authentication
For API clients and mobile apps, use bearer tokens instead of cookies.Get Bearer Token
After signing in, request a bearer token:Using Bearer Tokens
Include the token in theAuthorization header:
Session Management
TrackGeek uses custom session management powered by Better Auth.Get Current Session
Session Response
Protected Routes
Protected endpoints require authentication. The API uses NestJS guards to enforce authentication:src/modules/profile/profile.controller.ts
Example: Making Authenticated Requests
Mobile App Authentication
For mobile apps (iOS/Android), TrackGeek supports custom URL schemes for OAuth callbacks:src/modules/auth/config/auth.config.ts
Configure these URL schemes in your mobile app to handle OAuth redirects.
Security Best Practices
Use HTTPS in Production
Always use HTTPS for production deployments to protect credentials and session tokens.
Secure Token Storage
- Browser: Use
httpOnlycookies (handled automatically) - Mobile: Use secure storage (Keychain, KeyStore)
- Never store tokens in localStorage or plain text
Troubleshooting
Common Issues
401 Unauthorized errors
401 Unauthorized errors
CORS errors
CORS errors
- Verify
WEB_URLmatches your frontend origin - Ensure
credentials: 'include'is set in fetch requests - Check that
withCredentials: trueis set for axios
Social login not working
Social login not working
Magic links not sending
Magic links not sending
- Verify
RESEND_API_KEYis valid - Check
RESEND_FROMemail is verified - Review email logs in Resend dashboard
- Ensure email queue is processing (BullMQ + Redis)
Next Steps
API Reference
Explore all authenticated endpoints
User Profiles
Learn about profile customization
Media Tracking
Start tracking anime, games, and more
Social Features
Discover followers and feed events