This is a simple Express application that uses GitHub OAuth 2.0 for authentication via Passport.js. It allows users to log in with their GitHub account, view a personalized profile page, and log out.
- GitHub OAuth 2.0 authentication with Passport.js
- Session-based authentication to keep users logged in
- Displays a personalized greeting on the profile page
- Simple login and logout functionality
To run this project, you'll need:
- Node.js (version 12 or higher)
- A GitHub OAuth application (to obtain the Client ID and Client Secret)
- A
.env
file to securely manage sensitive information
- Go to GitHub Developer Settings.
- Click on New OAuth App.
- Configure the application:
- Application Name: Choose any name.
- Homepage URL:
http://localhost:3000
- Authorization Callback URL:
http://localhost:3000/auth/github/callback
- Register the application.
- Note your Client ID and Client Secret – you’ll need them in the
.env
file.
git clone https://github.com/sahil352005/Github-OAuth2.0.git
cd Github-OAuth2.0
Run the following command to install the necessary dependencies:
npm install
Create a .env
file in the root directory of the project and add your GitHub OAuth credentials:
GITHUB_CLIENT_ID=your-client-id
GITHUB_CLIENT_SECRET=your-client-secret
Start the server with the following command:
npm start
The server should now be running at http://localhost:3000
.
/
- Home page with a "Login with GitHub" link./auth/github
- Initiates GitHub authentication./auth/github/callback
- Callback URL after GitHub authentication./profile
- Displays the user’s GitHub username (only accessible when logged in)./logout
- Logs the user out and redirects to the home page.
- When you navigate to the home page, click the "Login with GitHub" link.
- GitHub will ask for your permission to share your profile information.
- After granting permission, you’ll be redirected to the
/profile
page, where you’ll see a welcome message with your GitHub username. - Use the
/logout
route to end the session and return to the home page.
- Ensure that the GitHub Client ID and Client Secret in the
.env
file are correct. - Confirm that http://localhost:3000/auth/github/callback is added as an authorized callback URL in your GitHub Developer Settings.
- If you encounter an issue with
req.logout()
, ensure you’re using a version of Passport compatible with the session handling method used.
This project is open-source and available under the MIT License.
This README provides all necessary information to understand, set up, and run GitHub OAuth authentication in this application.