Overview
Bounty uses Better Auth for authentication, providing a modern, type-safe authentication system with support for multiple providers and advanced features.The authentication system is implemented in
packages/auth/src/server.ts:1 and configured in packages/auth/src/config.ts:1.Authentication Methods
Bounty supports multiple authentication methods to accommodate different user preferences and use cases.Email & Password
Traditional email/password authentication with email verification.- Email verification on sign-up
- Password reset via email
- Auto sign-in after email verification
Email OTP (One-Time Password)
Passwordless authentication using one-time codes sent via email.email-verification- Verify email addresssign-in- Passwordless sign-inforget-password- Password reset
OAuth Providers
Bounty integrates with multiple OAuth providers for social authentication.GitHub
Primary OAuth provider with handle auto-syncScopes:
read:user, public_repo, read:orgGoogle OAuth authenticationScopes:
openid, email, profileDiscord
Discord authentication with guild accessScopes:
identify, email, guildsLinear
Linear workspace integrationScopes:
read, writeGitHub Integration
GitHub authentication includes automatic handle synchronization. When a user authenticates with GitHub:- Their GitHub username is extracted and lowercased
- If the user doesn’t have a handle, it’s set to their GitHub username
- The handle is used for personal team creation and profile URLs
packages/auth/src/server.ts:104 for implementation details.
Account Linking
Users can link multiple OAuth providers to a single account:Device Authorization
For CLI tools and mobile apps, Bounty supports OAuth 2.0 Device Authorization Flow.- Expires in: 30 minutes
- Poll interval: 5 seconds
- Client IDs validated via
DEVICE_AUTH_ALLOWED_CLIENT_IDSenvironment variable
Session Management
Session Configuration
Sessions are configured for security and performance. Seepackages/auth/src/config.ts:12 for details.
- 7-day session expiration
- Database refresh every 5 minutes for up-to-date user data
- Cookie caching to reduce database queries
Multi-Session Support
Users can maintain multiple active sessions across devices:Session Context
The API context includes session information from cookies or Bearer tokens. Seepackages/api/src/context.ts:38 for implementation.
Active Organization
Sessions track the user’s active organization for team-scoped operations:packages/auth/src/server.ts:384 for implementation.
Authorization Patterns
Role-Based Access Control (RBAC)
Bounty implements RBAC with three user roles:- User
- Early Access
- Admin
Default role for all users.Permissions:
- Create and manage own bounties
- Apply to bounties
- Comment and vote
- Join organizations
Organization-Based Access Control
Organization members have role-based permissions within teams:- Member
- Owner
Standard team member.Permissions:
- View organization bounties
- Collaborate on team projects
- Access organization resources
Impersonation
Admins can impersonate users for support purposes:Personal Teams
Every user automatically receives a personal team (organization) upon registration.Auto-Creation
Personal teams are created in theuser.create.after database hook. See packages/auth/src/server.ts:181 for implementation.
Team Naming:
- If user has a GitHub handle:
{handle}'s team - Otherwise:
{name}'s teamorMy's team
- Format:
{handle}-{random8chars}or{userId} - Random suffix prevents collisions
- Reserved slugs are avoided
Self-Healing
For users created before the personal teams feature, teams are auto-created on first login:Email Notifications
Authentication events trigger email notifications using React Email templates.Email Types
Email Verification
Email Verification
Sent when a user signs up with email/password.Template:
OTPVerificationContains: Verification link or OTP codeAuto sign-in: Enabled after verificationPassword Reset
Password Reset
Sent when a user requests a password reset.Template:
ForgotPasswordContains: Password reset linkExpires: Link expires after configured timeOrganization Invitation
Organization Invitation
Sent when a user is invited to join a team.Template:
OrgInvitationContains: Invitation link, inviter name, organization name, roleEmail Configuration
packages/auth/src/config.ts:42 for email sending implementation.
Security Features
Trusted Origins
CORS is configured with trusted origins for security:Bearer Token Authentication
API endpoints support Bearer token authentication for programmatic access:Cookie Security
Session cookies are configured with security best practices:httpOnly: Prevents JavaScript accesssecure: HTTPS only in productionsameSite: CSRF protectionpath: Limited to auth routes
Best Practices
Use the appropriate procedure type
Choose
publicProcedure, protectedProcedure, orgProcedure, etc. based on your authorization requirements.Handle missing sessions gracefully
The session may be
null in public procedures. Always check before accessing user data.Use organization context
When working with teams, use
orgProcedure to automatically validate membership and provide context.Next Steps
API Overview
Learn about tRPC architecture and available routers
Better Auth Docs
Explore Better Auth documentation for advanced features