mirror of
https://github.com/Dvorinka/Trackeep.git
synced 2026-06-03 20:12:58 +00:00
3.7 KiB
3.7 KiB
GitHub OAuth Integration Setup
This document explains how to set up GitHub OAuth integration for Trackeep.
1. GitHub OAuth App Setup
- Go to GitHub Settings → Developer settings → OAuth Apps
- Click "New OAuth App"
- Fill in the details:
- Application name: Trackeep
- Homepage URL:
http://localhost:5173 - Authorization callback URL:
http://localhost:8080/api/v1/auth/github/callback
- Click "Register application"
- Note down the Client ID and generate a Client Secret
2. Environment Variables
Add these to your .env file:
# GitHub OAuth Configuration
GITHUB_CLIENT_ID=your_github_client_id_here
GITHUB_CLIENT_SECRET=your_github_client_secret_here
GITHUB_REDIRECT_URL=http://localhost:8080/api/v1/auth/github/callback
# Frontend URL for callback redirect
FRONTEND_URL=http://localhost:5173
3. Database Migration
The User model has been updated with GitHub OAuth fields:
// GitHub OAuth fields
GitHubID int `json:"github_id" gorm:"uniqueIndex"`
AvatarURL string `json:"avatar_url"`
Provider string `json:"provider" gorm:"default:email"` // email, github
Run the application to auto-migrate the database schema.
4. How It Works
OAuth Flow:
- User clicks "Connect GitHub" → Redirects to
/api/v1/auth/github - GitHub Authorization → User authorizes the application on GitHub
- GitHub Callback → GitHub redirects to
/api/v1/auth/github/callbackwith authorization code - Token Exchange → Backend exchanges code for access token
- User Data Fetch → Backend fetches user profile and repositories from GitHub API
- User Creation/Update → Creates new user or links GitHub account to existing user
- JWT Generation → Generates JWT token for the user
- Frontend Redirect → Redirects to
/auth/callback?token=jwt_token - Token Storage → Frontend stores token and redirects to dashboard
API Endpoints:
GET /api/v1/auth/github- Initiates GitHub OAuth flowGET /api/v1/auth/github/callback- Handles GitHub OAuth callbackGET /api/v1/github/repos- Fetches user's GitHub repositories (protected)
5. Features
Authentication:
- Users can sign up/login with GitHub
- Existing accounts can be linked to GitHub
- Secure JWT token generation
GitHub Integration:
- Fetch user's public repositories
- Display repository statistics (stars, forks, watchers)
- Language distribution analysis
- Recent activity tracking
- Real-time data synchronization
Security:
- CSRF protection with state parameter
- Secure token storage
- OAuth 2.0 standard implementation
- Rate limiting awareness
6. Testing
- Start the backend server:
go run main.go - Start the frontend:
npm run dev - Navigate to
http://localhost:5173/app/github - Click "Connect GitHub"
- Authorize the application on GitHub
- You should be redirected back to the app with GitHub data
7. Production Considerations
For production deployment:
- Update the GitHub OAuth app with production URLs
- Use HTTPS for all callbacks
- Store secrets securely (environment variables, secret management)
- Implement proper error handling and logging
- Consider GitHub API rate limits
- Add webhook support for real-time updates
8. Troubleshooting
Common Issues:
- "Redirect URI mismatch" - Check that the callback URL in GitHub matches exactly
- "Invalid state" - Clear browser cookies and try again
- "Failed to get user info" - Check GitHub API permissions and token validity
- Database errors - Ensure database is running and migrations are applied
Debug Mode:
Enable debug logging by setting:
GIN_MODE=debug
This will provide detailed logs for troubleshooting OAuth flow issues.