-
Notifications
You must be signed in to change notification settings - Fork 361
feat(nextjs): Support Keyless mode #4602
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
Changes from 3 commits
202e089
ef1e29a
55e1f2d
86758da
af91677
0b40c97
0646cc9
5e0181d
07d7a08
dbfb097
7f0020a
293b15b
e971a2d
c4b1ac5
afd7dc9
8e1b64f
ca1df30
41bc200
b27445d
29abac2
92efadc
5342e15
b594ace
0888c61
eded662
fd12da6
bc7fc4c
1b203ae
b2bad5e
c0210e1
8df06f3
06c1f73
2950e68
206bb4f
b04a9d2
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 |
---|---|---|
@@ -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, | ||
panteliselef marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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 |
---|---|---|
@@ -0,0 +1,15 @@ | ||
'use client'; | ||
|
||
import type { AccountlessApplication } from '@clerk/backend'; | ||
import type { PropsWithChildren } from 'react'; | ||
import { useEffect } from 'react'; | ||
|
||
import { syncAccountlessKeysAction } from '../server-actions'; | ||
|
||
export function AccountlessCookieSync(props: PropsWithChildren<AccountlessApplication>) { | ||
useEffect(() => { | ||
void syncAccountlessKeysAction(props); | ||
}, []); | ||
|
||
return props.children; | ||
} |
panteliselef marked this conversation as resolved.
Show resolved
Hide resolved
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
import { appendFileSync, existsSync, mkdirSync, readFileSync, rmSync, writeFileSync } from 'node:fs'; | ||
import path from 'node:path'; | ||
|
||
import type { AccountlessApplication } from '@clerk/backend'; | ||
|
||
import { createClerkClientWithOptions } from './clerkClient'; | ||
|
||
const CLERK_HIDDEN = '.clerk'; | ||
|
||
function updateGitignore() { | ||
const gitignorePath = path.join(process.cwd(), '.gitignore'); | ||
if (!existsSync(gitignorePath)) { | ||
writeFileSync(gitignorePath, ''); | ||
} | ||
|
||
// Check if `.clerk/` entry exists in .gitignore | ||
const gitignoreContent = readFileSync(gitignorePath, 'utf-8'); | ||
if (!gitignoreContent.includes(CLERK_HIDDEN + '/')) { | ||
appendFileSync(gitignorePath, `\n${CLERK_HIDDEN}/\n`); | ||
panteliselef marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
} | ||
|
||
const CLERK_LOCK = 'clerk.lock'; | ||
const getClerkPath = () => path.join(process.cwd(), '.clerk', '.tmp', 'accountless.json'); | ||
|
||
let isCreatingFile = false; | ||
|
||
function safeParseClerkFile(): AccountlessApplication | undefined { | ||
try { | ||
const CLERK_PATH = getClerkPath(); | ||
let fileAsString; | ||
try { | ||
fileAsString = readFileSync(CLERK_PATH, { encoding: 'utf-8' }) || '{}'; | ||
} catch { | ||
fileAsString = '{}'; | ||
} | ||
return JSON.parse(fileAsString) as AccountlessApplication; | ||
} catch { | ||
return undefined; | ||
} | ||
} | ||
|
||
async function createAccountlessKeys(): Promise<AccountlessApplication | undefined> { | ||
if (isCreatingFile) { | ||
return undefined; | ||
} | ||
|
||
if (existsSync(CLERK_LOCK)) { | ||
panteliselef marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return undefined; | ||
} | ||
|
||
isCreatingFile = true; | ||
|
||
writeFileSync(CLERK_LOCK, 'You can delete this file.', { | ||
encoding: 'utf8', | ||
mode: '0777', | ||
flag: 'w', | ||
}); | ||
|
||
const CLERK_PATH = getClerkPath(); | ||
mkdirSync(path.dirname(CLERK_PATH), { recursive: true }); | ||
updateGitignore(); | ||
|
||
const envVarsMap = safeParseClerkFile(); | ||
|
||
if (envVarsMap?.publishableKey && envVarsMap?.secretKey) { | ||
isCreatingFile = false; | ||
rmSync(CLERK_LOCK, { force: true, recursive: true }); | ||
return envVarsMap; | ||
} | ||
|
||
/** | ||
* Maybe the server has not restarted yet | ||
*/ | ||
panteliselef marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
const client = createClerkClientWithOptions({}); | ||
|
||
const accountlessApplication = await client.__experimental_accountlessApplications.createAccountlessApplication(); | ||
|
||
console.log('--- new keys', accountlessApplication); | ||
|
||
writeFileSync(CLERK_PATH, JSON.stringify(accountlessApplication), { | ||
encoding: 'utf8', | ||
mode: '0777', | ||
flag: 'w', | ||
}); | ||
|
||
rmSync(CLERK_LOCK, { force: true, recursive: true }); | ||
|
||
isCreatingFile = false; | ||
return accountlessApplication; | ||
} | ||
|
||
export { createAccountlessKeys }; |
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