|
3 | 3 | [Passport](https://github.com/jaredhanson/passport) strategy for authenticating
|
4 | 4 | with GitHub using the OAuth 2.0 API.
|
5 | 5 |
|
| 6 | +## Installation |
| 7 | + |
| 8 | + $ npm install passport-github |
| 9 | + |
| 10 | +## Usage |
| 11 | + |
| 12 | +#### Configure Strategy |
| 13 | + |
| 14 | +The GitHub authentication strategy authenticates users using a GitHub account |
| 15 | +and OAuth 2.0 tokens. The strategy requires a `verify` callback, which accepts |
| 16 | +these credentials and calls `done` providing a user, as well as `options` |
| 17 | +specifying a client ID, client secret, and callback URL. |
| 18 | + |
| 19 | + passport.use(new GitHubStrategy({ |
| 20 | + clientID: GITHUB_CLIENT_ID, |
| 21 | + clientSecret: GITHUB_CLIENT_SECRET, |
| 22 | + callbackURL: "http://127.0.0.1:3000/auth/github/callback" |
| 23 | + }, |
| 24 | + function(accessToken, refreshToken, profile, done) { |
| 25 | + User.findOrCreate({ githubId: profile.id }, function (err, user) { |
| 26 | + return done(err, user); |
| 27 | + }); |
| 28 | + } |
| 29 | + )); |
| 30 | + |
| 31 | +#### Authenticate Requests |
| 32 | + |
| 33 | +Use `passport.authenticate()`, specifying the `'github'` strategy, to |
| 34 | +authenticate requests. |
| 35 | + |
| 36 | +For example, as route middleware in an [Express](http://expressjs.com/) |
| 37 | +application: |
| 38 | + |
| 39 | + app.get('/auth/github', |
| 40 | + passport.authenticate('github'), |
| 41 | + function(req, res){ |
| 42 | + // The request will be redirected to GitHub for authentication, so this |
| 43 | + // function will not be called. |
| 44 | + }); |
| 45 | + |
| 46 | + app.get('/auth/github/callback', |
| 47 | + passport.authenticate('github', { failureRedirect: '/login' }), |
| 48 | + function(req, res) { |
| 49 | + // Successful authentication, redirect home. |
| 50 | + res.redirect('/'); |
| 51 | + }); |
| 52 | + |
| 53 | +#### Examples |
| 54 | + |
| 55 | +For a complete, working example, refer to the [login example](https://github.com/jaredhanson/passport-github/tree/master/examples/login). |
| 56 | + |
6 | 57 | ## Credits
|
7 | 58 |
|
8 | 59 | - [Jared Hanson](http://github.com/jaredhanson)
|
|
0 commit comments