Google OAuth authentication strategies for Passport.js
npx @tessl/cli install tessl/npm-passport-google-oauth@2.0.0Passport 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.
npm install passport-google-oauthvar GoogleStrategy = require('passport-google-oauth').Strategy; // OAuth 1.0a
var GoogleOAuth2Strategy = require('passport-google-oauth').OAuth2Strategy; // OAuth 2.0Individual strategy imports:
var { Strategy, OAuthStrategy, OAuth2Strategy } = require('passport-google-oauth');Full module import:
var strategies = require('passport-google-oauth');var passport = require('passport');
var GoogleOAuth2Strategy = require('passport-google-oauth').OAuth2Strategy;
// OAuth 2.0 strategy (recommended)
passport.use(new GoogleOAuth2Strategy({
clientID: GOOGLE_CLIENT_ID,
clientSecret: GOOGLE_CLIENT_SECRET,
callbackURL: "http://www.example.com/auth/google/callback"
},
function(accessToken, refreshToken, profile, done) {
User.findOrCreate({ googleId: profile.id }, function (err, user) {
return done(err, user);
});
}
));
// OAuth 1.0a strategy (legacy)
var GoogleStrategy = require('passport-google-oauth').Strategy;
passport.use(new GoogleStrategy({
consumerKey: GOOGLE_CONSUMER_KEY,
consumerSecret: GOOGLE_CONSUMER_SECRET,
callbackURL: "http://www.example.com/auth/google/callback"
},
function(token, tokenSecret, profile, done) {
User.findOrCreate({ googleId: profile.id }, function (err, user) {
return done(err, user);
});
}
));This package is a meta-module that re-exports authentication strategies from two underlying packages:
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.
Default export providing backwards compatibility. This is an alias for OAuthStrategy.
/**
* Backwards compatibility alias for OAuthStrategy
* @constructor
* @param {Object} options - Configuration options for OAuth 1.0a authentication
* @param {Function} verify - Verification callback function
*/
Strategy = OAuthStrategy;Google OAuth 1.0a authentication strategy for legacy applications.
/**
* Google OAuth 1.0a authentication strategy
* @constructor
* @param {Object} options - Configuration options
* @param {string} options.consumerKey - Google consumer key
* @param {string} options.consumerSecret - Google consumer secret
* @param {string} options.callbackURL - Callback URL for authentication
* @param {Function} verify - Verification callback function
* @param {string} verify.token - OAuth token
* @param {string} verify.tokenSecret - OAuth token secret
* @param {Object} verify.profile - User profile information
* @param {Function} verify.done - Completion callback
*/
function OAuthStrategy(options, verify);Google OAuth 2.0 authentication strategy for modern applications.
/**
* Google OAuth 2.0 authentication strategy
* @constructor
* @param {Object} options - Configuration options
* @param {string} options.clientID - Google client ID
* @param {string} options.clientSecret - Google client secret
* @param {string} options.callbackURL - Callback URL for authentication
* @param {string[]} [options.scope] - Access scopes to request
* @param {Function} verify - Verification callback function
* @param {string} verify.accessToken - OAuth 2.0 access token
* @param {string} verify.refreshToken - OAuth 2.0 refresh token
* @param {Object} verify.profile - User profile information
* @param {Function} verify.done - Completion callback
*/
function OAuth2Strategy(options, verify);var passport = require('passport');
var GoogleOAuth2Strategy = require('passport-google-oauth').OAuth2Strategy;
passport.use(new GoogleOAuth2Strategy({
clientID: process.env.GOOGLE_CLIENT_ID,
clientSecret: process.env.GOOGLE_CLIENT_SECRET,
callbackURL: "/auth/google/callback",
scope: ['profile', 'email']
},
function(accessToken, refreshToken, profile, done) {
// Find or create user based on Google profile
return done(null, profile);
}
));
// Routes
app.get('/auth/google',
passport.authenticate('google', { scope: ['profile', 'email'] })
);
app.get('/auth/google/callback',
passport.authenticate('google', { failureRedirect: '/login' }),
function(req, res) {
res.redirect('/');
}
);var passport = require('passport');
var GoogleStrategy = require('passport-google-oauth').Strategy;
passport.use(new GoogleStrategy({
consumerKey: process.env.GOOGLE_CONSUMER_KEY,
consumerSecret: process.env.GOOGLE_CONSUMER_SECRET,
callbackURL: "/auth/google/callback"
},
function(token, tokenSecret, profile, done) {
// Find or create user based on Google profile
return done(null, profile);
}
));
// Routes
app.get('/auth/google',
passport.authenticate('google')
);
app.get('/auth/google/callback',
passport.authenticate('google', { failureRedirect: '/login' }),
function(req, res) {
res.redirect('/');
}
);As of version 1.0.0, it is recommended to declare dependencies on the specific OAuth version modules:
passport-google-oauth20 directlypassport-google-oauth1 directlyThis meta-module exists primarily for backwards compatibility with existing applications.