Google OAuth (Passport)
The Google OAuth component provides a secure and standardized way to integrate Google authentication into your Servercn Express applications using the official passport, passport-google-oauth20.
It handles the complete OAuth 2.0 flow including authorization URL generation, token exchange, user information retrieval, and token refresh.
Features
- Complete OAuth 2.0 flow - Authorization, token exchange, and user info retrieval
- Secure by default - CSRF protection with state parameter
- Token management - Access token, refresh token, and ID token verification
- Express integration - Ready-to-use route handlers
- Type-safe - Full TypeScript support
- Flexible scopes - Customizable OAuth scopes
Installation Guide
This component requires additional Servercn components.
๐ You do not need to install any Servercn components manually. Running this component installer will automatically install all required components. Manual installation is optional and only recommended if you prefer fine-grained components control
1. Install Servercn components(Optional)
2. Install this component
npx servercn-cli add oauthYou will be prompted to select a file upload provider:
The CLI will then automatically configure the component based on your selected provider.
Prerequisites
Google Cloud Console Setup
- Go to the Google Cloud Console
- Create a new project or select an existing one
- Enable the Google+ API (or Google Identity API)
- Go to Credentials โ Create Credentials โ OAuth client ID
- Configure the OAuth consent screen:
- Choose External (for testing) or Internal (for Google Workspace)
- Fill in the required information
- Create OAuth 2.0 Client ID:
- Application type: Web application
- Authorized JavaScript origins: Add your origin URL (e.g.,
http://localhost:9000) - Authorized redirect URIs: Add your callback URL (e.g.,
http://localhost:9000/api/auth/google/callback)
- Copy the Client ID and Client Secret
Environment Variables
Add the following to your .env file:
Ensure the following configuration are defined:
MVC: src/configs/env.ts
Feature: src/shared/configs/env.ts
Basic Implementation
1. MVC Structure
1. Configure passport in src/configs/passport.ts:
2. Create a Google OAuth controller in src/controllers/google-oauth.controller.ts or src/controllers/auth.controller.ts
3. Create a Google OAuth router in src/routes/google-oauth.routes.ts or src/routes/auth.routes.ts
4. Create a server in src/app.ts
2. Feature Structure
1. Configure passport in src/shared/configs/passport.ts:
2. Create a Google OAuth controller in src/modules/oauth/google-oauth.controller.ts or src/modules/auth/auth.controller.ts
3. Create a Google OAuth router in src/modules/oauth/google-oauth.routes.ts or src/modules/auth/auth.routes.ts
4. Create a index route in src/routes/index.ts
5. Create a server in src/app.ts
Success Response
This response is formated by ApiResponse component.
Common Issues
"redirect_uri_mismatch"
Ensure your redirect URI in .env exactly matches the one configured in Google Cloud Console.
"invalid_grant"
This usually means:
- The authorization code has expired (codes expire after a few minutes)
- The code has already been used
- The redirect URI doesn't match
"access_denied"
The user denied permission. Handle this gracefully in your UI.