Kotlin: Initializing

Create Supabase Client

Independently of which Supabase module you are using, you will need to initialize the main client first and install the module.

To create a new client, you can use the createSupabaseClient function.

When installing a module, you can pass a block to configure it.

OAuth and OTP link verification

supabase-kt provides several platform implementations for OAuth and OTP link verification.

On Desktop platforms (JVM, MacOS*, Linux, Windows), it uses a HTTP Callback Server to receive the session data from a successful OAuth login. The success page can be customized via AuthConfig#httpCallbackConfig
* If no deeplinks are being used.

Note: OTP link verification such as sign ups are not supported on JVM. You may have to send a verification token rather than a url in your email. To send the token, rather than a redirect url, change \{\{ .ConfirmationURL \}\} in your sign up email to \{\{ .Token \}\}

On Android, iOS & MacOS, OAuth and OTP verification use deeplinks. Refer to the guide below on how to setup deeplinks. Alternatively you can use Native Google Auth.
On JS, it uses the website origin as the callback url. Session importing gets handled automatically.
tvOS & watchOS currently have no default implementation. Feel free to create a PR.

You always make your own implementation and use auth.parseSessionFromFragment(fragment) or auth.parseSessionFromUrl(url) to let supabase-kt handle the parsing after receiving a callback. Then you can simply use auth.importSession(session).

Configure deeplink callbacks for Authentication

Deeplinks are supported on Android, iOS and MacOS.

  1. Set up a deeplink
    On Android, set up a deeplink in your Android manifest.
    On iOS and MacOS, set up a url scheme.
  2. Add your deeplink to the redirect URLs
    Pattern: scheme://host
  3. Configure the Auth plugin Set the host and scheme in the Auth config:
    install(Auth) \{
       host = "deeplink host" // this can be anything, eg. your package name or app/company url (not your Supabase url)
       scheme = "deeplink scheme"
    
       // On Android only, you can set OAuth and SSO logins to open in a custom tab, rather than an external browser:
       defaultExternalAuthAction = ExternalAuthAction.CustomTabs() //defaults to ExternalAuthAction.ExternalBrowser
    \}
    
  4. Call platform specific function on startup
    On Android: supabase.handleDeeplinks(intent)
    If you don't want a separate activity, just call this function at the top of your onCreate function in your MainActivity.
    On iOS/MacOS: supabase.handleDeeplinks(url)

Then you can log in using OAuth:

supabase.auth.signInWith(Google)

Or open OTP links directly in your app.

PKCE Authentication flow

supabase-kt supports the PKCE authentication flow. To use it, change the flowType in the Auth configuration:

install(Auth) \{
  flowType = FlowType.PKCE
\}

That's it! If you already implemented deeplinks to handle OTPs and OAuth you don't have to change anything!

Parameters

Examples

Initialize Client

val supabase = createSupabaseClient(
    supabaseUrl = "https://xyzcompany.supabase.co",
    supabaseKey = "public-anon-key"
) \{
    install(Auth)
    install(Postgrest)
    //install other modules
\}

Configure Auth module

val supabase = createSupabaseClient(
    supabaseUrl = "https://xyzcompany.supabase.co",
    supabaseKey = "public-anon-key"
) \{
    install(Auth) \{
        alwaysAutoRefresh = false // default: true
        autoLoadFromStorage = false // default: true
        //and more...
    \}
\}

Configure PostgREST module

val supabase = createSupabaseClient(
    supabaseUrl = "https://xyzcompany.supabase.co",
    supabaseKey = "public-anon-key"
) \{
    install(Postgrest) \{
        defaultSchema = "schema" // default: "public"
        propertyConversionMethod = PropertyConversionMethod.SERIAL_NAME // default: PropertyConversionMethod.CAMEL_CASE_TO_SNAKE_CASE
    \}
\}

Configure Storage module

val supabase = createSupabaseClient(
    supabaseUrl = "https://xyzcompany.supabase.co",
    supabaseKey = "public-anon-key"
) \{
    install(Storage) \{
        transferTimeout = 90.seconds // Default: 120 seconds
    \}
\}

Configure Realtime module

val supabase = createSupabaseClient(
    supabaseUrl = "https://xyzcompany.supabase.co",
    supabaseKey = "public-anon-key"
) \{
    install(Realtime) \{
        reconnectDelay = 5.seconds // Default: 7 seconds
    \}
\}

Configure Functions plugin

val supabase = createSupabaseClient(
    supabaseUrl = "https://xyzcompany.supabase.co",
    supabaseKey = "public-anon-key"
) \{
    install(Functions) \{
        //no custom settings
    \}
\}

Configure GraphQL plugin

val supabase = createSupabaseClient(
    supabaseUrl = "https://xyzcompany.supabase.co",
    supabaseKey = "public-anon-key"
) \{
    install(GraphQL) \{
        apolloConfiguration \{
          //custom configuration
        \}
    \}
\}