{"id":13751513,"url":"https://github.com/jaredhanson/passport-google-oauth2","last_synced_at":"2025-05-13T01:45:16.820Z","repository":{"id":37397008,"uuid":"51038048","full_name":"jaredhanson/passport-google-oauth2","owner":"jaredhanson","description":"Google authentication strategy for Passport and Node.js.","archived":false,"fork":false,"pushed_at":"2023-05-10T15:39:43.000Z","size":59,"stargazers_count":836,"open_issues_count":55,"forks_count":154,"subscribers_count":30,"default_branch":"master","last_synced_at":"2025-05-13T01:45:11.231Z","etag":null,"topics":["google","oauth2","passport"],"latest_commit_sha":null,"homepage":"https://www.passportjs.org/packages/passport-google-oauth20/?utm_source=github\u0026utm_medium=referral\u0026utm_campaign=passport-google-oauth20\u0026utm_content=about","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/jaredhanson.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":"CONTRIBUTING.md","funding":".github/FUNDING.yml","license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null},"funding":{"github":"jaredhanson"}},"created_at":"2016-02-03T23:21:10.000Z","updated_at":"2025-04-22T07:10:53.000Z","dependencies_parsed_at":"2024-06-18T11:23:32.973Z","dependency_job_id":null,"html_url":"https://github.com/jaredhanson/passport-google-oauth2","commit_stats":{"total_commits":72,"total_committers":3,"mean_commits":24.0,"dds":0.02777777777777779,"last_synced_commit":"4f98d9fcee98c8af31862c4f8be2c7b70d6fe9ef"},"previous_names":[],"tags_count":2,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jaredhanson%2Fpassport-google-oauth2","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jaredhanson%2Fpassport-google-oauth2/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jaredhanson%2Fpassport-google-oauth2/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jaredhanson%2Fpassport-google-oauth2/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/jaredhanson","download_url":"https://codeload.github.com/jaredhanson/passport-google-oauth2/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":253856613,"owners_count":21974575,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["google","oauth2","passport"],"created_at":"2024-08-03T09:00:47.159Z","updated_at":"2025-05-13T01:45:16.806Z","avatar_url":"https://github.com/jaredhanson.png","language":"JavaScript","funding_links":["https://github.com/sponsors/jaredhanson"],"categories":["*.js"],"sub_categories":["Node"],"readme":"# passport-google-oauth20\n\n[Passport](https://www.passportjs.org/) strategy for authenticating with\n[Google](https://www.google.com/) using [OAuth 2.0](https://www.passportjs.org/features/oauth2/).\n\nThis module lets you authenticate using Google in your Node.js applications.\nBy plugging into Passport, Sign In with Google can be easily and unobtrusively\nintegrated into any application or framework that supports\n[Connect](https://github.com/senchalabs/connect#readme)-style middleware,\nincluding [Express](https://expressjs.com/).\n\n\u003cdiv align=\"center\"\u003e\n  \n:brain: [Understanding OAuth 2.0](https://www.passportjs.org/concepts/oauth2/?utm_source=github\u0026utm_medium=referral\u0026utm_campaign=passport-google-oauth20\u0026utm_content=nav-concept) •\n:heart: [Sponsors](https://www.passportjs.org/sponsors/?utm_source=github\u0026utm_medium=referral\u0026utm_campaign=passport-google-oauth20\u0026utm_content=nav-sponsors)\n\n\u003c/div\u003e\n\n\u003cdiv align=\"right\"\u003e\n  \u003csup\u003eDeveloped by \u003ca href=\"#authors\"\u003eJared Hanson\u003c/a\u003e.\u003c/sub\u003e\n\u003c/div\u003e\n\n---\n\n\u003cdiv align=\"center\"\u003e\n  \u003csup\u003eAdvertisement\u003c/sup\u003e\n  \u003cbr\u003e\n  \u003ca href=\"https://click.linksynergy.com/link?id=D*o7yui4/NM\u0026offerid=507388.922484\u0026type=2\u0026murl=https%3A%2F%2Fwww.udemy.com%2Fcourse%2Fthe-complete-nodejs-developer-course-2%2F\u0026u1=21vl3Skt2fz9CZkPcetSToKITTB1Zqdi0ZpnMwjAMqG7p2N3J2BC5\"\u003eThe Complete Node.js Developer Course\u003c/a\u003e\u003cbr\u003eLearn Node. js by building real-world applications with Node, Express, MongoDB, Jest, and more!\n\u003c/div\u003e\n\n---\n\n## Install\n\n```sh\n$ npm install passport-google-oauth20\n```\n\n###### TypeScript type declarations\n\n```sh\n$ npm install @types/passport-google-oauth20\n```\n\n## Usage\n\n#### Register Application\n\nThe Google strategy authenticates users using their Google account.  Before your\napplication can make use of Google's authentication system, you must first\n[register](https://support.google.com/cloud/answer/6158849) your app to use\nOAuth 2.0 with Google APIs.  Once registered, a client ID and secret will be\nissued which are used by Google to identify your app.\n\n#### Configure Strategy\n\nOnce you've [registered your application](#register-application), the strategy\nneeds to be configured with your application's client ID and secret, along with\nits OAuth 2.0 redirect endpoint.\n\nThe strategy takes a `verify` function as an argument, which accepts\n`accessToken`, `refreshToken`, and `profile` as arguments.  `accessToken` and\n`refreshToken` are used for API access, and are not needed for authentication.\n`profile` contains the user's [profile information](https://www.passportjs.org/reference/normalized-profile/)\nstored in their Google account.  When authenticating a user, this strategy uses\nthe OAuth 2.0 protocol to obtain this information via a sequence of redirects\nand API requests to Google.\n\nThe `verify` function is responsible for determining the user to which the\nGoogle account belongs.  In cases where the account is logging in for the\nfirst time, a new user record is typically created automatically.  On subsequent\nlogins, the existing user record will be found via its relation to the Google\naccount.\n\nBecause the `verify` function is supplied by the application, the app is free to\nuse any database of its choosing.  The example below illustrates usage of a SQL\ndatabase.\n\n```js\nvar GoogleStrategy = require('passport-google-oauth20');\n\npassport.use(new GoogleStrategy({\n    clientID: process.env['GOOGLE_CLIENT_ID'],\n    clientSecret: process.env['GOOGLE_CLIENT_SECRET'],\n    callbackURL: 'https://www.example.com/oauth2/redirect/google',\n    scope: [ 'profile' ],\n    state: true\n  },\n  function verify(accessToken, refreshToken, profile, cb) {\n    db.get('SELECT * FROM federated_credentials WHERE provider = ? AND subject = ?', [\n      'https://accounts.google.com',\n      profile.id\n    ], function(err, cred) {\n      if (err) { return cb(err); }\n      \n      if (!cred) {\n        // The account at Google has not logged in to this app before.  Create a\n        // new user record and associate it with the Google account.\n        db.run('INSERT INTO users (name) VALUES (?)', [\n          profile.displayName\n        ], function(err) {\n          if (err) { return cb(err); }\n          \n          var id = this.lastID;\n          db.run('INSERT INTO federated_credentials (user_id, provider, subject) VALUES (?, ?, ?)', [\n            id,\n            'https://accounts.google.com',\n            profile.id\n          ], function(err) {\n            if (err) { return cb(err); }\n            \n            var user = {\n              id: id,\n              name: profile.displayName\n            };\n            return cb(null, user);\n          });\n        });\n      } else {\n        // The account at Google has previously logged in to the app.  Get the\n        // user record associated with the Google account and log the user in.\n        db.get('SELECT * FROM users WHERE id = ?', [ cred.user_id ], function(err, user) {\n          if (err) { return cb(err); }\n          if (!user) { return cb(null, false); }\n          return cb(null, user);\n        });\n      }\n    });\n  }\n));\n```\n\n#### Define Routes\n\nTwo routes are needed in order to allow users to log in with their Google\naccount.  The first route redirects the user to the Google, where they will\nauthenticate:\n\n```js\napp.get('/login/google', passport.authenticate('google'));\n```\n\nThe second route processes the authentication response and logs the user in,\nafter Google redirects the user back to the app:\n\n```js\napp.get('/oauth2/redirect/google',\n  passport.authenticate('google', { failureRedirect: '/login', failureMessage: true }),\n  function(req, res) {\n    res.redirect('/');\n  });\n```\n\n## Documentation\n\n* [Using OAuth 2.0 to Access Google APIs](https://developers.google.com/identity/protocols/oauth2/)\n\n  Official Google documentation on how to use OAuth 2.0 to access Google APIs.\n\n## Examples\n\n* [todos-express-google-oauth2](https://github.com/passport/todos-express-google-oauth2)\n\n  Illustrates how to use the Google strategy within an Express application.\n\n## Related Packages\n\n* [passport-google-oidc](https://www.passportjs.org/packages/passport-google-oidc/)\n\n  Passport strategy for authenticating with Google using OpenID Connect.\n\n## Authors\n\n- [Jared Hanson](https://www.jaredhanson.me/) { [![WWW](https://raw.githubusercontent.com/jaredhanson/jaredhanson/master/images/globe-12x12.svg)](https://www.jaredhanson.me/) [![Facebook](https://raw.githubusercontent.com/jaredhanson/jaredhanson/master/images/facebook-12x12.svg)](https://www.facebook.com/jaredhanson) [![LinkedIn](https://raw.githubusercontent.com/jaredhanson/jaredhanson/master/images/linkedin-12x12.svg)](https://www.linkedin.com/in/jaredhanson) [![Twitter](https://raw.githubusercontent.com/jaredhanson/jaredhanson/master/images/twitter-12x12.svg)](https://twitter.com/jaredhanson) [![GitHub](https://raw.githubusercontent.com/jaredhanson/jaredhanson/master/images/github-12x12.svg)](https://github.com/jaredhanson) }\n\n## License\n\n[The MIT License](http://opensource.org/licenses/MIT)\n\nCopyright (c) 2012-2023 Jared Hanson\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjaredhanson%2Fpassport-google-oauth2","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjaredhanson%2Fpassport-google-oauth2","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjaredhanson%2Fpassport-google-oauth2/lists"}