# useAuthenticatedWebView()

The `useAuthenticatedWebView()` hook shows a page of your web app in a [`react-native-webview`](https://github.com/react-native-webview/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](https://clerk.com/docs/guides/development/native-to-web.md).

## Installation

This hook requires `react-native-webview`:

filename: terminal
```bash
npx expo install react-native-webview
```

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

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

## Parameters

`useAuthenticatedWebView()` accepts the following parameters (`UseAuthenticatedWebViewParams`):

| Name                          | Type                                          | Description                                                                                                                                                                                                   |
| ----------------------------- | --------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| destination                   | string                                        | 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. |
| onError?                      | WebViewProps['onError']                      | Called when a page fails to load.                                                                                                                                                                             |
| onHttpError?                  | WebViewProps['onHttpError']                  | Called when a page responds with an HTTP error status.                                                                                                                                                        |
| onLoadEnd?                    | WebViewProps['onLoadEnd']                    | Called when the WebView finishes loading a page, whether it succeeded or failed.                                                                                                                              |
| onLoadStart?                  | WebViewProps['onLoadStart']                  | Called when the WebView starts loading a page.                                                                                                                                                                |
| onShouldStartLoadWithRequest? | WebViewProps['onShouldStartLoadWithRequest'] | 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.

> 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                                                                            | Type                                                                                                                                                                    | Description                                                                                                                                                                                                       |
| ------------------------------------------------------------------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| error                                                                           | SessionTransferError | null                                                                                                                                            | The reason for the failure, set while status is 'error'.                                                                                                                                                          |
| retry()                                                                         | () => void                                                                                                                                                              | Creates a new sign-in link. Call it from your error view.                                                                                                                                                         |
| '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. |                                                                                                                                                                                                                   |
| webViewProps                                                                    | AuthenticatedWebViewProps | null                                                                                                                                       | 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'`.

filename: app/(home)/account.tsx
```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>
  )
}
```

### Open links to other sites in the browser

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

```tsx
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](https://clerk.com/docs/guides/development/native-to-web.md#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`](https://github.com/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.

---

## Sitemap

[Overview of all docs pages](https://clerk.com/docs/llms.txt)
