Skip to content

Commit ae2b5a3

Browse files
authored
docs(expo): Add guide for accessing Clerk outside components in Expo (#2102)
1 parent 6dd3e69 commit ae2b5a3

File tree

2 files changed

+73
-0
lines changed

2 files changed

+73
-0
lines changed

docs/manifest.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -676,6 +676,10 @@
676676
{
677677
"title": "Offline support",
678678
"href": "/docs/references/expo/offline-support"
679+
},
680+
{
681+
"title": "Access the `Clerk` object outside of components",
682+
"href": "/docs/references/expo/access-clerk-outside-components"
679683
}
680684
]
681685
]
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
---
2+
title: Access the Clerk object outside of components
3+
description: Learn how to access and use the Clerk instance outside of React components in Expo applications.
4+
---
5+
6+
The [`Clerk`](/docs/references/javascript/clerk) object is accessible using the [`useClerk()`](/docs/hooks/use-clerk) hook. However, if you need to access the [`Clerk`](/docs/references/javascript/clerk) object outside of React components, such as in utility functions or background tasks, you can use the `getClerkInstance()` function.
7+
8+
<Tabs items={["Fetch", "Axios"]}>
9+
<Tab>
10+
```ts
11+
import { getClerkInstance } from '@clerk/clerk-expo'
12+
13+
export async function fetchFoo() {
14+
const clerkInstance = getClerkInstance()
15+
// Use `getToken()` to get the current session token
16+
const token = await clerkInstance.session?.getToken()
17+
18+
const response = await fetch('/api/foo', {
19+
headers: {
20+
// Include the session token as a Bearer token in the Authorization header
21+
Authorization: `Bearer ${token}`,
22+
'Content-Type': 'application/json',
23+
},
24+
})
25+
26+
if (!response.ok) {
27+
// Include status code and status text in error message
28+
throw new Error(`API Error: ${response.status} ${response.statusText}`)
29+
}
30+
31+
return response.json()
32+
}
33+
```
34+
</Tab>
35+
36+
<Tab>
37+
```ts
38+
import axios from 'axios'
39+
import { getClerkInstance } from '@clerk/clerk-expo'
40+
41+
export async function fetchFoo() {
42+
try {
43+
const data = await axios.get('/api/foo')
44+
return data
45+
} catch (error) {
46+
if (axios.isAxiosError(error) && error.response) {
47+
throw new Error(`API Error: ${error.response.status} ${error.response.statusText}`)
48+
}
49+
50+
throw new Error('Unknown error')
51+
}
52+
}
53+
54+
// Intercept requests and modify them to include the current session token
55+
axios.interceptors.request.use(async (config) => {
56+
const clerkInstance = getClerkInstance()
57+
// Use `getToken()` to get the current session token
58+
const token = await clerkInstance.session?.getToken()
59+
60+
if (token) {
61+
// Include the session token as a Bearer token in the Authorization header
62+
config.headers.Authorization = `Bearer ${token}`
63+
}
64+
65+
return config
66+
})
67+
```
68+
</Tab>
69+
</Tabs>

0 commit comments

Comments
 (0)