Skip to main content

useAuthenticatedWebView()

The useAuthenticatedWebView() hook shows a page of your web app in a react-native-webview <WebView> with the native app's user already signed in. It creates a one-time sign-in link for the page when it mounts, and returns props to spread onto <WebView>. Call it in the screen that shows the WebView.

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

Installation

This hook requires react-native-webview:

terminal
npx expo install react-native-webview

Then, import the hook from the @clerk/expo/native-to-web subpath:

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

Parameters

useAuthenticatedWebView() accepts the following parameters (UseAuthenticatedWebViewParams):

  • 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. Changing it creates a new link.

  • Name
    onError?
    Type
    WebViewProps['onError']
    Description

    Called when a page fails to load.

  • Name
    onHttpError?
    Type
    WebViewProps['onHttpError']
    Description

    Called when a page responds with an HTTP error status.

  • Name
    onLoadEnd?
    Type
    WebViewProps['onLoadEnd']
    Description

    Called when the WebView finishes loading a page, whether it succeeded or failed.

  • Name
    onLoadStart?
    Type
    WebViewProps['onLoadStart']
    Description

    Called when the WebView starts loading a page.

  • Name
    onShouldStartLoadWithRequest?
    Type
    WebViewProps['onShouldStartLoadWithRequest']
    Description

    Called before the WebView navigates. Return false to block the navigation. Clerk's sign-in page always loads.

The WebView callbacks use the types from react-native-webview. They receive events for every page except Clerk's sign-in page, which carries the one-time link.

Warning

Pass your WebView callbacks to the hook, not to <WebView>. Setting onLoadStart, onLoadEnd, onHttpError, onError, or onShouldStartLoadWithRequest on <WebView> after the spread replaces Clerk's handlers, and the hook can't tell when the user is signed in.

Returns

useAuthenticatedWebView() returns an object with the following properties (AuthenticatedWebView):

  • Name
    error
    Type
    SessionTransferErrorExpo Icon | null
    Description

    The reason for the failure, set while status is 'error'.

  • Name
    retry()
    Type
    () => void
    Description

    Creates a new sign-in link. Call it from your error view.

  • Name
    status
    Type
    'preparing' | 'loading' | 'ready' | 'signed-out' | 'error'
    Description

    The state of the WebView.

    • 'preparing': Clerk is creating the sign-in link. There's nothing to render yet.
    • 'loading': webViewProps is set and the WebView is on Clerk's sign-in page. Render the WebView under a loading overlay, because the page must load to finish signing in.
    • 'ready': The WebView left Clerk's sign-in page and loaded the next page. It stays 'ready' after later navigations, so it doesn't tell you whether the web page is still signed in.
    • 'signed-out': The app has no active session.
    • 'error': Creating or loading the link failed, or the sign-in on the web failed. Show your own error view in place of the WebView, because the page isn't signed in as the app's user.
  • Name
    webViewProps
    Type
    AuthenticatedWebViewProps | null
    Description

    The props to spread onto <WebView>, set while status is 'loading' or 'ready'. They include the WebView's source and its load callbacks. The source carries the one-time link, so never log or store these props.

Examples

Show a web page signed in

The following example shows the account page of a web app. It renders the WebView as soon as webViewProps is set, and covers it with a spinner 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>
  )
}

The following example keeps pages of the web app in the WebView, and opens links to other sites in the browser.

import { useAuthenticatedWebView } from '@clerk/expo/native-to-web'
import { Linking } from 'react-native'

const web = useAuthenticatedWebView({
  destination: 'https://app.example.com/account',
  onShouldStartLoadWithRequest: ({ isTopFrame, url }) => {
    if (isTopFrame === false || new URL(url).origin === 'https://app.example.com') return true
    void Linking.openURL(url)
    return false
  },
})

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.

Feedback

What did you think of this content?

Last updated on