0
# Passport Google OAuth
1
2
Passport Google OAuth provides Google authentication strategies for Passport.js applications. This is a meta-module that combines OAuth 1.0a and OAuth 2.0 authentication strategies, offering a unified interface for Google authentication while maintaining backwards compatibility with existing applications.
3
4
## Package Information
5
6
- **Package Name**: passport-google-oauth
7
- **Package Type**: npm
8
- **Language**: JavaScript
9
- **Installation**: `npm install passport-google-oauth`
10
11
## Core Imports
12
13
```javascript
14
var GoogleStrategy = require('passport-google-oauth').Strategy; // OAuth 1.0a
15
var GoogleOAuth2Strategy = require('passport-google-oauth').OAuth2Strategy; // OAuth 2.0
16
```
17
18
Individual strategy imports:
19
20
```javascript
21
var { Strategy, OAuthStrategy, OAuth2Strategy } = require('passport-google-oauth');
22
```
23
24
Full module import:
25
26
```javascript
27
var strategies = require('passport-google-oauth');
28
```
29
30
## Basic Usage
31
32
```javascript
33
var passport = require('passport');
34
var GoogleOAuth2Strategy = require('passport-google-oauth').OAuth2Strategy;
35
36
// OAuth 2.0 strategy (recommended)
37
passport.use(new GoogleOAuth2Strategy({
38
clientID: GOOGLE_CLIENT_ID,
39
clientSecret: GOOGLE_CLIENT_SECRET,
40
callbackURL: "http://www.example.com/auth/google/callback"
41
},
42
function(accessToken, refreshToken, profile, done) {
43
User.findOrCreate({ googleId: profile.id }, function (err, user) {
44
return done(err, user);
45
});
46
}
47
));
48
49
// OAuth 1.0a strategy (legacy)
50
var GoogleStrategy = require('passport-google-oauth').Strategy;
51
52
passport.use(new GoogleStrategy({
53
consumerKey: GOOGLE_CONSUMER_KEY,
54
consumerSecret: GOOGLE_CONSUMER_SECRET,
55
callbackURL: "http://www.example.com/auth/google/callback"
56
},
57
function(token, tokenSecret, profile, done) {
58
User.findOrCreate({ googleId: profile.id }, function (err, user) {
59
return done(err, user);
60
});
61
}
62
));
63
```
64
65
## Architecture
66
67
This package is a meta-module that re-exports authentication strategies from two underlying packages:
68
69
- **passport-google-oauth1**: Provides OAuth 1.0a strategy implementation
70
- **passport-google-oauth20**: Provides OAuth 2.0 strategy implementation
71
72
The package serves as a backwards-compatibility layer, allowing applications to import both OAuth versions from a single package while maintaining support for legacy applications that used the combined package before version 1.0.0.
73
74
## Capabilities
75
76
### Strategy (OAuth 1.0a)
77
78
Default export providing backwards compatibility. This is an alias for OAuthStrategy.
79
80
```javascript { .api }
81
/**
82
* Backwards compatibility alias for OAuthStrategy
83
* @constructor
84
* @param {Object} options - Configuration options for OAuth 1.0a authentication
85
* @param {Function} verify - Verification callback function
86
*/
87
Strategy = OAuthStrategy;
88
```
89
90
### OAuthStrategy (OAuth 1.0a)
91
92
Google OAuth 1.0a authentication strategy for legacy applications.
93
94
```javascript { .api }
95
/**
96
* Google OAuth 1.0a authentication strategy
97
* @constructor
98
* @param {Object} options - Configuration options
99
* @param {string} options.consumerKey - Google consumer key
100
* @param {string} options.consumerSecret - Google consumer secret
101
* @param {string} options.callbackURL - Callback URL for authentication
102
* @param {Function} verify - Verification callback function
103
* @param {string} verify.token - OAuth token
104
* @param {string} verify.tokenSecret - OAuth token secret
105
* @param {Object} verify.profile - User profile information
106
* @param {Function} verify.done - Completion callback
107
*/
108
function OAuthStrategy(options, verify);
109
```
110
111
### OAuth2Strategy
112
113
Google OAuth 2.0 authentication strategy for modern applications.
114
115
```javascript { .api }
116
/**
117
* Google OAuth 2.0 authentication strategy
118
* @constructor
119
* @param {Object} options - Configuration options
120
* @param {string} options.clientID - Google client ID
121
* @param {string} options.clientSecret - Google client secret
122
* @param {string} options.callbackURL - Callback URL for authentication
123
* @param {string[]} [options.scope] - Access scopes to request
124
* @param {Function} verify - Verification callback function
125
* @param {string} verify.accessToken - OAuth 2.0 access token
126
* @param {string} verify.refreshToken - OAuth 2.0 refresh token
127
* @param {Object} verify.profile - User profile information
128
* @param {Function} verify.done - Completion callback
129
*/
130
function OAuth2Strategy(options, verify);
131
```
132
133
## Usage Examples
134
135
### OAuth 2.0 Authentication (Recommended)
136
137
```javascript
138
var passport = require('passport');
139
var GoogleOAuth2Strategy = require('passport-google-oauth').OAuth2Strategy;
140
141
passport.use(new GoogleOAuth2Strategy({
142
clientID: process.env.GOOGLE_CLIENT_ID,
143
clientSecret: process.env.GOOGLE_CLIENT_SECRET,
144
callbackURL: "/auth/google/callback",
145
scope: ['profile', 'email']
146
},
147
function(accessToken, refreshToken, profile, done) {
148
// Find or create user based on Google profile
149
return done(null, profile);
150
}
151
));
152
153
// Routes
154
app.get('/auth/google',
155
passport.authenticate('google', { scope: ['profile', 'email'] })
156
);
157
158
app.get('/auth/google/callback',
159
passport.authenticate('google', { failureRedirect: '/login' }),
160
function(req, res) {
161
res.redirect('/');
162
}
163
);
164
```
165
166
### OAuth 1.0a Authentication (Legacy)
167
168
```javascript
169
var passport = require('passport');
170
var GoogleStrategy = require('passport-google-oauth').Strategy;
171
172
passport.use(new GoogleStrategy({
173
consumerKey: process.env.GOOGLE_CONSUMER_KEY,
174
consumerSecret: process.env.GOOGLE_CONSUMER_SECRET,
175
callbackURL: "/auth/google/callback"
176
},
177
function(token, tokenSecret, profile, done) {
178
// Find or create user based on Google profile
179
return done(null, profile);
180
}
181
));
182
183
// Routes
184
app.get('/auth/google',
185
passport.authenticate('google')
186
);
187
188
app.get('/auth/google/callback',
189
passport.authenticate('google', { failureRedirect: '/login' }),
190
function(req, res) {
191
res.redirect('/');
192
}
193
);
194
```
195
196
## Migration Notes
197
198
As of version 1.0.0, it is recommended to declare dependencies on the specific OAuth version modules:
199
200
- For OAuth 2.0: Use `passport-google-oauth20` directly
201
- For OAuth 1.0a: Use `passport-google-oauth1` directly
202
203
This meta-module exists primarily for backwards compatibility with existing applications.