DEV Community

Cover image for Easiest way to make Google and Local authentication using Passport.js
Muhammad ABir
Muhammad ABir

Posted on

12

Easiest way to make Google and Local authentication using Passport.js

To use Passport.js for both Google and local (username and password) authentication in a Node.js application, you will need to:

  1. Install the passport-google-oauth20 and passport-local packages by running npm install passport-google-oauth20 passport-local in your project's root directory.

  2. Create a new Google API Console project and configure it to allow your application to use the Google+ API and generate OAuth 2.0 credentials (client ID and client secret).

  3. In your Node.js application, import the passport-google-oauth20 strategy and configure it with your client ID and client secret. Also import the passport-local strategy, and configure it with the appropriate options.

  4. Create routes for handling the Google OAuth callback and local login.

  5. Initialize Passport.js and use the Google strategy and local strategy for authentication.

  6. Add the appropriate links in your application to initiate the authentication process with Google or local login.

Here is an example of how to set up Passport.js for both Google and local authentication in a Node.js application:

const express = require('express');
const passport = require('passport');
const GoogleStrategy = require('passport-google-oauth20').Strategy;
const LocalStrategy = require('passport-local').Strategy;

const app = express();

passport.use(new GoogleStrategy({
    clientID: 'YOUR_CLIENT_ID',
    clientSecret: 'YOUR_CLIENT_SECRET',
    callbackURL: 'http://localhost:3000/auth/google/callback'
  },
  (accessToken, refreshToken, profile, cb) => {
    // Perform any additional verification or user lookup here
    // and return the user object
    return cb(null, profile);
  }
));

passport.use(new LocalStrategy((username, password, cb) => {
    // Perform user lookup or verification here
    // and return the user object
    const user = { id: 1, username: 'user', password: 'password' };
    if (username !== user.username || password !== user.password) {
      return cb(null, false);
    }
    return cb(null, user);
}));

app.get('/auth/google', passport.authenticate('google', { scope: ['profile'] }));

app.get('/auth/google/callback', 
  passport.authenticate('google', { failureRedirect: '/login' }),
  (req, res) => {
    // Successful authentication, redirect home.
    res.redirect('/');
  });

app.post('/login', passport.authenticate('local', { failureRedirect: '/login', successRedirect: '/' }));

app.listen(3000);
Enter fullscreen mode Exit fullscreen mode

This is a basic example, you might need to customize this according to your application needs, like adding a user model, using bcrypt to hash passwords, etc.

also checkout the github repo

SurveyJS custom survey software

Simplify data collection in your JS app with a fully integrated form management platform. Includes support for custom question types, skip logic, integrated CCS editor, PDF export, real-time analytics & more. Integrates with any backend system, giving you full control over your data and no user limits.

Learn more

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay