Installations
npm install @realmikesolo/passport-google-oauth20
Developer Guide
Typescript
No
Module System
CommonJS
Min. Node Version
>= 0.4.0
Node Version
16.13.0
NPM Version
8.1.0
Score
70.2
Supply Chain
97.2
Quality
74.8
Maintenance
100
Vulnerability
100
License
Releases
Unable to fetch releases
Contributors
Unable to fetch Contributors
Languages
JavaScript (99.65%)
Makefile (0.35%)
Love this project? Help keep it running — sponsor us today! 🚀
Developer
Download Statistics
Total Downloads
711
Last Day
1
Last Week
2
Last Month
17
Last Year
91
GitHub Statistics
MIT License
832 Stars
86 Commits
153 Forks
31 Watchers
1 Branches
2 Contributors
Updated on Feb 01, 2025
Bundle Size
27.82 kB
Minified
8.36 kB
Minified + Gzipped
Sponsor this package
Package Meta Information
Latest Version
2.0.2
Package Id
@realmikesolo/passport-google-oauth20@2.0.2
Unpacked Size
24.26 kB
Size
8.74 kB
File Count
15
NPM Version
8.1.0
Node Version
16.13.0
Published on
Mar 22, 2023
Total Downloads
Cumulative downloads
Total Downloads
711
Last Day
0%
1
Compared to previous day
Last Week
-75%
2
Compared to previous week
Last Month
466.7%
17
Compared to previous month
Last Year
-85.3%
91
Compared to previous year
Daily Downloads
Weekly Downloads
Monthly Downloads
Yearly Downloads
Dependencies
1
Dev Dependencies
4
passport-google-oauth20
Passport strategy for authenticating with Google using OAuth 2.0.
This module lets you authenticate using Google in your Node.js applications. By plugging into Passport, Sign In with Google can be easily and unobtrusively integrated into any application or framework that supports Connect-style middleware, including Express.
:brain: Understanding OAuth 2.0 • :heart: Sponsors
Developed by Jared Hanson.
Advertisement
The Complete Node.js Developer Course
Learn Node. js by building real-world applications with Node, Express, MongoDB, Jest, and more!
Install
1$ npm install passport-google-oauth20
TypeScript support
1$ npm install @types/passport-google-oauth20
Usage
Register Application
The Google strategy authenticates users using their Google account. Before your application can make use of Google's authentication system, you must first register your app to use OAuth 2.0 with Google APIs. Once registered, a client ID and secret will be issued which are used by Google to identify your app.
Configure Strategy
Once you've registered your application, the strategy needs to be configured with your application's client ID and secret, along with its OAuth 2.0 redirect endpoint.
The strategy takes a verify
function as an argument, which accepts
accessToken
, refreshToken
, and profile
as arguments. accessToken
and
refreshToken
are used for API access, and are not needed for authentication.
profile
contains the user's profile information
stored in their Google account. When authenticating a user, this strategy uses
the OAuth 2.0 protocol to obtain this information via a sequence of redirects
and API requests to Google.
The verify
function is responsible for determining the user to which the
Google account belongs. In cases where the account is logging in for the
first time, a new user record is typically created automatically. On subsequent
logins, the existing user record will be found via its relation to the Google
account.
Because the verify
function is supplied by the application, the app is free to
use any database of its choosing. The example below illustrates usage of a SQL
database.
1var GoogleStrategy = require('passport-google-oauth20'); 2 3passport.use(new GoogleStrategy({ 4 clientID: GOOGLE_CLIENT_ID, 5 clientSecret: GOOGLE_CLIENT_SECRET, 6 callbackURL: 'https://www.example.com/oauth2/redirect/google', 7 scope: [ 'profile' ], 8 state: true 9 }, 10 function verify(accessToken, refreshToken, profile, cb) { 11 db.get('SELECT * FROM federated_credentials WHERE provider = ? AND subject = ?', [ 12 'https://accounts.google.com', 13 profile.id 14 ], function(err, cred) { 15 if (err) { return cb(err); } 16 17 if (!cred) { 18 // The account at Google has not logged in to this app before. Create a 19 // new user record and associate it with the Google account. 20 db.run('INSERT INTO users (name) VALUES (?)', [ 21 profile.displayName 22 ], function(err) { 23 if (err) { return cb(err); } 24 25 var id = this.lastID; 26 db.run('INSERT INTO federated_credentials (user_id, provider, subject) VALUES (?, ?, ?)', [ 27 id, 28 'https://accounts.google.com', 29 profile.id 30 ], function(err) { 31 if (err) { return cb(err); } 32 var user = { 33 id: id, 34 name: profile.displayName 35 }; 36 return cb(null, user); 37 }); 38 }); 39 } else { 40 // The account at Google has previously logged in to the app. Get the 41 // user record associated with the Google account and log the user in. 42 db.get('SELECT * FROM users WHERE id = ?', [ cred.user_id ], function(err, row) { 43 if (err) { return cb(err); } 44 if (!row) { return cb(null, false); } 45 return cb(null, row); 46 }); 47 } 48 }); 49 } 50));
Define Routes
Two routes are needed in order to allow users to log in with their Google account. The first route redirects the user to the Google, where they will authenticate:
1app.get('/login/google', passport.authenticate('google'));
The second route processes the authentication response and logs the user in, after Google redirects the user back to the app:
1app.get('/oauth2/redirect/google', 2 passport.authenticate('google', { failureRedirect: '/login', failureMessage: true }), 3 function(req, res) { 4 res.redirect('/'); 5 });
Documentation
-
Using OAuth 2.0 to Access Google APIs
Official Google documentation on how to use OAuth 2.0 to access Google APIs.
Examples
-
Illustrates how to use the Google strategy within an Express application.
Authors
License
Copyright (c) 2012-2022 Jared Hanson <https://www.jaredhanson.me/>

No vulnerabilities found.
Reason
no dangerous workflow patterns detected
Reason
no binaries found in the repo
Reason
license file detected
Details
- Info: project has a license file: LICENSE:0
- Info: FSF or OSI recognized license: MIT License: LICENSE:0
Reason
0 existing vulnerabilities detected
Reason
Found 0/30 approved changesets -- score normalized to 0
Reason
no SAST tool detected
Details
- Warn: no pull requests merged into dev branch
Reason
detected GitHub workflow tokens with excessive permissions
Details
- Warn: no topLevel permission defined: .github/workflows/node.yml:1
- Info: no jobLevel write permissions found
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
dependency not pinned by hash detected -- score normalized to 0
Details
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/node.yml:36: update your workflow using https://app.stepsecurity.io/secureworkflow/jaredhanson/passport-google-oauth2/node.yml/master?enable=pin
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/node.yml:38: update your workflow using https://app.stepsecurity.io/secureworkflow/jaredhanson/passport-google-oauth2/node.yml/master?enable=pin
- Warn: npmCommand not pinned by hash: .github/workflows/node.yml:42
- Info: 0 out of 2 GitHub-owned GitHubAction dependencies pinned
- Info: 0 out of 1 npmCommand dependencies pinned
Reason
security policy file not detected
Details
- Warn: no security policy file detected
- Warn: no security file to analyze
- Warn: no security file to analyze
- Warn: no security file to analyze
Reason
project is not fuzzed
Details
- Warn: no fuzzer integrations found
Reason
branch protection not enabled on development/release branches
Details
- Warn: branch protection not enabled for branch 'master'
Score
3.4
/10
Last Scanned on 2025-02-10
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More