Skip to content
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

feat(clerk-expo): Introduce getClerkInstance to access/create a Clerk instance #3420

Conversation

panteliselef
Copy link
Member

@panteliselef panteliselef commented May 22, 2024

Description

Introduce getClerkInstance to avoid importing the Clerk class from clerk-js manually.

This enables developers to create and access a Clerk instance in their application outside of React.

import { ClerkProvider, getClerkInstance } from "@clerk/expo"

const clerkInstance = getClerkInstance({ publishableKey: 'xxxx' })

// Be sure to pass the new instance to ClerkProvider to avoid running multiple instances of Clerk in your application
<ClerkProvider publishableKey={'xxxx'}>
    ...
</ClerkProvider>

// Somewhere in your code, outside of React you can do
const token = await clerkInstance.session?.getToken();
fetch('http://example.com/', {headers: {Authorization: token })
import { ClerkProvider, getClerkInstance } from "@clerk/expo"

// Always pass the `publishableKey` to `ClerkProvider`
<ClerkProvider publishableKey={'xxxx'}>
    ...
</ClerkProvider>

// If you sure that this code will run after the ClerkProvider has rendered then you can use `getClerkIntance` without options
const token = await getClerkInstance().session?.getToken();
fetch('http://example.com/', {headers: {Authorization: token })

⚠Note:

  • Keep in mind that calling getClerkInstance() with different publishable keys will create a new Clerk instance.
  • If getClerkInstance is called without a publishable key, and ClerkProvider has not rendered yet, an error will be thrown

Checklist

  • npm test runs as expected.
  • npm run build runs as expected.
  • (If applicable) JSDoc comments have been added or updated for any package exports
  • (If applicable) Documentation has been updated

Type of change

  • 🐛 Bug fix
  • 🌟 New feature
  • 🔨 Breaking change
  • 📖 Refactoring / dependency upgrade / documentation
  • other:

Copy link

changeset-bot bot commented May 22, 2024

🦋 Changeset detected

Latest commit: 8eb1154

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@clerk/clerk-expo Minor

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@panteliselef panteliselef requested review from BRKalow, dimkl and nikosdouvlis and removed request for BRKalow May 22, 2024 09:38
@panteliselef panteliselef self-assigned this May 22, 2024
@panteliselef panteliselef changed the title feat(expo): Introduce createClerkClient to avoid importing the Clerk class from clerk-js manually feat(clerk-expo): Introduce createClerkClient to avoid importing the Clerk class from clerk-js manually May 22, 2024
@panteliselef panteliselef force-pushed the elef/sdk-1724-figure-out-how-we-should-respond-to-the-token-outside-of branch from 58266b6 to b9fce29 Compare May 22, 2024 09:54
Copy link
Member

@octoper octoper left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔥

const clerkInstance = createClerkClient({ publishableKey: 'xxxx' })

// Be sure to pass the new instance to ClerkProvider to avoid running multiple instances of Clerk in your application
<ClerkProvider Clerk={clerkInstance}>

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

so going forward in Expo apps, every developer should create this new Clerk instance and pass it in if they want to be able to access it outside of React right?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah that's right they should create the new instance with createClerkClient

@BRKalow
Copy link
Member

BRKalow commented May 22, 2024

Is this preferable over exporting the singleton? Seems like we're introducing a footgun if someone forgets to pass their Clerk instance to ClerkProvider.

@panteliselef
Copy link
Member Author

@BRKalow the exported Clerk variable will be undefined until the ClerkProvider renders. I find that weird, because the types don't indicate it. The createClerkClient is a pattern that we already have and expect developers to follow. We do something similar in clerk-react

Copy link
Member

@BRKalow BRKalow left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Revisit naming to not conflict with clerk/backend naming, and make sure we don't make it possible to create two clerk instances.

@panteliselef panteliselef force-pushed the elef/sdk-1724-figure-out-how-we-should-respond-to-the-token-outside-of branch from ac33c7d to 4492bba Compare May 24, 2024 15:08
@panteliselef panteliselef changed the title feat(clerk-expo): Introduce createClerkClient to avoid importing the Clerk class from clerk-js manually feat(clerk-expo): Introduce getClerkInstance to avoid importing the Clerk class from clerk-js manually May 24, 2024
@royanger
Copy link
Member

This request comes up with React/Next/Remix apps when using tools like Tanstack Query or even just Axios/Fetch. We see this request with even Chrome Extensions. We can use window.Clerk.session.getToken() but that's not a great experience. Can we provide this for all SDKs to use use outside of hooks?

@panteliselef panteliselef force-pushed the elef/sdk-1724-figure-out-how-we-should-respond-to-the-token-outside-of branch from 4492bba to 06903f9 Compare June 3, 2024 09:24
@panteliselef panteliselef changed the title feat(clerk-expo): Introduce getClerkInstance to avoid importing the Clerk class from clerk-js manually feat(clerk-expo): Introduce getClerkInstance to access/create a Clerk instance Jun 3, 2024
Copy link
Contributor

@dimkl dimkl left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

❓ Should we add the getClerkInstance in the rest of our frontend-only packages (eg chrome-extension) to support the same use case?
Also, should we add it to the server-related packages (eg nextjs, remix) as a way to retrieve the default backend clerkClient instead of using the exported clerkClient?

@@ -11,29 +13,56 @@ Clerk.sdkMetadata = {

const KEY = '__clerk_client_jwt';

/**
* @deprecated Use `getClerkInstance` instead. `Clerk` will be removed in the next major version.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🙃 typo, clerk should be with lower case to match the actual export.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So i looked at this again, turns out the exported variable from the package is Clerk so the initial comment is correct

@panteliselef panteliselef enabled auto-merge (squash) June 4, 2024 13:58
@panteliselef panteliselef merged commit 89fc4b9 into main Jun 4, 2024
11 checks passed
@panteliselef panteliselef deleted the elef/sdk-1724-figure-out-how-we-should-respond-to-the-token-outside-of branch June 4, 2024 14:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants