Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 47 additions & 0 deletions .changeset/expo-native-to-web.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
---
'@clerk/expo': minor
---

Introduce the `useAuthenticatedWebView()` hook for showing a page of your web app in a WebView with the native app's user already signed in.

```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() {
// Prepares a one-time sign-in link for this page
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 }}>
{/* Render the WebView as soon as it has props. It loads the link to sign the user in. */}
{web.webViewProps && <WebView {...web.webViewProps} style={{ flex: 1 }} />}
{/* Cover Clerk's sign-in page until the destination has loaded */}
{web.status !== 'ready' && <ActivityIndicator style={StyleSheet.absoluteFill} />}
</View>
);
}
```

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

For more advanced cases, such as opening the link in a WebView or browser you manage yourself, create it with `prepareSessionTransfer()`:

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

const { url } = await prepareSessionTransfer({
destination: 'https://app.example.com/reports',
// 'webview' for a WebView you render, 'browser' for a browser
presentation: 'webview',
});

// Open the link once, within 60 seconds, and never log or store it
```

On Android, a browser only accepts the link when the intent that opens it names your app as the referrer.
4 changes: 4 additions & 0 deletions packages/expo/native-to-web/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"main": "../dist/native-to-web/index.js",
"types": "../dist/native-to-web/index.d.ts"
}
12 changes: 11 additions & 1 deletion packages/expo/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,10 @@
"types": "./dist/hosted-auth/index.d.ts",
"default": "./dist/hosted-auth/index.js"
},
"./native-to-web": {
"types": "./dist/native-to-web/index.d.ts",
"default": "./dist/native-to-web/index.js"
},
"./resource-cache": {
"types": "./dist/resource-cache/index.d.ts",
"default": "./dist/resource-cache/index.js"
Expand Down Expand Up @@ -105,6 +109,7 @@
"apple",
"biometrics",
"hosted-auth",
"native-to-web",
"experimental",
"legacy",
"app.plugin.js",
Expand Down Expand Up @@ -145,6 +150,7 @@
"expo-secure-store": "^15.0.8",
"expo-web-browser": "^15.0.11",
"react-native": "^0.86.0",
"react-native-webview": "^14.0.1",
"tslib": "catalog:repo"
},
"peerDependencies": {
Expand All @@ -163,7 +169,8 @@
"expo-web-browser": ">=12.5.0",
"react": "^18.0.0 || ^19.0.0",
"react-dom": "^18.0.0 || ^19.0.0",
"react-native": ">=0.75"
"react-native": ">=0.75",
"react-native-webview": ">=13.0.0"
},
"peerDependenciesMeta": {
"@clerk/expo-biometrics": {
Expand Down Expand Up @@ -204,6 +211,9 @@
},
"react-dom": {
"optional": true
},
"react-native-webview": {
"optional": true
}
},
"engines": {
Expand Down
40 changes: 40 additions & 0 deletions packages/expo/src/native-to-web/__tests__/errors.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { describe, expect, test } from 'vitest';

import { isSessionTransferError, sessionTransferError, toSessionTransferError } from '../errors';

describe('session transfer errors', () => {
test('builds an Error with a name and code', () => {
const error = sessionTransferError('expired', 'The sign-in link expired.');

expect(error).toBeInstanceOf(Error);
expect(error).toMatchObject({
name: 'SessionTransferError',
code: 'expired',
message: 'The sign-in link expired.',
});
expect(error).not.toHaveProperty('cause');
});

test('keeps the cause when given one', () => {
const cause = new Error('underlying');

expect(sessionTransferError('network_error', 'Offline.', cause).cause).toBe(cause);
});

test.each([
['a session transfer error', sessionTransferError('signed_out', 'Signed out.'), true],
['an error with a code but another name', Object.assign(new Error('x'), { code: 'signed_out' }), false],
['a plain object', { name: 'SessionTransferError', code: 'signed_out' }, false],
['null', null, false],
])('recognizes %s: %s', (_, value, expected) => {
expect(isSessionTransferError(value)).toBe(expected);
});

test('wraps an unknown failure as unexpected_response', () => {
const cause = new Error('boom');
const known = sessionTransferError('load_failed', 'Load failed.');

expect(toSessionTransferError(known)).toBe(known);
expect(toSessionTransferError(cause)).toMatchObject({ code: 'unexpected_response', cause });
});
});
Loading
Loading