GitHub OAuth
Configuring GitHub as an authentication provider using Auth.js v5.
This guide explains how to set up GitHub OAuth for user authentication in StarterKitPro using Auth.js v5.
Optional Feature
Github is not included by default to keep the core lean. But you can add it in less then one minute.
Prerequisites
- Database Setup: Ensure you have configured and migrated your database, as Auth.js requires an adapter (like Prisma) to store user and account information. See the Database Setup documentation.
1. Environment Variables
Add the following variables to your .env.local
file.
AUTH_SECRET
: A random string used to encrypt JWTs and session cookies. Use the commandopenssl rand -base64 32
in your terminal to generate a strong one. Do not commit this file.AUTH_URL
: Set this to your canonical URL (e.g.,http://localhost:3000
for development,https://your-site.com
for production).- Replace the
AUTH_GITHUB_
placeholders with the actual credentials obtained from GitHub in the next steps.
Use the exact environment variable names AUTH_GITHUB_ID
and
AUTH_GITHUB_SECRET
. Auth.js automatically picks up variables following the
AUTH_PROVIDER-ID_ID
and AUTH_PROVIDER-ID_SECRET
pattern, eliminating the
need to explicitly pass clientId
and clientSecret
in the provider
configuration within lib/auth/auth-config.ts
(or similar).
2. GitHub OAuth App Setup
You need to register a new OAuth application on GitHub.
- Navigate to Settings: Go to your GitHub profile settings.
- Developer Settings: In the left sidebar, click Developer settings.
- OAuth Apps: In the left sidebar, click OAuth Apps.
- New OAuth App: Click the [New OAuth App] button.
- Fill in Application Details:
- Application name: Enter a name for your application (e.g., "StarterKitPro Dev" or "StarterKitPro Prod").
- Homepage URL: Enter the URL of your application.
- Development:
http://localhost:3000
- Production:
https://your-domain.com
- Development:
- Application description (Optional): Briefly describe your application.
- Authorization callback URL: This is crucial. It's where GitHub sends the user back after they authorize your application. It must match the pattern
/api/auth/callback/[provider]
.- Development:
http://localhost:3000/api/auth/callback/github
- Production:
https://your-domain.com/api/auth/callback/github
- Development:
- Register Application: Click [Register application].
- Copy Credentials: On the next page, you'll see your Client ID. Click [Generate a new client secret]. Copy both the Client ID and the newly generated Client Secret. Paste them into your
.env.local
file asAUTH_GITHUB_ID
andAUTH_GITHUB_SECRET
.
3. Setup
Server Side
Cient Side
4. Testing
- Restart Server: Stop and restart your development server (
npm run dev
). - Sign In: Navigate to your application's sign-in page. You should now see a "Sign in with GitHub" option.
- Authorize: Click the button. You'll be redirected to GitHub to authorize the application. After authorization, you should be redirected back to your application, logged in.
Unlike Google, GitHub OAuth apps generally don't require a separate verification process for production use beyond correctly configuring the callback URLs.
You can now use GitHub for authentication in StarterKitPro!
Add other social providers
Just like github you can add twitter, pintrest etc check the Auth doc