Quickstart Guide
This guide walks you through adding Google OAuth authentication to an existing Node.js project using Devark. You’ll go from zero to a working OAuth flow in about 5 minutes.Prerequisites
Before starting, make sure you have:- Node.js 18+ installed
- An existing Node.js project with
package.json - An Express.js app (or similar entry file like
app.js) - Devark installed (see Installation)
Don’t have a project yet? Create one quickly:
Step-by-Step Tutorial
Navigate to your project
Open your terminal and navigate to your Node.js project directory:Verify you have a
package.json file:Run the Devark command
Execute the command to add Google OAuth:You’ll see the Devark logo and initialization message:
Choose JavaScript or TypeScript
Devark will ask which language you want to use:Use arrow keys to select and press Enter. For this guide, we’ll choose JavaScript.
Specify your entry file
Enter the path to your main application file:Press Enter to use the default (
app.js) or type your custom path (e.g., src/index.js).Provide Google OAuth credentials
Devark will prompt for your Google OAuth credentials. You can skip these for now and add them later:
Wait for installation
Devark will now install dependencies and generate code:This installs:
expresspassportpassport-google-oauth20express-sessiondotenv
Review generated files
Devark has created several files in your project:Let’s examine what was added:app.js - Updated with middleware and route registration:.env - Environment variables for configuration:
Configure Google OAuth credentials
To make OAuth work, you need real credentials from Google Cloud:
- Go to Google Cloud Console
- Create a new project or select existing one
- Navigate to APIs & Services → Credentials
- Click Create Credentials → OAuth 2.0 Client ID
- Configure the consent screen if prompted
- Select Web application as application type
- Add authorized redirect URI:
- Copy the Client ID and Client Secret
.env file with the real credentials:Start your application
Run your Express.js application:If you have a start script in You should see:
package.json:Test the OAuth flow
Open your browser and navigate to:You should be redirected to Google’s login page. After signing in, Google will redirect you back to your callback URL.The generated routes provide these endpoints:Expected response:
- GET /auth/google - Initiates OAuth flow
- GET /auth/google/callback - Handles OAuth callback
- GET /auth/logout - Logs out the user
- GET /auth/profile - Returns user profile (if authenticated)
Understanding the Generated Code
Passport Strategy (config/googleStrategy.js)
This file configures Passport.js with the Google OAuth strategy:
Auth Routes (routes/googleAuthRoutes.js)
Provides ready-to-use OAuth endpoints:
Customizing the Integration
The generated code is fully editable. Common customizations:Save users to database
Modifyconfig/googleStrategy.js:
Add authentication middleware
Create middleware to protect routes:Customize redirect URLs
Edit the callback route inroutes/googleAuthRoutes.js:
Troubleshooting
”Redirect URI mismatch” error
Ensure the callback URL in Google Cloud Console exactly matches your.env file:
”Invalid client” error
Double-check yourGOOGLE_CLIENT_ID and GOOGLE_CLIENT_SECRET in .env. Make sure there are no extra spaces.
Session not persisting
Ensureexpress-session is configured before passport.initialize():
Next Steps
Congratulations! You’ve successfully added Google OAuth to your project. Here are some ideas for next steps:Add GitHub OAuth
Add Email OTP
Create New Project
Explore TypeScript
Run any module with TypeScript support
Available Modules
Explore all available Devark modules:devark add google-oauth- Google OAuth 2.0devark add github-oauth- GitHub OAuthdevark add resend-otp- Email OTP via Resenddevark add node-mongo- Node.js + MongoDB templatedevark add node-postgres- Node.js + PostgreSQL templatedevark add oauth- Interactive OAuth provider selector

