Skip to main content

Open your web app signed in from a native app

When a signed-in user of your native app opens a page of your web app, the page normally loads signed out, and the user has to sign in again. Native to Web SSO signs them in for you. Clerk creates a one-time sign-in link for the page, and the WebView or browser opens it. The page then loads with the same user signed in.

Choose how to open the page

The Expo SDK exports three functions for this from @clerk/expo/native-to-web:

Before you start

Important

Native to Web SSO is free in development instances. In production, it requires the Pro or Business plan. See pricing.

  1. In the Clerk Dashboard, navigate to the Native applications page.
  2. Enable the Native API, if it isn't already.
  3. Turn on Native to Web SSO. Until you do, Clerk can't create sign-in links and returns the feature_not_enabled error code.

Then check the following requirements:

  • Your native app uses @clerk/expo 4.17.0 or later.
  • Your web app uses Clerk with the same instance as your native app.
  • The destination you open is 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.
  • The user is signed in to your native app. Sessions with pending session tasks can't be transferred.

Show a web page in a WebView

Install react-native-webview:

terminal
npx expo install react-native-webview

Call useAuthenticatedWebView() in the screen that shows the page, and spread its webViewProps onto <WebView>. Render the WebView as soon as webViewProps is set. It loads Clerk's sign-in page before destination, so cover it with a loading overlay until status is 'ready'.

app/(home)/account.tsx
import { useAuthenticatedWebView } from '@clerk/expo/native-to-web'
import { Redirect } from 'expo-router'
import { ActivityIndicator, Button, StyleSheet, View } from 'react-native'
import { WebView } from 'react-native-webview'

export default function AccountScreen() {
  const web = useAuthenticatedWebView({ destination: 'https://app.example.com/account' })

  if (web.status === 'signed-out') return <Redirect href="/sign-in" />
  if (web.status === 'error') return <Button title="Try again" onPress={web.retry} />

  return (
    <View style={{ flex: 1 }}>
      {web.webViewProps && <WebView {...web.webViewProps} style={{ flex: 1 }} />}
      {web.status !== 'ready' && <ActivityIndicator style={StyleSheet.absoluteFill} />}
    </View>
  )
}

Pass your own WebView callbacks, such as onShouldStartLoadWithRequest, to the hook rather than to <WebView>. Setting them on <WebView> replaces Clerk's handlers.

Clear the WebView's cookies when the user changes

The WebView keeps its cookies when the user signs out of your app. Clerk ends the web sessions that the app's links created once the app's session ends, except in the cases listed under Sign-out. When the WebView still holds the previous user's web session, the hook fails with the other_user_signed_in error code.

To start each user with a clean WebView, render it with the incognito prop:

  • On iOS, the WebView keeps its cookies in memory only, and discards them when it unmounts. Don't combine incognito with sharedCookiesEnabled, which copies cookies between the WebView and your app's shared cookie storage.
  • On Android, the WebView clears cookies when it mounts. Android WebViews share one cookie store, so this can sign users out of other WebViews in your app that rely on those cookies. The cookies stay after the WebView unmounts, until the next clear.

If your WebView needs to keep cookies between visits, or the cookies must not stay on an Android device after sign-out, clear them with a cookie manager such as @react-native-cookies/cookies when the user signs out or a different user signs in. On iOS, call clearAll(true) to clear the WebView's cookies, and also clearAll() if you set sharedCookiesEnabled, which copies cookies into your app's shared cookie storage. Both clearAll(true) on iOS and clearAll() on Android clear every cookie that your WebViews share, which can sign users out of other WebViews in your app. Wait for the clear to finish before you show the WebView to the next user, or call retry() after it finishes.

Open a web page in the browser

On Android, install expo-intent-launcher and expo-application. iOS doesn't need any extra packages.

terminal
npx expo install expo-intent-launcher expo-application

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.

Call openInBrowser() with the page to open:

components/open-on-web-button.tsx
import { isSessionTransferError, openInBrowser } from '@clerk/expo/native-to-web'
import { router } from 'expo-router'
import { Alert, Button } from 'react-native'

export function OpenOnWebButton() {
  const onPress = async () => {
    try {
      await openInBrowser({ destination: 'https://app.example.com/settings' })
    } catch (error) {
      if (isSessionTransferError(error) && error.code === 'signed_out') {
        router.replace('/sign-in')
        return
      }
      Alert.alert('Could not open the web app')
    }
  }

  return <Button title="Open on web" onPress={onPress} />
}

Handle a failed sign-in on the web

openInBrowser() resolves after it starts opening the link. It doesn't wait for the web page to sign the user in, and if the sign-in fails, your app isn't notified.

The browser lands on destination, or on the Home URL set in the Clerk Dashboard if the link isn't valid. The page usually loads signed out. For other_user_signed_in and impersonation_in_progress, Clerk leaves the browser's sessions as they were, so the page stays signed in as the other user or in the impersonation session. Either page gets the following query parameters:

  • __clerk_status is session_transfer_failed.
  • __clerk_error_code is the reason, such as expired or other_user_signed_in.

Check for these parameters on your web page to show the user a message or a sign-in prompt. For the possible codes, see the error codesExpo Icon. useAuthenticatedWebView() checks them for you and sets status to 'error'.

Sign-out

When the app's session ends, Clerk also ends the web sessions that its links created. This happens when the user signs out of your app or when you revoke the app's session.

Some web sessions stay signed in:

  • If the browser was already signed in as the same user, the link may keep that existing session. Ending the app's session doesn't end it.
  • When a new sign-in replaces the app's session, such as in single-session mode or with hosted authentication, Clerk doesn't end the web sessions. They still expire when the app's session would have.

The sign-in link signs in anyone who opens it. It works once, within 60 seconds.

  • openInBrowser() never exposes the link.
  • useAuthenticatedWebView() passes the link to the WebView in webViewProps, so don't log or store those props.
  • If you use prepareSessionTransfer(), open the link once and don't log or store it.

Handle errors

Every function reports failures as a SessionTransferError. Use isSessionTransferError()Expo Icon to detect it, and branch on its code. For example, signed_out means the app has no active session, so send the user to sign in.

Feedback

What did you think of this content?

Last updated on