1 | # Passport-Google-OAuth
|
2 |
|
3 | [Passport](http://passportjs.org/) strategies for authenticating with [Google](http://www.google.com/)
|
4 | using OAuth 1.0a and OAuth 2.0.
|
5 |
|
6 | This module lets you authenticate using Google in your Node.js applications.
|
7 | By plugging into Passport, Google authentication can be easily and
|
8 | unobtrusively integrated into any application or framework that supports
|
9 | [Connect](http://www.senchalabs.org/connect/)-style middleware, including
|
10 | [Express](http://expressjs.com/).
|
11 |
|
12 | The 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 |
|
22 | The Google OAuth 1.0 authentication strategy authenticates users using a Google
|
23 | account and OAuth tokens. The strategy requires a `verify` callback, which
|
24 | accepts these credentials and calls `done` providing a user, as well as `options`
|
25 | specifying a consumer key, consumer secret, and callback URL.
|
26 |
|
27 | ```Javascript
|
28 | var GoogleStrategy = require('passport-google-oauth').OAuthStrategy;
|
29 |
|
30 | passport.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 |
|
45 | Use `passport.authenticate()`, specifying the `'google'` strategy, to
|
46 | authenticate requests.
|
47 |
|
48 | For example, as route middleware in an [Express](http://expressjs.com/)
|
49 | application:
|
50 |
|
51 | ```Javascript
|
52 | app.get('/auth/google',
|
53 | passport.authenticate('google', { scope: 'https://www.google.com/m8/feeds' }));
|
54 |
|
55 | app.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 |
|
67 | The Google OAuth 2.0 authentication strategy authenticates users using a Google
|
68 | account and OAuth 2.0 tokens. The strategy requires a `verify` callback, which
|
69 | accepts 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
|
73 | var GoogleStrategy = require('passport-google-oauth').OAuth2Strategy;
|
74 |
|
75 | passport.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 |
|
90 | Use `passport.authenticate()`, specifying the `'google'` strategy, to
|
91 | authenticate requests.
|
92 |
|
93 | For example, as route middleware in an [Express](http://expressjs.com/)
|
94 | application:
|
95 |
|
96 | ```Javascript
|
97 | app.get('/auth/google',
|
98 | passport.authenticate('google'));
|
99 |
|
100 | app.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 |
|
110 | For a complete, working example, refer to the [OAuth 1.0 example](https://github.com/jaredhanson/passport-google-oauth/tree/master/examples/oauth)
|
111 | and 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 |
|
128 | Copyright (c) 2012-2013 Jared Hanson <[http://jaredhanson.net/](http://jaredhanson.net/)>
|