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:Interactive Setup
The CLI will guide you through an interactive setup process with the following prompts:Specify Entry File
Enter your project’s main entry file (relative to root)
For TypeScript projects, if the file doesn’t exist, Devark will auto-detect
.ts files in your src/ directory.Dependencies Installed
The module automatically installs the following packages:Runtime Dependencies
TypeScript Dev Dependencies (TypeScript only)
Generated File Structure
- JavaScript
- TypeScript
Configuration
The following environment variables are automatically added to your.env file:
Your Google OAuth 2.0 Client ID from Google Cloud Console
Your Google OAuth 2.0 Client Secret from Google Cloud Console
The callback URL where Google redirects after authentication
Secret key used to sign the session ID cookie
Example .env
Generated Code
Strategy Configuration
- JavaScript
- TypeScript
The module generates
config/googleStrategy.js:Authentication Routes
- JavaScript
- TypeScript
The module generates
routes/googleAuthRoutes.js:Usage Example
After installation, your app will automatically have Google OAuth configured. To test:Available Routes
GET /auth/google- Initiates Google OAuth flowGET /auth/google/callback- Handles Google’s redirect after authenticationGET /success- Success page displaying user informationGET /auth/failure- Failure page for authentication errors
App.js Patching
The module intelligently patches your main app file to include:- Required imports for passport and session management
- Session middleware configuration (placed before passport initialization)
- Passport initialization middleware
- 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
Error: Entry file not found
Error: Entry file not found
Solution: Ensure your
app.js or src/app.ts file exists before running the module installation. Create the file if it doesn’t exist:Error: Redirect URI mismatch
Error: Redirect URI mismatch
Solution: Ensure the callback URL in your Google Cloud Console matches the
GOOGLE_CALLBACK_URL in your .env file.- Go to Google Cloud Console
- Navigate to APIs & Services → Credentials
- Edit your OAuth 2.0 Client ID
- Add
http://localhost:3000/auth/google/callbackto Authorized redirect URIs
Session is not persisting
Session is not persisting
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:TypeScript errors after installation
TypeScript errors after installation
Solution: The module installs TypeScript types automatically. If you still see errors:
Cannot find module errors
Cannot find module errors
Solution: If dependencies weren’t installed properly, manually install them:
Getting Google OAuth Credentials
Create a Google Cloud Project
Visit Google Cloud Console and create a new project.
Set Authorized Redirect URIs
Add your callback URL (e.g.,
http://localhost:3000/auth/google/callback) to the authorized redirect URIs.Next Steps
GitHub OAuth
Add GitHub authentication to your project
Resend OTP
Add email OTP verification

