Skip to main content

prepareSessionTransfer()

The prepareSessionTransfer() function creates a one-time link that signs the native app's user in on a page of your web app. Each call creates a new link.

Use it only when you open the link yourself. For a WebView, use useAuthenticatedWebView()Expo Icon instead. For the default browser, use openInBrowser()Expo Icon.

Warning

The link signs in anyone who opens it. Open it once, within 60 seconds, and never log or store it.

For setup steps, see Open your web app signed in from a native app.

Import the function from the @clerk/expo/native-to-web subpath:

import { prepareSessionTransfer } from '@clerk/expo/native-to-web'

Usage

prepareSessionTransfer() has the following function signature:

function prepareSessionTransfer(params: PrepareSessionTransferParams): Promise<SessionTransfer>

Parameters

prepareSessionTransfer() accepts the following parameters (PrepareSessionTransferParams):

  • Name
    destination
    Type
    string
    Description

    The page to open, as an absolute URL. Outside development instances, it must use HTTPS. In production, it must share a root domain with one of your Clerk instance's domains.

  • Name
    presentation
    Type
    'webview' | 'browser'
    Description

    Where you'll open the link. Use 'webview' for a WebView that your app renders, and 'browser' for the system browser or an in-app browser tab.

Returns

prepareSessionTransfer() returns a Promise that resolves to an object with the following properties (SessionTransfer):

  • Name
    expiresAt
    Type
    Date
    Description

    When the link stops working.

  • Name
    url
    Type
    string
    Description

    The one-time sign-in link. It isn't enumerable, so JSON.stringify and object spread leave it out. Debuggers can still show it, so don't log the object.

The Promise rejects with a SessionTransferErrorExpo Icon when Clerk can't create the link.

On Android, Clerk only accepts the link when the intent that opens it names your app as the referrer. Set android.intent.extra.REFERRER_NAME to android-app://<your-package-name> on that intent. Otherwise the sign-in fails with the cross_site_request error code.

expo-web-browser Custom Tabs can't set the referrer, so they can't open the link. Use openInBrowser()Expo Icon, which sets it for you.

Warning

On Android, the sign-in link opens in your app instead of the browser if your app has a verified App Link that matches it. The link's path is /v1/session_transfers/redeem on your . The intent filter in the passkeys setupExpo Icon matches every path on that host, so it matches the link, and the user isn't signed in on the web.

To fix this, narrow the intent filter with pathPrefix or pathPattern to the paths that your app handles, or remove it if your app doesn't open links on that host. Keep your Android app registered on the Native applications page, which serves the assetlinks.json file.

Example

The following example opens the reports page of a web app in an in-app browser on iOS.

import { prepareSessionTransfer } from '@clerk/expo/native-to-web'
import * as WebBrowser from 'expo-web-browser'

const transfer = await prepareSessionTransfer({
  destination: 'https://app.example.com/reports',
  presentation: 'browser',
})
await WebBrowser.openBrowserAsync(transfer.url)

The in-app browser keeps its cookies separate from Safari, so the user is signed in only inside the in-app browser.

Feedback

What did you think of this content?

Last updated on