Skip to main content

OAuth Guide

Cocobase simplifies third-party authentication by handling the complex OAuth exchange and automatically creating or linking user accounts.

Supported Providers

  • Google: Recommended for all platforms.
  • GitHub: Best for developer-focused applications.
  • Apple: Required for iOS apps with third-party login.

Configuration

Before implementing OAuth, you must configure your providers in the Cocobase Dashboard:
  1. Go to Settings > Authentication > OAuth.
  2. Enable your desired providers.
  3. Enter your Client ID and Client Secret (obtained from the provider’s developer console).
  4. Add the Cocobase redirect URI to your provider’s allowed list: https://api.cocobase.cc/auth/oauth/{provider}/callback

OAuth Flow

Cocobase uses a two-step flow to handle OAuth.

1. Get Authorization URL

Direct your user to the provider’s login page.
  • Endpoint: GET /auth-collections/oauth/{provider}?redirect_uri={your_callback_url}

2. Verify and Login

Once the user completes the login, the provider redirects them back to your redirect_uri with an authorization code. Send this code to Cocobase to complete the login.
  • Endpoint: POST /auth-collections/verify-{provider}-login
  • Body:

Implementation Examples

Google OAuth (Web)

Apple OAuth (iOS)

When using Apple Sign In on iOS, you typically use the native SDK to get an identityToken and then send it to Cocobase.

Security Features

  1. State Parameter: Cocobase can automatically handle the state parameter to prevent CSRF attacks.
  2. PKCE Support: Recommended for mobile and single-page applications.
  3. Secure Callbacks: Only allow redirects to domains whitelisted in your dashboard.
  4. Account Linking: If a user logs in with an email that already exists via a different provider, Cocobase can automatically link the accounts (configurable in settings).

Troubleshooting

  • Redirect URI Mismatch: Ensure the URI in your dashboard, provider console, and API request are IDENTICAL.
  • Scope Issues: Check if you’ve requested the correct scopes (e.g., email, profile) in the provider console.
  • Token Expiry: Auth codes from providers usually expire within minutes. Process them immediately.