Skip to content

feat(nextjs): Read dynamic keys from clerkClient within middleware runtime - #3617

Merged
LauraBeatris merged 9 commits into
propagate-nextjs-middleware-optionsfrom
read-dynamic-keys-from-clerk-client
Jun 27, 2024
Merged

feat(nextjs): Read dynamic keys from clerkClient within middleware runtime #3617
LauraBeatris merged 9 commits into
propagate-nextjs-middleware-optionsfrom
read-dynamic-keys-from-clerk-client

Conversation

@LauraBeatris

@LauraBeatris LauraBeatris commented Jun 21, 2024

Copy link
Copy Markdown
Contributor

Description

Resolves SDK-1253, SDK-1811, SDK-1770

PR on top of #3525 to facilitate review on clerkClient changes.

Refactoring clerkClient to be called as a function, instead of singleton, in order to read request data. It reads based on the following:

  • If clerkClient gets accessed on middleware runtime, then use ALS store to access dynamic keys data
  • If clerkClient gets accessed on application server, then use NextRequest to access dynamic keys data

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:

@LauraBeatris LauraBeatris self-assigned this Jun 21, 2024
@changeset-bot

changeset-bot Bot commented Jun 21, 2024

Copy link
Copy Markdown

⚠️ No Changeset found

Latest commit: 068938c

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

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

@LauraBeatris
LauraBeatris requested review from brkalow, nikosdouvlis and panteliselef and removed request for nikosdouvlis June 21, 2024 19:08
@LauraBeatris
LauraBeatris marked this pull request as ready for review June 21, 2024 19:10
return clerkClient;
};

const clerkClient: ClerkClient & typeof clerkClientForRequest = Object.assign(

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Must update docs to call clerkClient() instead of clerkClient https://clerk.com/docs/references/nextjs/route-handlers#interact-with-clerks-backend-api

@LauraBeatris
LauraBeatris force-pushed the read-dynamic-keys-from-clerk-client branch 2 times, most recently from 7f14956 to 23f837e Compare June 21, 2024 19:15
let handlerResult: Response = NextResponse.next();
try {
handlerResult =
(await clerkClientStorage.run(

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

It was necessary to place another ALS context wrapped around the returned handler, otherwise clerkClient calls within the middleware handler (👇🏻 ) wouldn't have access to the store:

export default clerkMiddleware((auth)=> {
  clerkClient().users().get("...")
})

@LauraBeatris
LauraBeatris force-pushed the read-dynamic-keys-from-clerk-client branch from 23f837e to 1b83516 Compare June 21, 2024 19:17
* @deprecated
* This singleton is deprecated and will be removed in a future release. Please use `clerkClient()` as a function instead.
*/
const clerkClientSingletonProxy = new Proxy(clerkClientSingleton, {

@LauraBeatris LauraBeatris Jun 21, 2024

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Calling out for JS help 🙇🏻 We want to log a deprecation warning only if clerkClient gets called as a singleton.

The Proxy is being placed around the singleton reference here, but in line 90, it's properties are being merged with clerkClient() on Object.assign, therefore the deprecation warnings is being logged for both cases.

It's important to keep exporting clerkClient and allow it to be either a singleton or a function but to do so, it requires that Object.assign bit - is there another way to merge those properties while performing a proxy for the deprecation warning?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Do you mean that the usage of Object.assign() is causing the deprecation warning to be logged? Or because we're using the proxied client object under the hood for the non-dynamic keys case, it logs for both:

clerkClient().users.getUser()

clerkClient.users.getUser()

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

It's logging for both cases, and I think this is due to Object.assign since it's executing Proxy.get for every single property.

Comment thread packages/nextjs/src/server/clerkClient.ts
Comment on lines +51 to +54
get(target, prop, receiver) {
deprecated('clerkClient object', 'Use `clerkClient()` as a function instead.');

return Reflect.get(target, prop, receiver);
},

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

How does this work with nested properties? E.g. if I access clerkClient.users.getUser() will this still log the deprecation warning?

@LauraBeatris
LauraBeatris force-pushed the read-dynamic-keys-from-clerk-client branch from 152212d to 21a4f1e Compare June 24, 2024 21:55
@LauraBeatris
LauraBeatris force-pushed the read-dynamic-keys-from-clerk-client branch from a3fc154 to 7fd9a0f Compare June 25, 2024 19:47
@LauraBeatris
LauraBeatris force-pushed the read-dynamic-keys-from-clerk-client branch from 7fd9a0f to 4a4b66c Compare June 25, 2024 19:51
Comment thread integration/tests/dynamic-keys.test.ts Outdated
await app.teardown();
});

// test('redirects to `signInUrl` on `auth.protect`')

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Still have to implement those tests here, leaving this as a WIP, will continue to work today/tomorrow!

@LauraBeatris
LauraBeatris force-pushed the read-dynamic-keys-from-clerk-client branch from 49275a8 to 4a4b66c Compare June 26, 2024 13:44
@LauraBeatris
LauraBeatris force-pushed the read-dynamic-keys-from-clerk-client branch from 496de39 to b101927 Compare June 26, 2024 19:48
const PORT = 4199;

test.skip('Client handshake @generic', () => {
test.describe('Client handshake @generic', () => {

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

@nikosdouvlis I've went ahead and removed the skip from handshake tests here.

@LauraBeatris
LauraBeatris force-pushed the read-dynamic-keys-from-clerk-client branch from 2677ce2 to cda14e6 Compare June 26, 2024 22:25
@LauraBeatris
LauraBeatris force-pushed the read-dynamic-keys-from-clerk-client branch from cda14e6 to aa18e35 Compare June 26, 2024 22:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants