Skip to main content

Lovable Prompt

This setup is more technical and you’ll need to paste the following prompt, which includes full scaffolding, into Lovable to get good results:

Setup Requirements

  1. Create a Google OAuth application in Google Cloud Console
  2. Configure your Supabase project with Google OAuth credentials
  3. Set up your Despia project with a deeplink scheme
  4. Important: This works with Lovable + Supabase projects out of the box

Understanding the Complete OAuth Flow

The Problem

When you wrap a Lovable web app as a native iOS/Android app using Despia:
  • The app runs in a WebView (embedded browser)
  • WebViews cannot handle OAuth redirects properly
  • Google OAuth redirects back to a URL, but the WebView doesn’t know what to do

The Solution

For native apps, we:
  1. Open OAuth in ASWebAuthenticationSession (iOS) or Chrome Custom Tabs (Android)
  2. After login, redirect to a deeplink to close the session and return to app
  3. The native app intercepts this deeplink, navigates to the correct page, and sets the session

How Native OAuth Works

Step 1: Detect if running in native app When the user clicks “Sign in”, your app first checks if it’s running inside a Despia native app. Despia automatically adds despia to the userAgent string:
Step 2: Get OAuth URL from edge function For native apps, you need an edge function to generate the OAuth URL. This is necessary because we need to set a special redirect URL that points to /native-callback instead of /auth:
Step 3: Open OAuth in secure browser session Once you have the URL from the edge function, open it using despia('oauth://?url=...'). The oauth:// prefix tells Despia to open the URL in a secure native browser session:
This opens the URL in:
  • iOS: ASWebAuthenticationSession (secure Safari sheet)
  • Android: Chrome Custom Tabs (secure Chrome overlay)
Step 4: User completes OAuth in the browser session
  • User sees Google login inside ASWebAuthenticationSession / Chrome Custom Tab
  • After login, Google redirects to your /native-callback page
  • This page is still inside the ASWebAuthenticationSession / Chrome Custom Tab
Step 5: Parse tokens from URL Supabase uses the implicit OAuth flow, which returns tokens in the URL hash (the part after #). The URL looks like /native-callback?deeplink_scheme=myapp#access_token=xxx&refresh_token=yyy. Your callback page parses the tokens:
Step 6: Close browser session and return to app via deeplink This is the crucial step. You’re still inside the ASWebAuthenticationSession or Chrome Custom Tab and need to close it. Despia listens for deeplinks that contain the oauth/ prefix:
Deeplink format: {scheme}://oauth/{path}?params
  • myapp:// - Your app’s deeplink scheme
  • oauth/ - Required prefix - tells native code to close the browser session
  • {path} - Where to navigate in your app (e.g., auth, home, profile)
  • ?params - Query params passed to that page
Examples:
  • myapp://oauth/auth?access_token=xxx - Closes browser, opens /auth?access_token=xxx
  • myapp://oauth/home - Closes browser, opens /home
  • myapp://oauth/profile?tab=settings - Closes browser, opens /profile?tab=settings
Step 7: App receives deeplink, navigates, sets session
  • Native app intercepts myapp://oauth/… deeplink
  • Closes ASWebAuthenticationSession / Chrome Custom Tab
  • Navigates WebView to /path?params (e.g., /auth?access_token=xxx)
  • Your Auth.tsx parses tokens and sets the session:

How The Two Flows Work

Web Browser Flow Native App Flow (Despia WebView) Key points:
  • oauth:// prefix in despia() call opens ASWebAuthenticationSession / Chrome Custom Tab
  • myapp://oauth/ prefix in deeplink closes the browser session
  • The path after oauth/ (e.g., /auth) is where the WebView navigates
  • Session is set in the WebView (not the browser session) so cookies persist

Installation

Install the Despia package from NPM:

Usage

Import the SDK:

Opening the Native OAuth Session:

Exiting the Native Browser Session:

The native authentication tab (ASWebAuthenticationSession / Chrome Custom Tab) will automatically close when you redirect to a deeplink with the oauth/ prefix. In your NativeCallback page, call this to exit the browser and return to your app:
The oauth/ prefix is required - it tells Despia to close the browser session. Without it, the user will be stuck in the native browser tab.

Resources

  • NPM Package
  • View full NPM documentation for additional configuration options

Integration

This SDK is optimized for prompt-based AI builders, enabling quick integration of native OAuth into your generated apps via the master prompt above. For additional support or questions, please contact our support team at support@despia.com