-
Notifications
You must be signed in to change notification settings - Fork 612
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
docs(expo): Add guide for accessing Clerk outside components in Expo #2102
Conversation
…o-use-new-package-name
Hey, here’s your docs preview: https://clerk.com/docs/pr/2102 |
import axios from 'axios' | ||
import { getClerkInstance } from '@clerk/clerk-expo' | ||
|
||
export async function fetchFoo() { | ||
try { | ||
const data = await axios.get('/api/foo') | ||
return data | ||
} catch (error) { | ||
if (axios.isAxiosError(error) && error.response) { | ||
throw new Error(`API Error: ${error.response.status} ${error.response.statusText}`) | ||
} | ||
|
||
throw new Error('Unknown error') | ||
} | ||
} | ||
|
||
// Intercept requests and modify them to include the current session token | ||
axios.interceptors.request.use(async (config) => { | ||
const clerkInstance = getClerkInstance() | ||
// Use `getToken()` to get the current session token | ||
const token = await clerkInstance.session?.getToken() | ||
|
||
if (token) { | ||
// Include the session token as a Bearer token in the Authorization header | ||
config.headers.Authorization = `Bearer ${token}` | ||
} | ||
|
||
return config | ||
}) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Axios interceptors is a very common pattern among axios devs for handling auth tokens
See https://github.com/orgs/clerk/discussions/1751#discussioncomment-8907251
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
From a sdk standpoint this is correct, but let's wait for the docs team to review as well.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
- updated the name to remove gerund ('-ing')
- slightly updated the copy
the rest lgtm 😸💖
Thanks @alexisintech! ❤️❤️ |
🔎 Previews:
clerk-demo-pr.mov
Demo repo: https://github.com/wobsoriano/clerk-docs-2102
What does this solve?
getClerkInstance()
available since last year, but it's not documented and is hard for customers to find.What changed?
Checklist