Google Authentication using Passport in Node.js Last Updated : 01 Aug, 2024 Summarize Comments Improve Suggest changes Share Like Article Like Report The following approach covers how to authenticate with google using passport in nodeJs. Authentication is basically the verification of users before granting them access to the website or services. Authentication which is done using a Google account is called Google Authentication. We can do Google authentication using OAuth API which is provided by Google on their developer portal.Creating Node Project And Installing Module:Step 1: Creating Node project using the following command.npm init Keep pressing enter and enter “yes/no” accordingly at the terminus line.Step 2: Installing required modules.npm install express passport passport-google-oauth2 cookie-sessionStep 3: Creating two files index.js and passport.jsProject Structure:Step 4: Create basic server. index.js const express = require('express'); const app = express(); app.get('/' , (req , res) => { res.send("<h1>GeeksForGeeks</h1>"); }); app.listen(4000 , () => { console.log("Server running on port 4000"); }); Output:Step 5: Now go to google platform and generate your credentials.Step 6: creating login functionality. passport.js const passport = require('passport'); const GoogleStrategy = require('passport-google-oauth2').Strategy; passport.serializeUser((user , done) => { done(null , user); }) passport.deserializeUser(function(user, done) { done(null, user); }); passport.use(new GoogleStrategy({ clientID:"YOUR ID", // Your Credentials here. clientSecret:"YOUR SECRET", // Your Credentials here. callbackURL:"http://localhost:4000/auth/callback", passReqToCallback:true }, function(request, accessToken, refreshToken, profile, done) { return done(null, profile); } )); index.js const express = require('express'); const app = express(); const passport = require('passport'); const cookieSession = require('cookie-session'); require('./passport'); app.use(cookieSession({ name: 'google-auth-session', keys: ['key1', 'key2'] })); app.use(passport.initialize()); app.use(passport.session()); app.get('/', (req, res) => { res.send("<button><a href='/auth'>Login With Google</a></button>") }); // Auth app.get('/auth' , passport.authenticate('google', { scope: [ 'email', 'profile' ] })); // Auth Callback app.get( '/auth/callback', passport.authenticate( 'google', { successRedirect: '/auth/callback/success', failureRedirect: '/auth/callback/failure' })); // Success app.get('/auth/callback/success' , (req , res) => { if(!req.user) res.redirect('/auth/callback/failure'); res.send("Welcome " + req.user.email); }); // failure app.get('/auth/callback/failure' , (req , res) => { res.send("Error"); }) app.listen(4000 , () => { console.log("Server Running on port 4000"); }); Step to Run Application: Run the application using the following command from the root directory of the project:node index.jsOutput: Comment More infoAdvertise with us Next Article Basic Authentication in Node.js using HTTP Header I iamabhishekkalra Follow Improve Article Tags : Web Technologies Node.js Geeks Premier League Geeks-Premier-League-2022 NodeJS-Questions Similar Reads How to add authentication in file uploads using Node.js ? There are multiple ways to upload files and apply authentications to them. The easiest way to do so is to use a node module called multer. We can add authentication by restricting users on file uploads such as they can upload only pdf and the file size should be less than 1 Mb. There are many module 3 min read Node.js authentication using Passportjs and passport-local-mongoose Passport is the authentication middleware for Node. It is designed to serve a singular purpose which is to authenticate requests. It is not practical to store user password as the original string in the database but it is a good practice to hash the password and then store them into the database. Bu 5 min read Login Authentication using Express.js, Passport.js and BCrypt In this article, we will create a Login Authentication application. This application basically displays a login/register form interface and authenticate the user. All this logic of login/register authentication is implemented using express, mongodb, passport and bcrypt and frontend is created using 9 min read Basic Authentication in Node.js using HTTP Header Basic Authentication is a simple authentication method where the client sends a username and password encoded in base64 format in the HTTP request header.The basic authentication in the Node.js application can be done with the help express.js framework. Express.js framework is mainly used in Node.js 3 min read Google SignIn using Firebase Authentication in ReactJS Firebase simplifies mobile and web app development by offering pre-built features like user authentication (email/password, Google Sign-In, etc.) without the need to build complex backends. This saves time and resources for developers.In this article, we will discuss about the Google Sign-In feature 5 min read How to check user authentication in GET method using Node.js ? There are so many authentication methods like web token authentication, cookies based authentication, and many more. In this article, we will discuss one of the simplest authentication methods using express.js during handling clients get a request in node.js with the help of the HTTP headers. Appro 3 min read Like