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:
- useAuthenticatedWebView() shows the page in a
react-native-webview<WebView>inside your app. Use it for web pages that are part of your app's screens, such as an account page. - openInBrowser() opens the page in the device's default browser. Use it to send users to your full web app.
- prepareSessionTransfer() creates the link and leaves opening it to you. Use it only when neither of the other two fits.
Before you start
- In the Clerk Dashboard, navigate to the Native applications page.
- Enable the Native API, if it isn't already.
- Turn on Native to Web SSO. Until you do, Clerk can't create sign-in links and returns the
feature_not_enablederror code.
Then check the following requirements:
- Your native app uses
@clerk/expo4.17.0 or later. - Your web app uses Clerk with the same instance as your native app.
- The
destinationyou 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:
npx expo install react-native-webviewCall 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'.
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
incognitowithsharedCookiesEnabled, 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.
npx expo install expo-intent-launcher expo-applicationCall openInBrowser() with the page to open:
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_statusissession_transfer_failed.__clerk_error_codeis the reason, such asexpiredorother_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 codes. 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.
Keep the link secret
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 inwebViewProps, 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() 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
Last updated on