Bulk Messaging System

Documentation

Gmail OAuth 2 configuration

Introduction#

This page provides detailed configuration guidance for Gmail OAuth2 authentication in the application. It covers environment variables, OAuth2 flow, scopes, redirect URIs, token exchange, authentication window configuration, and step-by-step setup instructions for Google Cloud Console. It also includes troubleshooting guidance for common OAuth2 errors and best practices for credential storage.

Project structure#

The Gmail OAuth2 integration spans the Electron main process, preload bridge, and React UI components. The main process handles OAuth2 flow and token persistence, while the renderer communicates via IPC.

Core components#

  • Environment variables:
    • GOOGLE_CLIENT_ID
    • GOOGLE_CLIENT_SECRET
  • OAuth2 configuration:
    • Scopes: Gmail send only
    • Redirect URI: localhost callback
    • Access type: offline (refresh token)
  • Token storage:
    • electron-store persists tokens locally
  • Authentication window:
    • Size: 800x800 pixels
    • Security: context isolation, no node integration
    • Timeout: 5 minutes

Architecture overview#

The OAuth2 flow is handled in the Electron main process. The renderer triggers authentication via IPC, the main process opens an embedded BrowserWindow, generates the authorization URL, captures the redirect, exchanges the authorization code for tokens, and stores them securely.

Detailed component analysis#

Environment variables and security#

  • Required variables:
    • GOOGLE_CLIENT_ID
    • GOOGLE_CLIENT_SECRET
  • Storage recommendations:
    • Use a.env file in the electron directory
    • Do not commit secrets to version control
    • Restrict file permissions to owner-only
    • Consider platform keychain integration for production builds

Security implications:

  • Exposing client credentials allows unauthorized API access
  • Tokens grant full Gmail send privileges
  • Store tokens securely and rotate credentials periodically

OAuth2 flow and authorization URL generation#

  • Scope configuration:
    • Single scope: Gmail send
  • Access type:
    • offline to receive refresh tokens
  • Consent prompt:
    • prompt set to force consent screen for refresh token
  • Redirect URI:

Redirect URI configuration#

  • The application expects a redirect to http://localhost:3000/oauth/callback
  • Ensure this redirect URI is configured in the Google OAuth2 client configuration
  • The embedded BrowserWindow listens for this exact URL to capture the authorization code

Token exchange process#

  • After redirect, the handler extracts the authorization code from the URL
  • It exchanges the code for tokens using the OAuth2 client
  • On success, sets credentials on the OAuth2 client and persists the token
  • On failure, returns an error response

Scope configuration for gmail API access#

  • Current scope: Gmail send only
  • Implication: Application can only send emails; no read or manage permissions
  • If broader access is needed, adjust the scope accordingly

Offline access type and refresh tokens#

  • access_type set to offline ensures a refresh token is issued
  • The consent prompt forces explicit user consent for offline access
  • The application stores the token for future use without re-prompting

Authentication window configuration#

  • Size: 800x800 pixels
  • Security:
    • contextIsolation enabled
    • nodeIntegration disabled
    • show initially hidden, shown on ready-to-show
  • Timeout: 5 minutes; closes window if not redirected within this period

Step-by-Step setup instructions#

Google cloud console setup#

  1. Navigate to Google Cloud Console.
  2. Create or select a project.
  3. Enable the Gmail API for the project.
  4. Go to Credentials and create OAuth 2.0 Client IDs.
  5. Configure the OAuth consent screen.
  6. Create desktop application credentials.
  7. Download the credentials JSON file.

OAuth2 client creation#

Credential configuration#

  • Place the downloaded credentials in the electron directory
  • Create a.env file with:
    • GOOGLE_CLIENT_ID=your_client_id_here
    • GOOGLE_CLIENT_SECRET=your_client_secret_here

Running the application#

  • Install dependencies in the electron directory
  • Start the development server
  • Use the Gmail tab to authenticate and send emails

Dependency analysis#

External dependencies involved in Gmail OAuth2:

  • googleapis: Provides OAuth2 client and Gmail API access
  • electron-store: Persists tokens locally
  • dotenv: Loads environment variables from.env

Performance considerations#

  • Token reuse: The application reuses stored tokens to avoid repeated authentication prompts
  • Rate limiting: The UI allows configuring delay between emails to avoid throttling
  • Window lifecycle: Authentication window is closed after successful token exchange or timeout

[No sources needed since this section provides general guidance]

Troubleshooting guide#

Common OAuth2 errors and resolutions:

  • Missing environment variables:
    • Ensure GOOGLE_CLIENT_ID and GOOGLE_CLIENT_SECRET are present in.env
  • Redirect URI mismatch:
  • Token exchange failures:
    • Verify client credentials and network connectivity
    • Check for invalid_grant or expired token scenarios
  • Authentication timeout:
    • Increase timeout if needed or ensure the redirect occurs promptly
  • Window closed prematurely:
    • Ensure the BrowserWindow remains open until redirect completes

Credential storage best practices:

  • Store tokens securely using electron-store
  • Avoid exposing tokens in logs or UI
  • Rotate client credentials periodically
  • Use separate OAuth2 clients for development and production

Conclusion#

The application implements a secure, offline-capable Gmail OAuth2 flow with a dedicated authentication window and reliable token persistence. By following the setup instructions and best practices outlined here, you can configure Gmail API access safely and reliably.

[No sources needed since this section summarizes without analyzing specific files]

Appendices#

Appendix A: environment variable reference#

  • GOOGLE_CLIENT_ID: OAuth2 client identifier
  • GOOGLE_CLIENT_SECRET: OAuth2 client secret

Storage recommendations:

  • Use.env file in electron directory
  • Restrict file permissions
  • Do not commit to version control

Appendix B: build and distribution notes#

  • The application uses electron-builder for cross-platform builds
  • Ensure environment variables are available at runtime for distribution builds