Skip to main content

Overview

The Google OAuth module integrates Google authentication into your existing Node.js Express application using Passport.js. The module automatically:
  • Installs all required dependencies
  • Generates configuration and route files
  • Patches your main app file intelligently
  • Injects environment variables to .env
  • Supports both JavaScript and TypeScript

Installation

Run the following command in your project directory:
npx devark add google-oauth

Interactive Setup

The CLI will guide you through an interactive setup process with the following prompts:
1

Select Language

Choose between JavaScript or TypeScript implementation
? Which version do you want to add for this module?
› JavaScript
  TypeScript
2

Specify Entry File

Enter your project’s main entry file (relative to root)
? Enter your project entry file (relative to root):
› app.js (for JavaScript)
› src/app.ts (for TypeScript)
For TypeScript projects, if the file doesn’t exist, Devark will auto-detect .ts files in your src/ directory.
3

Google Client ID

Enter your Google OAuth Client ID (or leave empty for sample values)
? Enter your Google Client ID (leave empty for sample):
› your-client-id
4

Google Client Secret

Enter your Google OAuth Client Secret (or leave empty for sample values)
? Enter your Google Client Secret (leave empty for sample):
› your-client-secret
5

Callback URL

Enter your OAuth callback URL
? Enter your Google Callback URL:
› http://localhost:3000/auth/google/callback
6

Session Secret

Enter a secret key for session management
? Enter your session secret (leave empty for sample):
› your-session-secret

Dependencies Installed

The module automatically installs the following packages:

Runtime Dependencies

express
passport
passport-google-oauth20
express-session
dotenv

TypeScript Dev Dependencies (TypeScript only)

typescript
ts-node
@types/node
@types/express
@types/express-session
@types/passport
@types/passport-google-oauth20

Generated File Structure

your-project/
├── config/
│   └── googleStrategy.js    # Passport strategy configuration
├── routes/
│   └── googleAuthRoutes.js  # OAuth routes
├── app.js                    # Patched with OAuth setup
└── .env                      # Environment variables

Configuration

The following environment variables are automatically added to your .env file:
GOOGLE_CLIENT_ID
string
required
Your Google OAuth 2.0 Client ID from Google Cloud Console
GOOGLE_CLIENT_SECRET
string
required
Your Google OAuth 2.0 Client Secret from Google Cloud Console
GOOGLE_CALLBACK_URL
string
default:"http://localhost:3000/auth/google/callback"
The callback URL where Google redirects after authentication
SESSION_SECRET
string
required
Secret key used to sign the session ID cookie

Example .env

GOOGLE_CLIENT_ID=your-client-id
GOOGLE_CLIENT_SECRET=your-client-secret
GOOGLE_CALLBACK_URL=http://localhost:3000/auth/google/callback
SESSION_SECRET=your-session-secret

Generated Code

Strategy Configuration

The module generates config/googleStrategy.js:
import passport from 'passport'
import { Strategy as GoogleStrategy } from 'passport-google-oauth20'

passport.use(new GoogleStrategy({
  clientID: process.env.GOOGLE_CLIENT_ID,
  clientSecret: process.env.GOOGLE_CLIENT_SECRET,
  callbackURL: '/auth/google/callback',
}, (accessToken, refreshToken, profile, done) => {
  // Replace this with DB logic
  return done(null, profile)
}))

passport.serializeUser((user, done) => {
  done(null, user)
})

passport.deserializeUser((user, done) => {
  done(null, user)
})

Authentication Routes

The module generates routes/googleAuthRoutes.js:
import express from "express";
import passport from "passport";

const router = express.Router();

// Step 1: Start Google login
router.get(
  "/auth/google",
  passport.authenticate("google", { scope: ["profile", "email"] })
);

// Step 2: Google OAuth callback
router.get(
  "/auth/google/callback",
  passport.authenticate("google", {
    failureRedirect: "/auth/failure",
    successRedirect: "/success",
  })
);

// Step 3: Success page
router.get("/success", (req, res) => {
  const user = req.user;
  
  if (user) {
    res.send(`
      <h1>✅ Logged In Successfully</h1>
      <p>Welcome, ${user.displayName}!</p>
      <p>Email: ${user.emails?.[0]?.value || "N/A"}</p>
      <p><a href="/">Go back home</a></p>
    `);
  } else {
    res.send(`
      <h1>Login Failed</h1>
      <p>No user data found.</p>
      <p><a href="/">Try again</a></p>
    `);
  }
});

// Step 4: Failure route
router.get("/auth/failure", (req, res) => {
  res.send("Failed to authenticate..");
});

export default router;

Usage Example

After installation, your app will automatically have Google OAuth configured. To test:
1

Start Your Server

npm start
2

Navigate to Login

Open your browser and visit:
http://localhost:3000/auth/google
3

Authenticate

You’ll be redirected to Google’s login page. After successful authentication, you’ll be redirected back to /success.

Available Routes

  • GET /auth/google - Initiates Google OAuth flow
  • GET /auth/google/callback - Handles Google’s redirect after authentication
  • GET /success - Success page displaying user information
  • GET /auth/failure - Failure page for authentication errors

App.js Patching

The module intelligently patches your main app file to include:
  1. Required imports for passport and session management
  2. Session middleware configuration (placed before passport initialization)
  3. Passport initialization middleware
  4. Route registration for Google OAuth routes
The patching logic ensures no duplicate imports or middleware declarations. It detects existing setup and only adds what’s missing.

Troubleshooting

Solution: Ensure your app.js or src/app.ts file exists before running the module installation. Create the file if it doesn’t exist:
touch app.js  # for JavaScript
mkdir -p src && touch src/app.ts  # for TypeScript
Solution: Ensure the callback URL in your Google Cloud Console matches the GOOGLE_CALLBACK_URL in your .env file.
  1. Go to Google Cloud Console
  2. Navigate to APIs & Services → Credentials
  3. Edit your OAuth 2.0 Client ID
  4. Add http://localhost:3000/auth/google/callback to Authorized redirect URIs
Solution: Ensure you have a strong SESSION_SECRET in your .env file and that express-session is configured before passport initialization.The module handles this automatically, but if you’re manually configuring, ensure this order:
app.use(session({ secret: process.env.SESSION_SECRET }))
app.use(passport.initialize())
app.use(passport.session())
Solution: The module installs TypeScript types automatically. If you still see errors:
npm install --save-dev @types/express @types/passport @types/passport-google-oauth20
Solution: If dependencies weren’t installed properly, manually install them:
npm install express passport passport-google-oauth20 express-session dotenv

Getting Google OAuth Credentials

1

Create a Google Cloud Project

Visit Google Cloud Console and create a new project.
2

Enable Google+ API

Navigate to APIs & ServicesLibrary and enable the Google+ API.
3

Create OAuth Credentials

Go to APIs & ServicesCredentialsCreate CredentialsOAuth client ID
4

Configure OAuth Consent Screen

Fill in the required information for your OAuth consent screen.
5

Set Authorized Redirect URIs

Add your callback URL (e.g., http://localhost:3000/auth/google/callback) to the authorized redirect URIs.
6

Copy Credentials

Copy the Client ID and Client Secret and add them to your .env file.

Next Steps

GitHub OAuth

Add GitHub authentication to your project

Resend OTP

Add email OTP verification

Build docs developers (and LLMs) love