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): Support swapping publishableKey at runtime #1655

Merged
merged 3 commits into from
Sep 1, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
7 changes: 7 additions & 0 deletions .changeset/hot-pans-grow.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
'@clerk/clerk-js': patch
'@clerk/clerk-react': patch
'@clerk/clerk-expo': patch
BRKalow marked this conversation as resolved.
Show resolved Hide resolved
---

Support swapping the Clerk publishableKey at runtime to allow users to toggle the instance being used.
1 change: 1 addition & 0 deletions packages/clerk-js/src/core/clerk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,7 @@ export default class Clerk implements ClerkInterface {

const { frontendApi, instanceType } = publishableKey as PublishableKey;

this.publishableKey = key;
Copy link
Member Author

Choose a reason for hiding this comment

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

This is specified as a public readonly property, but we weren't setting it anywhere.

Copy link
Member

Choose a reason for hiding this comment

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

I'm almost sure this is not used anywhere - @panteliselef , any chance this is needed by the multidomains feature?

Copy link
Member Author

Choose a reason for hiding this comment

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

I'm using it in another spot in this PR to check for a new key, but let me know if we don't want to set this for some reason!

this.frontendApi = frontendApi;
this.#instanceType = instanceType;
}
Expand Down
2 changes: 2 additions & 0 deletions packages/expo/src/ClerkProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ export function ClerkProvider(props: ClerkProviderProps): JSX.Element {
return (
//@ts-expect-error
<ClerkReactProvider
// Force reset the state when the provided key changes, this ensures that the provider does not retain stale state. See JS-598 for additional context.
key={key}
Copy link
Contributor

Choose a reason for hiding this comment

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

❓I think that there is publishableKey and not key prop in ClerkReactProvider. Could we double check this?

Copy link
Member Author

Choose a reason for hiding this comment

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

The use of key here is intentional to force react to remount the component and wipe away any stale state from the previous instance. As far as I can tell, by passing the clerk instance below we remove the need to pass the publishableKey as a prop

{...rest}
Clerk={buildClerk({ key, tokenCache })}
standardBrowser={!isReactNative()}
Expand Down
4 changes: 4 additions & 0 deletions packages/expo/src/cache.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
export interface TokenCache {
getToken: (key: string) => Promise<string | undefined | null>;
saveToken: (key: string, token: string) => Promise<void>;
clearToken?: (key: string) => void;
}

const createMemoryTokenCache = (): TokenCache => {
Expand All @@ -13,6 +14,9 @@ const createMemoryTokenCache = (): TokenCache => {
getToken: key => {
return Promise.resolve(cache[key]);
},
clearToken: key => {
delete cache[key];
},
};
};

Expand Down
9 changes: 8 additions & 1 deletion packages/expo/src/singleton.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,14 @@ type BuildClerkOptions = {
};

export function buildClerk({ key, tokenCache }: BuildClerkOptions): HeadlessBrowserClerk {
if (!clerk) {
// Support "hot-swapping" the Clerk instance at runtime. See JS-598 for additional details.
const hasKeyChanged = clerk && key !== clerk.publishableKey;

if (!clerk || hasKeyChanged) {
if (hasKeyChanged) {
tokenCache.clearToken?.(KEY);
}
nikosdouvlis marked this conversation as resolved.
Show resolved Hide resolved

const getToken = tokenCache.getToken;
const saveToken = tokenCache.saveToken;
// TODO: DO NOT ACCEPT THIS
Expand Down
3 changes: 2 additions & 1 deletion packages/react/src/isomorphicClerk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,9 @@ export default class IsomorphicClerk {
static getOrCreateInstance(options: IsomorphicClerkOptions) {
// During SSR: a new instance should be created for every request
// During CSR: use the cached instance for the whole lifetime of the app
// Also will recreate the instance if the provided Clerk instance changes
// This method should be idempotent in both scenarios
if (!inClientSide() || !this.#instance) {
if (!inClientSide() || !this.#instance || (options.Clerk && this.#instance.Clerk !== options.Clerk)) {
Copy link
Member Author

Choose a reason for hiding this comment

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

Without this change, there was no way to forcibly reset the created IsomorphicClerk instance. Given that we have not considered the possibility of changing the instance of Clerk at runtime, adding this additional check seems reasonable given the new requirement.

Copy link
Member

Choose a reason for hiding this comment

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

This makes sense 👍🏻

this.#instance = new IsomorphicClerk(options);
}
return this.#instance;
Expand Down