Imagine that we have a task to set OAuth2 to our application in order to increase security. To make that happen, we need a provider to provide us with secret keys. Those are the clientID and the clientSecret keys. GitHub provides us with the necessary source.
Here is a little piece of Passport.js code optimized for GitHub OAuth2 authentication:
// Set up Passport.js for GitHub OAuth2 authentication
passport.use(new GitHubStrategy({
clientID: 'YOUR_GITHUB_CLIENT_ID', // Replace with your GitHub Client ID
clientSecret: 'YOUR_GITHUB_CLIENT_SECRET', // Replace with your GitHub Client Secret
callbackURL: 'https://localhost:4000/auth/github/callback', // GitHub callback URL
}, (accessToken, refreshToken, profile, done) => {
// Store user profile and token in the session or database
return done(null, { accessToken, profile });
}));
Here are the steps we need to take to get these credentials:
- Go to GitHub Developer Settings.
- Create a new OAuth application or use an existing one.
- Fill in the details for the OAuth application:
- Application Name: Give your app a name.
- Homepage URL: Use
https://localhost:4000
(or your production URL if applicable). - Authorization callback URL: Use
https://localhost:4000/auth/github/callback
.
After creating the OAuth app, you’ll receive your Client ID and Client Secret. Replace the YOUR_GITHUB_CLIENT_ID
and YOUR_GITHUB_CLIENT_SECRET
in the code with these values.