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

  • Create a TikTok OAuth application in TikTok Developer Portal
  • Configure your Supabase project with TikTok OAuth credentials (custom provider - requires edge function)
  • Set up your Despia project with a deeplink scheme
Important: This works with Lovable + Supabase projects but requires a custom edge function since TikTok is not natively supported by Supabase

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
  • TikTok OAuth redirects back to a URL, but the WebView doesn’t know what to do

The Solution

For native apps, we:
  • Open OAuth in ASWebAuthenticationSession (iOS) or Chrome Custom Tabs (Android)
  • After login, exchange the authorization code for tokens via edge function
  • Redirect to a deeplink to close the session and return to app
  • 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: Generate TikTok OAuth URL For TikTok, we generate the OAuth URL on the frontend. The client key is public (visible in the OAuth URL anyway). We 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 OAuth URL, 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 TikTok login inside ASWebAuthenticationSession / Chrome Custom Tab
  • After login, TikTok redirects to your /native-callback page with an authorization code
  • This page is still inside the ASWebAuthenticationSession / Chrome Custom Tab
Step 5: Exchange code for tokens via edge function TikTok uses authorization code flow (not implicit like Google), so we need an edge function to exchange the code for Supabase tokens. The URL looks like /native-callback?code=xxx&state=yyy. Your callback page calls the edge function:
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
  • TikTok uses authorization code flow, requiring an edge function to exchange the code

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. Need Help? For additional support or questions, please contact our support team at support@despia.com