A Next.js application demonstrating GitHub OAuth2 authentication flow using access token. This project showcases how to implement secure authentication with GitHub OAuth2, including session management and protected routes.
- 🔐 GitHub OAuth2 authentication flow
- 🍪 Session management with HTTP-only cookies
- 🛡️ Protected routes with middleware
- 📦 Product listing page (requires authentication)
- 🎨 Modern UI with Tailwind CSS
- Framework: Next.js 16.1.1
- Runtime: React 19.2.3
- Language: TypeScript 5
- Styling: Tailwind CSS 4
- Package Manager: pnpm 10.15.0
- Node.js 18+
- pnpm (recommended) or npm/yarn
- Install dependencies:
pnpm installCreate a .env.local file in the root directory with the following environment variables:
# GitHub OAuth2 Configuration
# The begin step, get auth code by your client_id, scope & etc. configs.
GITHUB_AUTH_URL=https://github.com/login/oauth/authorize
# Get the access token from your auth provider with your client_id, client_secret and auth_code.
GITHUB_TOKEN_URL=https://github.com/login/oauth/access_token
# The callback endpoint once the auth flow is succeed, should be setup by the auth provider.
GITHUB_REDIRECT_URI=http://localhost:3000/api/auth/github/callback
# The client id once you setup your auth provider(better not to share, not strictly to confidential).
GITHUB_CLIENT_ID=your_github_client_id
# The client secret generated by the auth provider(***CONFIDENTIAL, NEVER SHARE***).
GITHUB_CLIENT_SECRET=your_github_client_secret
# The open api to get userInfo of github(since github doesn't support OIDC).
GITHUB_USER_INFO_URL=https://api.github.com/user
# Revoke the consent of your authentication
GITHUB_REVOKE_TOEKN_URL=https://api.github.com/applications/{client_id}/token
# Application URL (optional, defaults to http://localhost:3000)
NEXT_PUBLIC_APP_URL=http://localhost:3000- Go to GitHub Settings → Developer settings → OAuth Apps
- Click "New OAuth App"
- Fill in the application details:
- Application name: Your app name
- Homepage URL:
http://localhost:3000 - Authorization callback URL:
http://localhost:3000/api/auth/github/callback
- Click "Register application"
- Copy the Client ID and generate a Client Secret
- Add these values to your
.env.localfile
Start the development server:
pnpm devOpen http://localhost:3000 in your browser.
├── app/
│ ├── (protected)/ # Protected routes
│ │ └── products/ # Products page (requires auth)
│ ├── api/ # API routes
│ │ ├── auth/ # Authentication endpoints
│ │ └── (protected)/ # Protected API endpoints
│ └── page.tsx # Home page
├── components/ # React components
├── hooks/ # Custom React hooks
├── lib/ # Utility libraries
│ ├── auth/ # Authentication logic
│ ├── oauth/ # OAuth implementation
│ └── products/ # Product data
└── middleware.ts # Next.js middleware for route protection
-
Home Page (
/):- Displays welcome message
- Shows login button if not authenticated
- Shows available pages and logout button if authenticated
-
Products Page (
/products):- Protected route requiring authentication
- Displays a list of products
- Redirects to home if not authenticated
-
Authentication Flow:
- Click "Login" to initiate GitHub OAuth2 flow
- Redirects to GitHub for authorization
- After authorization, redirects back to the application
- Session is stored in HTTP-only cookie
- User info is displayed in the header bar
This project is private and not licensed for public use.