Eco-It supports Google OAuth 2.0 as a first-class authentication method, letting users sign in with their existing Google account without creating a separate password. The integration is built with Passport.js and theDocumentation Index
Fetch the complete documentation index at: https://mintlify.com/vanegasjoseignacio2-cyber/Eco-It/llms.txt
Use this file to discover all available pages before exploring further.
passport-google-oauth20 strategy. On successful authentication, the backend mints a JWT and redirects the browser to the frontend with the token embedded in the URL query string, where it is immediately extracted and moved to localStorage.
How it works
The complete Google OAuth flow involves three parties: the Eco-It frontend, the Eco-It backend, and Google’s OAuth servers.Redirect the user to Google
The frontend redirects the browser to the backend’s Google auth endpoint. Passport intercepts this request and builds the Google authorization URL, requesting access to the user’s Passport redirects the browser to:
profile and email scopes, then redirects the browser to Google.Endpoint: GET /api/auth/googleGoogle redirects to the callback
After the user grants consent, Google redirects the browser to the registered callback URL with an authorization code. Passport exchanges the code for an access token, fetches the user’s Google profile, and runs the strategy’s verify callback.Endpoint:
GET /api/auth/google/callbackInside the verify callback, three outcomes are possible:| Scenario | Action |
|---|---|
User exists by googleId | Authenticate the existing user. |
User exists by email but no googleId | Link the Google account to the existing user and authenticate. |
New user (no matching googleId or email) | Create a new User document with perfilCompleto: false, send a welcome email, and authenticate. |
New Google users are created with
authProvider: 'google', without a password, telefono, or edad. The apellido field is set from profile.name.familyName when Google provides it. perfilCompleto is always false for brand-new Google registrations until the user completes their profile.Backend mints a JWT and redirects to the frontend
Once Passport has resolved the user, the callback handler signs a JWT containing Redirect target:If authentication fails (e.g. the user denies consent), Passport redirects to:
id, rol, and perfilCompleto, then constructs an intermediate HTML page that immediately sets window.location.href to the frontend success URL with the token as a query parameter.JWT payload:Profile completion after Google sign-in
WhenperfilCompleto is false in the JWT payload, the AuthContext.login() function is called with /completar-perfil as the redirectOverride. This sends the user to a form where they fill in their apellido, edad, and telefono before accessing the full platform.
The profile completion endpoint requires an authenticated request:
Passport.js configuration
The Google strategy is initialised inbackend/controllers/AutheticationGoogle.js via the exported setupGoogleAuth() function, which must be called after dotenv.config() has loaded environment variables. The callbackURL is constructed from GOOGLE_CALLBACK_URL if set, otherwise it falls back to BACK_URL/api/auth/google/callback.
Required environment variables
The OAuth 2.0 Client ID from your Google Cloud Console project. Note that the code reads this as
CLIENT_ID, not GOOGLE_CLIENT_ID.The OAuth 2.0 Client Secret from your Google Cloud Console project. Read as
CLIENT_SECRET in the strategy config.Full callback URL registered in Google Cloud Console. If omitted, the backend constructs it from
BACK_URL. Must exactly match the URI registered in the Google Cloud Console.Base URL of the backend (e.g.
https://api.eco-it.co). Used as a fallback for building the callback URL when GOOGLE_CALLBACK_URL is not set.Base URL of the React frontend (e.g.
https://eco-it.co). The callback handler redirects to FRONT_URL/auth/google/success?token=... on success and to FRONT_URL/login on failure.Google Cloud Console setup
Before the OAuth flow can work, you must register the callback URL as an Authorised redirect URI in the Google Cloud Console.Open your OAuth credentials
In the Google Cloud Console, navigate to APIs & Services → Credentials and open your OAuth 2.0 Client ID.