Skip to content

Commit

Permalink
feat(clerk-js,backend): Url for copying instance keys on Keyless mode (
Browse files Browse the repository at this point in the history
  • Loading branch information
panteliselef authored Dec 11, 2024
1 parent 7f7edca commit e174858
Show file tree
Hide file tree
Showing 11 changed files with 44 additions and 6 deletions.
7 changes: 7 additions & 0 deletions .changeset/gold-monkeys-collect.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
'@clerk/clerk-js': patch
'@clerk/nextjs': patch
'@clerk/types': patch
---

Introduce `__internal_copyInstanceKeysUrl` as property in `ClerkOptions`. It is intented for internall usage from other Clerk SDKs and will be used in Keyless mode.
5 changes: 5 additions & 0 deletions .changeset/twelve-spiders-obey.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@clerk/backend': patch
---

Add property `api_keys_url` in the `AccountlessApplication` class
3 changes: 2 additions & 1 deletion packages/backend/src/api/resources/AccountlessApplication.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@ export class AccountlessApplication {
readonly publishableKey: string,
readonly secretKey: string,
readonly claimUrl: string,
readonly apiKeysUrl: string,
) {}

static fromJSON(data: AccountlessApplicationJSON): AccountlessApplication {
return new AccountlessApplication(data.publishable_key, data.secret_key, data.claim_url);
return new AccountlessApplication(data.publishable_key, data.secret_key, data.claim_url, data.api_keys_url);
}
}
1 change: 1 addition & 0 deletions packages/backend/src/api/resources/JSON.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ export interface AccountlessApplicationJSON extends ClerkResourceJSON {
publishable_key: string;
secret_key: string;
claim_url: string;
api_keys_url: string;
}

export interface AllowlistIdentifierJSON extends ClerkResourceJSON {
Expand Down
5 changes: 4 additions & 1 deletion packages/clerk-js/src/core/clerk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2052,7 +2052,10 @@ export class Clerk implements ClerkInterface {
void this.#componentControls?.ensureMounted().then(controls => {
if (this.#options.__internal_claimKeylessApplicationUrl) {
controls.updateProps({
options: { __internal_claimKeylessApplicationUrl: this.#options.__internal_claimKeylessApplicationUrl },
options: {
__internal_claimKeylessApplicationUrl: this.#options.__internal_claimKeylessApplicationUrl,
__internal_copyInstanceKeysUrl: this.#options.__internal_copyInstanceKeysUrl,
},
});
}
});
Expand Down
8 changes: 6 additions & 2 deletions packages/clerk-js/src/ui/Components.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -518,9 +518,13 @@ const Components = (props: ComponentsProps) => {
)}

{__BUILD_FLAG_KEYLESS_UI__
? state.options?.__internal_claimKeylessApplicationUrl && (
? state.options?.__internal_claimKeylessApplicationUrl &&
state.options?.__internal_copyInstanceKeysUrl && (
<LazyImpersonationFabProvider globalAppearance={state.appearance}>
<KeylessPrompt url={state.options.__internal_claimKeylessApplicationUrl} />
<KeylessPrompt
claimUrl={state.options.__internal_claimKeylessApplicationUrl}
copyKeysUrl={state.options.__internal_copyInstanceKeysUrl}
/>
</LazyImpersonationFabProvider>
)
: null}
Expand Down
8 changes: 7 additions & 1 deletion packages/clerk-js/src/ui/components/KeylessPrompt/index.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { useClerk } from '@clerk/shared/react';
// eslint-disable-next-line no-restricted-imports
import { css } from '@emotion/react';
import { useState } from 'react';
Expand All @@ -9,12 +10,14 @@ import { ClerkLogoIcon } from './ClerkLogoIcon';
import { KeySlashIcon } from './KeySlashIcon';

type KeylessPromptProps = {
url?: string;
claimUrl: string;
copyKeysUrl: string;
};

const _KeylessPrompt = (_props: KeylessPromptProps) => {
const [isExpanded, setIsExpanded] = useState(true);
const handleFocus = () => setIsExpanded(true);
const clerk = useClerk();

return (
<Portal>
Expand Down Expand Up @@ -310,6 +313,9 @@ const _KeylessPrompt = (_props: KeylessPromptProps) => {
type='button'
onFocus={handleFocus}
data-expanded={isExpanded}
onClick={() => {
void clerk.navigate(_props.claimUrl);
}}
css={css`
position: absolute;
right: 0.375rem;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ export const KeylessCreatorOrReader = (props: NextClerkProviderProps) => {
key: state?.publishableKey,
publishableKey: state?.publishableKey,
__internal_claimKeylessApplicationUrl: state?.claimUrl,
__internal_copyInstanceKeysUrl: state?.apiKeysUrl,
__internal_bypassMissingPublishableKey: true,
} as any);
};
3 changes: 2 additions & 1 deletion packages/nextjs/src/app-router/keyless-actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export async function createOrReadKeylessAction(): Promise<null | Omit<Accountle
return null;
}

const { claimUrl, publishableKey, secretKey } = result;
const { claimUrl, publishableKey, secretKey, apiKeysUrl } = result;

void (await cookies()).set(getKeylessCookieName(), JSON.stringify({ claimUrl, publishableKey, secretKey }), {
secure: false,
Expand All @@ -40,5 +40,6 @@ export async function createOrReadKeylessAction(): Promise<null | Omit<Accountle
return {
claimUrl,
publishableKey,
apiKeysUrl,
};
}
1 change: 1 addition & 0 deletions packages/nextjs/src/app-router/server/ClerkProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ export async function ClerkProvider(
...rest,
publishableKey: newOrReadKeys.publishableKey,
__internal_claimKeylessApplicationUrl: newOrReadKeys.claimUrl,
__internal_copyInstanceKeysUrl: newOrReadKeys.apiKeysUrl,
})}
nonce={await nonce}
initialState={await statePromise}
Expand Down
8 changes: 8 additions & 0 deletions packages/types/src/clerk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -753,8 +753,16 @@ export type ClerkOptions = ClerkOptionsNavigation &
Record<string, any>
>;

/**
* The URL a developer should be redirected to in order to claim an instance created on Keyless mode.
*/
__internal_claimKeylessApplicationUrl?: string;

/**
* After a developer has claimed their instance created by Keyless mode, they can use this URL to find their instance's keys
*/
__internal_copyInstanceKeysUrl?: string;

/**
* [EXPERIMENTAL] Provide the underlying host router, required for the new experimental UI components.
*/
Expand Down

0 comments on commit e174858

Please sign in to comment.