or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

index.md
tile.json

tessl/npm-passport-google-oauth

Google OAuth authentication strategies for Passport.js

Workspace
tessl
Visibility
Public
Created
Last updated
Describes
npmpkg:npm/passport-google-oauth@2.0.x

To install, run

npx @tessl/cli install tessl/npm-passport-google-oauth@2.0.0

index.mddocs/

Passport Google OAuth

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.

Package Information

  • Package Name: passport-google-oauth
  • Package Type: npm
  • Language: JavaScript
  • Installation: npm install passport-google-oauth

Core Imports

var GoogleStrategy = require('passport-google-oauth').Strategy;  // OAuth 1.0a
var GoogleOAuth2Strategy = require('passport-google-oauth').OAuth2Strategy;  // OAuth 2.0

Individual strategy imports:

var { Strategy, OAuthStrategy, OAuth2Strategy } = require('passport-google-oauth');

Full module import:

var strategies = require('passport-google-oauth');

Basic Usage

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);
    });
  }
));

Architecture

This package is a meta-module that re-exports authentication strategies from two underlying packages:

  • passport-google-oauth1: Provides OAuth 1.0a strategy implementation
  • passport-google-oauth20: Provides OAuth 2.0 strategy implementation

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.

Capabilities

Strategy (OAuth 1.0a)

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;

OAuthStrategy (OAuth 1.0a)

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);

OAuth2Strategy

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);

Usage Examples

OAuth 2.0 Authentication (Recommended)

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('/');
  }
);

OAuth 1.0a Authentication (Legacy)

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('/');
  }
);

Migration Notes

As of version 1.0.0, it is recommended to declare dependencies on the specific OAuth version modules:

  • For OAuth 2.0: Use passport-google-oauth20 directly
  • For OAuth 1.0a: Use passport-google-oauth1 directly

This meta-module exists primarily for backwards compatibility with existing applications.