GitHub OAuth (Passport)
The GitHub OAuth component provides a secure and standardized way to integrate GitHub authentication into your Servercn Express applications using the official passport, passport-github2.
Installation Guide
npx servercn-cli add oauthYou will be prompted to select a file upload provider:
The CLI will then automatically configure the component based on your selected provider.
Prerequisites
GitHub OAuth Setup
- Go to your GitHub Settings
- Developer settings -> OAuth Apps -> New OAuth App
- Fill in the required information
- Set Homepage URL to
http://localhost:9000(or your production URL) - Set Authorization callback URL to
http://localhost:9000/api/auth/github/callback(or your production URL) - Register application
- Generate a Client Secret
- Copy the Client ID and Client Secret
Environment Variables
Add the following to your .env file:
Basic Implementation
1. Configure Passport Strategy
2. Create a GitHub OAuth Controller
3. Create a GitHub OAuth Routes
4. Initialize passport in app
Success Response
This response is formated by ApiResponse component.
Common Issues
"redirect_uri_mismatch"
Ensure your redirect URI in .env exactly matches the one configured in GitHub Developer Settings.
"bad_verification_code"
The code passed is incorrect or expired.
- The authorization code has expired (codes expire after 10 minutes)
- The code has already been used