1.0.0 • Published 1 year ago
@md03/passport-github v1.0.0
GitHub strategy for Passport.js
Easy to use GitHub strategy. It's just OAuth2 for GitHub implementation.
Installation
# pnpm:
pnpm add @md03/passport-github
# npm:
npm install @md03/passport-github
# yarn:
yarn add @md03/passport-github
Options
clientId
(required) - used to exchange the code for an access token.clientSecret
(required) - used to exchange the code for an access token.passReqToCallback
(optional) - strategy will pass Request object to callback.codeFromRequest
(optional) - strategy will use provided function to extract code from request.
// example:
function getCode(req: Request) {
return req.body.code;
}
rawUser
(optional) - strategy will return raw result from GitHub's API instead of transformed user object.
Important things
- Use HTTPS
- Never include your secrets directly in your code base - use environment variables instead.
Usage
import { getGithubEmails, Strategy, GithubProfile } from '@md03/passport-github';
const githubStrategy = new Strategy({
clientId: process.env.GITHUB_CLIENT_ID,
clientSecret: process.env.GITHUB_CLIENT_SECRET,
}, async (accessToken, user: GithubProfile, verified) => {
const emails = await getGithubEmails(accessToken);
// perform some operations on emails, create new user in database
return verified(null, user);
});
app.use(passport.initialize());
passport.use(githubStrategy);
// callback path:
app.get('/github',
passport.authenticate('github', { session: false }),
(req, res) => {
res.json(req.user);
}
);
License
Distributed under the MIT License.
1.0.0
1 year ago