The Ecommerce API integrates Google OAuth 2.0 via Passport.js, allowing users to sign in with their existing Google account instead of creating a separate password. The API automatically creates a new user document the first time a Google account is seen, assigns the standardDocumentation Index
Fetch the complete documentation index at: https://mintlify.com/fredy-rizo/Ecommerce/llms.txt
Use this file to discover all available pages before exploring further.
Usuario role, and issues a 365-day JWT — the same token format used by the native JWT flow.
Prerequisites
Before the OAuth flow will work, three environment variables must be set on the API server:| Variable | Description |
|---|---|
CLIENT_ID | The OAuth 2.0 client ID from your Google Cloud Console project |
CLIENT_SECRET | The client secret paired with the client ID |
CLIENT_URL | The callback URL registered in Google Cloud Console (e.g. https://your-api-domain.com/auth/google/callback) |
Setting up Google OAuth credentials
Create or select a Google Cloud project
Go to the Google Cloud Console and either create a new project or select an existing one from the project picker in the top navigation bar.
Enable the Google People API
Navigate to APIs & Services → Library and search for Google People API. Click Enable to allow your project to request profile and email scopes.
Configure the OAuth consent screen
Go to APIs & Services → OAuth consent screen. Choose External if your app is for general users.Fill in the required fields:
- App name — shown to users on the consent screen
- User support email — a contact email for OAuth-related queries
- Authorized domains — add your API’s root domain (e.g.
your-api-domain.com) - Developer contact information
email and profile scopes, which correspond to the scopes requested by the Passport.js strategy.Create OAuth 2.0 credentials
Go to APIs & Services → Credentials and click Create Credentials → OAuth client ID.
- Application type: Web application
- Name: A descriptive label (e.g.
Ecommerce API OAuth Client) - Authorized JavaScript origins: Your API’s origin (e.g.
https://your-api-domain.com) - Authorized redirect URIs: The exact value you will use for
CLIENT_URL(e.g.https://your-api-domain.com/auth/google/callback)
CLIENT_ID and CLIENT_SECRET in your API’s environment.OAuth authentication flow
Once credentials are configured, the full sign-in flow works as follows:User navigates to GET /auth/google
The client (typically a login button in the browser) sends the user to:The Passport.js middleware intercepts this request and builds a Google authorization URL with the requested scopes.
The API requests the
email and profile scopes, so Google will ask the user to share their name, profile picture, and email address.User sees the Google consent screen
The browser is redirected to Google’s OAuth consent screen. The user selects their Google account and grants the requested permissions. Google then generates an authorization code and prepares to redirect back to the API.
Google redirects to GET /auth/google/callback
Google calls back to the URL configured in This route is handled entirely by the API — the client does not need to send any additional data.
CLIENT_URL (e.g. /auth/google/callback), appending an authorization code as a query parameter. Passport.js automatically exchanges this code for tokens and fetches the user’s Google profile.API finds or creates the user by googleId
The Passport.js strategy callback receives the Google
profile object and searches the database for an existing user document with a matching googleId.- Existing user: The user document is retrieved, a new 365-day JWT is signed and saved to the User document, and the user is passed to the session.
- New user: A new user document is created using the profile data (
googleId,userNamefromprofile.displayName, andemailfromprofile.emails[0].value).
The strategy callback in
src/middleware/utils/passport.js uses the signature (profile, done) — two parameters. The profile object is the Google profile returned after Passport has already exchanged the authorization code internally.Because Google verifies the user’s email as part of the OAuth flow, new accounts created through Google sign-in are immediately active — no email verification step is required.
New Google users are assigned default role and active status
All users created through the Google OAuth flow are automatically assigned:
This mirrors the state of a native user who has completed email verification. Admin roles must be assigned manually in the database after account creation.
| Field | Value |
|---|---|
roles | [{ name: "Usuario", value: "1" }] |
activate | [{ name: "Activado", value: "1" }] |
A 365-day JWT is issued and stored
The API signs a JWT using
config.SECRET with a 365d expiry. The token is stored on the User document.Using the token after OAuth sign-in
After a successful Google OAuth sign-in, the resulting JWT is used exactly like a token obtained fromPOST /api/user/login. Attach it to the token-access header on every call to a protected endpoint:
cURL
Passport.js strategy configuration reference
The Google strategy is configured insrc/middleware/utils/passport.js using the following environment variables:
CLIENT_ID
The OAuth 2.0 client ID from Google Cloud Console. Identifies your application to Google.
CLIENT_SECRET
The client secret paired with
CLIENT_ID. Keep this value private and never expose it in client-side code.CLIENT_URL
The authorized redirect URI. Must exactly match the value registered in Google Cloud Console under your OAuth 2.0 credentials.