UNPKG

4.01 kBMarkdownView Raw
1# Passport-Google-OAuth
2
3[Passport](http://passportjs.org/) strategies for authenticating with [Google](http://www.google.com/)
4using OAuth 1.0a and OAuth 2.0.
5
6This module lets you authenticate using Google in your Node.js applications.
7By plugging into Passport, Google authentication can be easily and
8unobtrusively integrated into any application or framework that supports
9[Connect](http://www.senchalabs.org/connect/)-style middleware, including
10[Express](http://expressjs.com/).
11
12The client id and client secret needed to authenticate with Google can be set up from the developer's console [Google Developer's Console](https://console.developers.google.com/project).
13
14## Install
15
16 $ npm install passport-google-oauth
17
18## Usage of OAuth 1.0
19
20#### Configure Strategy
21
22The Google OAuth 1.0 authentication strategy authenticates users using a Google
23account and OAuth tokens. The strategy requires a `verify` callback, which
24accepts these credentials and calls `done` providing a user, as well as `options`
25specifying a consumer key, consumer secret, and callback URL.
26
27```Javascript
28var GoogleStrategy = require('passport-google-oauth').OAuthStrategy;
29
30passport.use(new GoogleStrategy({
31 consumerKey: GOOGLE_CONSUMER_KEY,
32 consumerSecret: GOOGLE_CONSUMER_SECRET,
33 callbackURL: "http://127.0.0.1:3000/auth/google/callback"
34 },
35 function(token, tokenSecret, profile, done) {
36 User.findOrCreate({ googleId: profile.id }, function (err, user) {
37 return done(err, user);
38 });
39 }
40));
41```
42
43#### Authenticate Requests
44
45Use `passport.authenticate()`, specifying the `'google'` strategy, to
46authenticate requests.
47
48For example, as route middleware in an [Express](http://expressjs.com/)
49application:
50
51```Javascript
52app.get('/auth/google',
53 passport.authenticate('google', { scope: 'https://www.google.com/m8/feeds' }));
54
55app.get('/auth/google/callback',
56 passport.authenticate('google', { failureRedirect: '/login' }),
57 function(req, res) {
58 // Successful authentication, redirect home.
59 res.redirect('/');
60 });
61```
62
63## Usage of OAuth 2.0
64
65#### Configure Strategy
66
67The Google OAuth 2.0 authentication strategy authenticates users using a Google
68account and OAuth 2.0 tokens. The strategy requires a `verify` callback, which
69accepts these credentials and calls `done` providing a user, as well as
70`options` specifying a client ID, client secret, and callback URL.
71
72```Javascript
73var GoogleStrategy = require('passport-google-oauth').OAuth2Strategy;
74
75passport.use(new GoogleStrategy({
76 clientID: GOOGLE_CLIENT_ID,
77 clientSecret: GOOGLE_CLIENT_SECRET,
78 callbackURL: "http://127.0.0.1:3000/auth/google/callback"
79 },
80 function(accessToken, refreshToken, profile, done) {
81 User.findOrCreate({ googleId: profile.id }, function (err, user) {
82 return done(err, user);
83 });
84 }
85));
86```
87
88#### Authenticate Requests
89
90Use `passport.authenticate()`, specifying the `'google'` strategy, to
91authenticate requests.
92
93For example, as route middleware in an [Express](http://expressjs.com/)
94application:
95
96```Javascript
97app.get('/auth/google',
98 passport.authenticate('google'));
99
100app.get('/auth/google/callback',
101 passport.authenticate('google', { failureRedirect: '/login' }),
102 function(req, res) {
103 // Successful authentication, redirect home.
104 res.redirect('/');
105 });
106```
107
108## Examples
109
110For a complete, working example, refer to the [OAuth 1.0 example](https://github.com/jaredhanson/passport-google-oauth/tree/master/examples/oauth)
111and the [OAuth 2.0 example](https://github.com/jaredhanson/passport-google-oauth/tree/master/examples/oauth2).
112
113## Tests
114
115 $ npm install --dev
116 $ make test
117
118[![Build Status](https://secure.travis-ci.org/jaredhanson/passport-google-oauth.png)](http://travis-ci.org/jaredhanson/passport-google-oauth)
119
120## Credits
121
122 - [Jared Hanson](http://github.com/jaredhanson)
123
124## License
125
126[The MIT License](http://opensource.org/licenses/MIT)
127
128Copyright (c) 2012-2013 Jared Hanson <[http://jaredhanson.net/](http://jaredhanson.net/)>