Authentication Flow
Initiate GitHub OAuth
Users start the authentication process by navigating to the
/github/login endpoint. The server generates a unique state parameter and redirects to GitHub’s authorization page.internal/handlers/auth_handler.go
The state parameter includes a
mobile- prefix for mobile clients, enabling platform-specific redirect handling.GitHub Authorization
GitHub prompts the user to authorize PrivyCode with the following scopes:Required Scopes:
internal/github/oauth.go
read:user- Access basic user profile informationrepo- Access public and private repository contents
Token Exchange
After authorization, GitHub redirects back with an authorization code. The server exchanges this code for an access token:
internal/github/oauth.go
User Creation or Update
The server fetches user details from GitHub and creates or updates the user record:
internal/handlers/auth_handler.go
If GitHub doesn’t provide an email, the system generates a fallback email:
[email protected]User Model
Authenticated users are stored with the following schema:internal/models/user.go
Email- User’s email address (unique)GitHubUsername- GitHub username (unique)GitHubToken- GitHub OAuth access token for API callsViewerLinks- Associated viewer links created by the user
Token-Based Authentication
All protected API endpoints require a Bearer token in the Authorization header.Authentication Middleware
The auth middleware validates tokens on every request:internal/middleware/auth.go
Making Authenticated Requests
Protected Endpoints
The following endpoints require authentication:| Endpoint | Method | Description |
|---|---|---|
/dashboard | GET | User dashboard with all viewer links |
/me | GET | Current user information |
/generate-viewer-link | POST | Create a new viewer link |
/update-link/:id | PUT | Update viewer link settings |
/delete-link/:id | DELETE | Delete a viewer link |
Viewer access endpoints (
/view/*, /view-files/*, /view-folder/*) do not require authentication - they use viewer link tokens instead.Environment Variables
Configure these environment variables for GitHub OAuth:.env
Security Considerations
- Token Storage - GitHub tokens are stored encrypted in the database
- Token Validation - Every request validates the token against the database
- HTTPS Only - All authentication flows must use HTTPS in production
- State Parameter - Prevents CSRF attacks during OAuth flow
- Token Refresh - Tokens are updated on each successful OAuth flow
Next Steps
Viewer Links
Learn how to generate and manage viewer links
Security
Explore security features and best practices