-
Notifications
You must be signed in to change notification settings - Fork 267
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(nextjs): Support Accountless mode #4602
base: main
Are you sure you want to change the base?
Changes from all commits
202e089
ef1e29a
55e1f2d
86758da
af91677
0b40c97
0646cc9
5e0181d
07d7a08
dbfb097
7f0020a
293b15b
e971a2d
c4b1ac5
afd7dc9
8e1b64f
ca1df30
41bc200
b27445d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
--- | ||
'@clerk/clerk-js': minor | ||
'@clerk/types': minor | ||
--- | ||
|
||
accountless | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Once we're ready, let's expand the changeset description here. And consolidate the two changeset files. |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
--- | ||
'@clerk/backend': minor | ||
'@clerk/nextjs': minor | ||
--- | ||
|
||
accountless | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. todo |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -132,7 +132,7 @@ export default function RootLayout({ children }: { children: React.ReactNode }) | |
'src/app/nested-provider/page.tsx', | ||
() => `import { ClerkProvider } from '@clerk/nextjs'; | ||
import { ClientComponent } from './client'; | ||
|
||
export default function Page() { | ||
return ( | ||
<ClerkProvider dynamic> | ||
|
@@ -147,10 +147,10 @@ export default function RootLayout({ children }: { children: React.ReactNode }) | |
() => `'use client'; | ||
|
||
import { useAuth } from '@clerk/nextjs'; | ||
|
||
export function ClientComponent() { | ||
useAuth(); | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this is just auto formatting |
||
return <p>I am dynamically rendered</p>; | ||
} | ||
`, | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import type { AccountlessApplication } from '../resources/AccountlessApplication'; | ||
import { AbstractAPI } from './AbstractApi'; | ||
|
||
const basePath = '/accountless_applications'; | ||
|
||
export class AccountlessApplicationAPI extends AbstractAPI { | ||
public async createAccountlessApplication() { | ||
return this.request<AccountlessApplication>({ | ||
method: 'POST', | ||
path: basePath, | ||
}); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
import { | ||
AccountlessApplicationAPI, | ||
AllowlistIdentifierAPI, | ||
ClientAPI, | ||
DomainAPI, | ||
|
@@ -23,6 +24,9 @@ export function createBackendApiClient(options: CreateBackendApiOptions) { | |
const request = buildRequest(options); | ||
|
||
return { | ||
__experimental_accountlessApplications: new AccountlessApplicationAPI( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. i'd like to have this as experimental for a while |
||
buildRequest({ ...options, skipSecretKey: true }), | ||
), | ||
allowlistIdentifiers: new AllowlistIdentifierAPI(request), | ||
clients: new ClientAPI(request), | ||
emailAddresses: new EmailAddressAPI(request), | ||
|
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -51,13 +51,23 @@ type BuildRequestOptions = { | |||||
apiVersion?: string; | ||||||
/* Library/SDK name */ | ||||||
userAgent?: string; | ||||||
|
||||||
skipSecretKey?: boolean; | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. the endpoint for creating the accountless keys doesn't require a secret key |
||||||
}; | ||||||
export function buildRequest(options: BuildRequestOptions) { | ||||||
const requestFn = async <T>(requestOptions: ClerkBackendApiRequestOptions): Promise<ClerkBackendApiResponse<T>> => { | ||||||
const { secretKey, apiUrl = API_URL, apiVersion = API_VERSION, userAgent = USER_AGENT } = options; | ||||||
const { | ||||||
secretKey, | ||||||
skipSecretKey = false, | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think
Suggested change
|
||||||
apiUrl = API_URL, | ||||||
apiVersion = API_VERSION, | ||||||
userAgent = USER_AGENT, | ||||||
} = options; | ||||||
const { path, method, queryParams, headerParams, bodyParams, formData } = requestOptions; | ||||||
|
||||||
assertValidSecretKey(secretKey); | ||||||
if (!skipSecretKey) { | ||||||
assertValidSecretKey(secretKey); | ||||||
} | ||||||
|
||||||
const url = joinPaths(apiUrl, apiVersion, path); | ||||||
|
||||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import type { AccountlessApplicationJSON } from './JSON'; | ||
|
||
export class AccountlessApplication { | ||
constructor( | ||
readonly publishableKey: string, | ||
readonly secretKey: string, | ||
readonly claimUrl: string, | ||
) {} | ||
|
||
static fromJSON(data: AccountlessApplicationJSON): AccountlessApplication { | ||
return new AccountlessApplication(data.publishable_key, data.secret_key, data.claim_url); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,6 +4,16 @@ | |
"#components": { | ||
"react-server": "./components.server.js", | ||
"default": "./components.client.js" | ||
}, | ||
"#fs": { | ||
"edge-light": "./runtime/browser/fs.js", | ||
"worker": "./runtime/browser/fs.js", | ||
"browser": "./runtime/browser/fs.js", | ||
"node": { | ||
"require": "./runtime/node/fs.js", | ||
"import": "./runtime/node/fs.js" | ||
}, | ||
"default": "./runtime/browser/fs.js" | ||
Comment on lines
+7
to
+16
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. fixes a bug in nextjs where importing from |
||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,6 +4,16 @@ | |
"#components": { | ||
"react-server": "./components.server.js", | ||
"default": "./components.client.js" | ||
}, | ||
"#fs": { | ||
"edge-light": "./runtime/browser/fs.js", | ||
"worker": "./runtime/browser/fs.js", | ||
"browser": "./runtime/browser/fs.js", | ||
"node": { | ||
"require": "./runtime/node/fs.js", | ||
"import": "./runtime/node/fs.js" | ||
}, | ||
"default": "./runtime/browser/fs.js" | ||
Comment on lines
+8
to
+16
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. fixes a bug in nextjs where importing from |
||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
'use server'; | ||
import type { AccountlessApplication } from '@clerk/backend'; | ||
import { isDevelopmentEnvironment } from '@clerk/shared/utils'; | ||
import { getCookies } from 'ezheaders'; | ||
import { redirect, RedirectType } from 'next/navigation'; | ||
|
||
import { getAccountlessCookieName } from '../server/accountless'; | ||
import { ALLOW_ACCOUNTLESS } from '../server/constants'; | ||
import { isNextWithUnstableServerActions } from '../utils/sdk-versions'; | ||
|
||
export async function syncAccountlessKeysAction(args: AccountlessApplication & { returnUrl: string }): Promise<void> { | ||
const { claimUrl, publishableKey, secretKey, returnUrl } = args; | ||
void (await getCookies()).set(getAccountlessCookieName(), JSON.stringify({ claimUrl, publishableKey, secretKey }), { | ||
secure: true, | ||
httpOnly: true, | ||
}); | ||
|
||
// TODO-ACCOUNTLESS: Do we even need this ? I think setting the cookie will reset the router cache. | ||
redirect(`/clerk-sync-accountless?returnUrl=${returnUrl}`, RedirectType.replace); | ||
Comment on lines
+18
to
+19
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What is the route for specifically? Would be nice if we could remove! Would it be enough if we triggered a full page refresh? |
||
} | ||
|
||
export async function createAccountlessKeysAction(): Promise<null | Omit<AccountlessApplication, 'secretKey'>> { | ||
if (!isDevelopmentEnvironment() || isNextWithUnstableServerActions || !ALLOW_ACCOUNTLESS) { | ||
return null; | ||
} | ||
|
||
const result = await import('../server/accountless-node.js').then(m => m.createAccountlessKeys()); | ||
|
||
if (!result) { | ||
return null; | ||
} | ||
|
||
const { claimUrl, publishableKey, secretKey } = result; | ||
|
||
void (await getCookies()).set(getAccountlessCookieName(), JSON.stringify({ claimUrl, publishableKey, secretKey }), { | ||
secure: false, | ||
httpOnly: false, | ||
}); | ||
|
||
return { | ||
claimUrl, | ||
publishableKey, | ||
}; | ||
} |
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -1,17 +1,28 @@ | ||||||
'use client'; | ||||||
import { ClerkProvider as ReactClerkProvider } from '@clerk/clerk-react'; | ||||||
import dynamic from 'next/dynamic'; | ||||||
import { useRouter } from 'next/navigation'; | ||||||
import React, { useEffect, useTransition } from 'react'; | ||||||
|
||||||
import { useSafeLayoutEffect } from '../../client-boundary/hooks/useSafeLayoutEffect'; | ||||||
import { ClerkNextOptionsProvider, useClerkNextOptions } from '../../client-boundary/NextOptionsContext'; | ||||||
import { ALLOW_ACCOUNTLESS } from '../../server/constants'; | ||||||
import type { NextClerkProviderProps } from '../../types'; | ||||||
import { ClerkJSScript } from '../../utils/clerk-js-script'; | ||||||
import { mergeNextClerkPropsWithEnv } from '../../utils/mergeNextClerkPropsWithEnv'; | ||||||
import { isNextWithUnstableServerActions } from '../../utils/sdk-versions'; | ||||||
import { invalidateCacheAction } from '../server-actions'; | ||||||
import { useAwaitablePush } from './useAwaitablePush'; | ||||||
import { useAwaitableReplace } from './useAwaitableReplace'; | ||||||
|
||||||
/** | ||||||
* Accountless creator should only be loaded if the conditions below are met. | ||||||
* Note: Using lazy() with Suspense instead of dynamic is not possible as React will throw a hydration error when `ClerkProvider` wraps `<html><body>...` | ||||||
*/ | ||||||
const LazyAccountlessCreator = dynamic(() => | ||||||
import('./lazy-accountless-creator.js').then(m => m.AccountlessCreateKeys), | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is the extension necessary?
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. i believe eslint was complaining |
||||||
); | ||||||
|
||||||
declare global { | ||||||
export interface Window { | ||||||
__clerk_nav_await: Array<(value: void) => void>; | ||||||
|
@@ -23,7 +34,7 @@ declare global { | |||||
} | ||||||
} | ||||||
|
||||||
export const ClientClerkProvider = (props: NextClerkProviderProps) => { | ||||||
const NextClientClerkProvider = (props: NextClerkProviderProps) => { | ||||||
const { __unstable_invokeMiddlewareOnAuthStateChange = true, children } = props; | ||||||
const router = useRouter(); | ||||||
const push = useAwaitablePush(); | ||||||
|
@@ -99,3 +110,18 @@ export const ClientClerkProvider = (props: NextClerkProviderProps) => { | |||||
</ClerkNextOptionsProvider> | ||||||
); | ||||||
}; | ||||||
|
||||||
export const ClientClerkProvider = (props: NextClerkProviderProps) => { | ||||||
const { children, ...rest } = props; | ||||||
const safePk = mergeNextClerkPropsWithEnv(rest).publishableKey; | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
|
||||||
if (safePk || isNextWithUnstableServerActions || !ALLOW_ACCOUNTLESS) { | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
return <NextClientClerkProvider {...rest}>{children}</NextClientClerkProvider>; | ||||||
} | ||||||
|
||||||
return ( | ||||||
<LazyAccountlessCreator> | ||||||
<NextClientClerkProvider {...rest}>{children}</NextClientClerkProvider> | ||||||
</LazyAccountlessCreator> | ||||||
); | ||||||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
'use client'; | ||
|
||
import type { AccountlessApplication } from '@clerk/backend'; | ||
import type { PropsWithChildren } from 'react'; | ||
import { useEffect } from 'react'; | ||
|
||
import { isNextWithUnstableServerActions } from '../../utils/sdk-versions'; | ||
|
||
export function AccountlessCookieSync(props: PropsWithChildren<AccountlessApplication>) { | ||
useEffect(() => { | ||
if (!isNextWithUnstableServerActions) { | ||
void import('../accountless-actions.js').then(m => | ||
m.syncAccountlessKeysAction({ | ||
...props, | ||
// Preserve the current url and return back, once keys are synced in the middleware | ||
returnUrl: window.location.href, | ||
}), | ||
); | ||
} | ||
}, []); | ||
|
||
return props.children; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import React, { useEffect } from 'react'; | ||
|
||
import type { NextClerkProviderProps } from '../../types'; | ||
import { createAccountlessKeysAction } from '../accountless-actions'; | ||
|
||
export const AccountlessCreateKeys = (props: NextClerkProviderProps) => { | ||
const { children } = props; | ||
const [state, fetchKeys] = React.useActionState(createAccountlessKeysAction, null); | ||
useEffect(() => { | ||
React.startTransition(() => { | ||
fetchKeys(); | ||
}); | ||
}, []); | ||
|
||
if (!React.isValidElement(children)) { | ||
return children; | ||
} | ||
|
||
return React.cloneElement(children, { | ||
key: state?.publishableKey, | ||
publishableKey: state?.publishableKey, | ||
__internal_claimAccountlessKeysUrl: state?.claimUrl, | ||
__internal_bypassMissingPk: true, | ||
} as any); | ||
}; |
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.
todo