From f307b281557f499927be00cb7f514197ae7b2e16 Mon Sep 17 00:00:00 2001 From: Alexis Aguilar <98043211+alexisintech@users.noreply.github.com> Date: Mon, 18 Nov 2024 17:54:33 -0500 Subject: [PATCH] Fix instantiation of clerkclient in nextjs examples (#1718) Co-authored-by: victoria --- .../social-connections/oauth.mdx | 4 ++- docs/custom-flows/user-impersonation.mdx | 4 ++- docs/deployments/migrate-from-cognito.mdx | 2 +- docs/guides/add-onboarding-flow.mdx | 4 ++- .../backend/authenticate-request.mdx | 4 ++- docs/references/backend/overview.mdx | 4 ++- .../references/backend/sessions/get-token.mdx | 8 +++-- docs/references/backend/user/update-user.mdx | 4 ++- docs/references/nextjs/build-clerk-props.mdx | 4 ++- docs/references/nextjs/get-auth.mdx | 4 ++- docs/references/nextjs/read-session-data.mdx | 8 +++-- docs/references/nextjs/route-handlers.mdx | 4 ++- docs/users/creating-users.mdx | 4 ++- docs/users/deleting-users.mdx | 3 +- docs/users/metadata.mdx | 34 ++++++++++++++----- docs/users/web3.mdx | 4 ++- 16 files changed, 74 insertions(+), 25 deletions(-) diff --git a/docs/authentication/social-connections/oauth.mdx b/docs/authentication/social-connections/oauth.mdx index 80695dd1cd..91d9b7a0af 100644 --- a/docs/authentication/social-connections/oauth.mdx +++ b/docs/authentication/social-connections/oauth.mdx @@ -97,7 +97,9 @@ export async function GET() { // Get the OAuth access token for the user const provider = 'oauth_notion' - const clerkResponse = await clerkClient().users.getUserOauthAccessToken(userId, provider) + const client = await clerkClient() + + const clerkResponse = await client.users.getUserOauthAccessToken(userId, provider) const accessToken = clerkResponse[0].token || '' diff --git a/docs/custom-flows/user-impersonation.mdx b/docs/custom-flows/user-impersonation.mdx index 27ca2f9e22..4ac5a06f40 100644 --- a/docs/custom-flows/user-impersonation.mdx +++ b/docs/custom-flows/user-impersonation.mdx @@ -34,8 +34,10 @@ This guide will walk you through how to build a custom flow that handles user im return

You do not have permission to access this page.

} + const client = await clerkClient() + // Fetch list of application's users using Clerk's Backend SDK - const users = await clerkClient.users.getUserList() + const users = await client.users.getUserList() // This page needs to be a server component to use clerkClient.users.getUserList() // You must pass the list of users to the client for the rest of the logic diff --git a/docs/deployments/migrate-from-cognito.mdx b/docs/deployments/migrate-from-cognito.mdx index b722269e82..6715dd2499 100644 --- a/docs/deployments/migrate-from-cognito.mdx +++ b/docs/deployments/migrate-from-cognito.mdx @@ -75,7 +75,7 @@ The provided script below lists your Cognito user pool users, calls [^1]: [`CreateUser`](/docs/reference/backend-api/tag/Users#operation/CreateUser){{ target: '_blank' }} has a rate limit rule of 20 requests per 10 seconds. ```ts {{ title: '~/cognito_to_clerk/main.ts' }} -import { createClerkClient } from '@clerk/backend@' +import { createClerkClient } from '@clerk/backend' import * as IDP from '@aws-sdk/client-cognito-identity-provider' // NOTE: The IAM user should have permissions roughly equivalent to AmazonCognitoReadOnly. diff --git a/docs/guides/add-onboarding-flow.mdx b/docs/guides/add-onboarding-flow.mdx index 8306d431d9..0591f2a733 100644 --- a/docs/guides/add-onboarding-flow.mdx +++ b/docs/guides/add-onboarding-flow.mdx @@ -217,8 +217,10 @@ export const completeOnboarding = async (formData: FormData) => { return { message: 'No Logged In User' } } + const client = await clerkClient() + try { - const res = await clerkClient().users.updateUser(userId, { + const res = await client.users.updateUser(userId, { publicMetadata: { onboardingComplete: true, applicationName: formData.get('applicationName'), diff --git a/docs/references/backend/authenticate-request.mdx b/docs/references/backend/authenticate-request.mdx index debdc96f42..03561e3e06 100644 --- a/docs/references/backend/authenticate-request.mdx +++ b/docs/references/backend/authenticate-request.mdx @@ -153,8 +153,10 @@ It is recommended to set these options as [environment variables](/docs/deployme ```tsx import { clerkClient } from '@clerk/nextjs/server' +const client = await clerkClient() + export async function GET(req: Request) { - const { isSignedIn } = await clerkClient.authenticateRequest(req, { + const { isSignedIn } = await client.authenticateRequest(req, { authorizedParties: ['https://example.com'], }) diff --git a/docs/references/backend/overview.mdx b/docs/references/backend/overview.mdx index 1b3d25147d..10f7d6a699 100644 --- a/docs/references/backend/overview.mdx +++ b/docs/references/backend/overview.mdx @@ -367,7 +367,9 @@ To access the [properties](/docs/references/nextjs/auth-object#auth-object-prope return Response.json({ error: 'Unauthorized' }, { status: 401 }) } - const user = await (await clerkClient()).users.getUser(userId) + const client = await clerkClient() + + const user = await client.users.getUser(userId) return Response.json({ user }) } diff --git a/docs/references/backend/sessions/get-token.mdx b/docs/references/backend/sessions/get-token.mdx index c5394edbf9..5fc746d3a7 100644 --- a/docs/references/backend/sessions/get-token.mdx +++ b/docs/references/backend/sessions/get-token.mdx @@ -64,7 +64,9 @@ _Token { const template = 'test' - const token = await clerkClient().sessions.getToken(sessionId, template) + const client = await clerkClient() + + const token = await client.sessions.getToken(sessionId, template) console.log(token) /* @@ -90,7 +92,9 @@ _Token { const template = 'test' - const token = await clerkClient().sessions.getToken(sessionId, template) + const client = await clerkClient() + + const token = await client.sessions.getToken(sessionId, template) console.log(token) /* diff --git a/docs/references/backend/user/update-user.mdx b/docs/references/backend/user/update-user.mdx index acc25ca6df..20c1e0dbea 100644 --- a/docs/references/backend/user/update-user.mdx +++ b/docs/references/backend/user/update-user.mdx @@ -302,7 +302,9 @@ _User { // The user attributes to update const params = { firstName: 'John', lastName: 'Wick' } - const updatedUser = await clerkClient.users.updateUser(userId, params) + const client = await clerkClient() + + const updatedUser = await client.users.updateUser(userId, params) return res.status(200).json({ updatedUser }) } diff --git a/docs/references/nextjs/build-clerk-props.mdx b/docs/references/nextjs/build-clerk-props.mdx index 099a2d8263..7b06f5a61a 100644 --- a/docs/references/nextjs/build-clerk-props.mdx +++ b/docs/references/nextjs/build-clerk-props.mdx @@ -36,7 +36,9 @@ import { GetServerSideProps } from 'next' export const getServerSideProps: GetServerSideProps = async (ctx) => { const { userId } = getAuth(ctx.req) - const user = userId ? await clerkClient().users.getUser(userId) : undefined + const client = await clerkClient() + + const user = userId ? await client.users.getUser(userId) : undefined return { props: { ...buildClerkProps(ctx.req, { user }) } } } diff --git a/docs/references/nextjs/get-auth.mdx b/docs/references/nextjs/get-auth.mdx index f100536fdb..dd05ce81a4 100644 --- a/docs/references/nextjs/get-auth.mdx +++ b/docs/references/nextjs/get-auth.mdx @@ -99,7 +99,9 @@ import type { NextApiRequest, NextApiResponse } from 'next' export default async function handler(req: NextApiRequest, res: NextApiResponse) { const { userId } = getAuth(req) - const user = userId ? await clerkClient().users.getUser(userId) : null + const client = await clerkClient() + + const user = userId ? await client.users.getUser(userId) : null return res.status(200).json({}) } diff --git a/docs/references/nextjs/read-session-data.mdx b/docs/references/nextjs/read-session-data.mdx index 370f7678b3..7ab00a712c 100644 --- a/docs/references/nextjs/read-session-data.mdx +++ b/docs/references/nextjs/read-session-data.mdx @@ -132,7 +132,9 @@ Under the hood, `currentUser()` uses the [`clerkClient`](/docs/references/backen return res.status(401).json({ error: 'Unauthorized' }) } - const user = await clerkClient().users.getUser(userId) + const client = await clerkClient() + + const user = await client.users.getUser(userId) // use the user object to decide what data to return @@ -173,7 +175,9 @@ Under the hood, `currentUser()` uses the [`clerkClient`](/docs/references/backen export const getServerSideProps: GetServerSideProps = async (ctx) => { const { userId } = getAuth(ctx.req) - const user = userId ? await clerkClient.users.getUser(userId) : undefined + const client = await clerkClient() + + const user = userId ? await client.users.getUser(userId) : undefined return { props: { ...buildClerkProps(ctx.req, { user }) } } } diff --git a/docs/references/nextjs/route-handlers.mdx b/docs/references/nextjs/route-handlers.mdx index 50763c4633..7f238adccb 100644 --- a/docs/references/nextjs/route-handlers.mdx +++ b/docs/references/nextjs/route-handlers.mdx @@ -104,7 +104,9 @@ export async function POST(req: NextRequest) { const params = { firstName: 'John', lastName: 'Wick' } - const user = await clerkClient().users.updateUser(userId, params) + const client = await clerkClient() + + const user = await client.users.updateUser(userId, params) return NextResponse.json({ user }) } diff --git a/docs/users/creating-users.mdx b/docs/users/creating-users.mdx index fed78dd9ad..2049d1a868 100644 --- a/docs/users/creating-users.mdx +++ b/docs/users/creating-users.mdx @@ -26,7 +26,9 @@ To create users using the Clerk API, you can use the [`createUser()`](/docs/refe export async function POST() { try { - const user = await clerkClient.users.createUser({ + const client = await clerkClient() + + const user = await client.users.createUser({ emailAddress: ['test@example.com'], password: 'password', }) diff --git a/docs/users/deleting-users.mdx b/docs/users/deleting-users.mdx index 7f31693dbb..9c7cb7f5f4 100644 --- a/docs/users/deleting-users.mdx +++ b/docs/users/deleting-users.mdx @@ -27,7 +27,8 @@ To delete users using the Clerk API, you can use the [`deleteUser()`](/docs/refe const userId = 'user_123' try { - await clerkClient().users.deleteUser(userId) + const client = await clerkClient() + await client.users.deleteUser(userId) return NextResponse.json({ message: 'User deleted' }) } catch (error) { console.log(error) diff --git a/docs/users/metadata.mdx b/docs/users/metadata.mdx index 2060078bf9..6ed04c0041 100644 --- a/docs/users/metadata.mdx +++ b/docs/users/metadata.mdx @@ -29,7 +29,10 @@ Private metadata is only accessible by the backend, which makes this useful for export async function POST() { const { stripeId, userId } = await body.json() - await clerkClient.users.updateUserMetadata(userId, { + + const client = await clerkClient() + + await client.users.updateUserMetadata(userId, { privateMetadata: { stripeId: stripeId, }, @@ -45,7 +48,9 @@ Private metadata is only accessible by the backend, which makes this useful for export default async function handler(req: NextApiRequest, res: NextApiResponse) { const { stripeId, userId } = req.body - await clerkClient.users.updateUserMetadata(userId, { + const client = await clerkClient() + + await client.users.updateUserMetadata(userId, { privateMetadata: { stripeId: stripeId, }, @@ -138,7 +143,9 @@ You can retrieve the private metadata for a user by using the [`getUser`](/docs/ export async function GET(request: Request) { const { stripeId, userId } = await request.body.json() - const user = await clerkClient.users.getUser(userId) + const client = await clerkClient() + + const user = await client.users.getUser(userId) return NextResponse.json(user.privateMetadata) } ``` @@ -150,7 +157,9 @@ You can retrieve the private metadata for a user by using the [`getUser`](/docs/ export default async function handler(req: NextApiRequest, res: NextApiResponse) { const { userId } = await req.body.json() - const user = await clerkClient.users.getUser(userId) + const client = await clerkClient() + + const user = await client.users.getUser(userId) res.status(200).json(user.privateMetadata) } @@ -223,7 +232,9 @@ Public metadata is accessible by both the frontend and the backend, but can only export async function POST() { const { role, userId } = await body.json() - await clerkClient.users.updateUserMetadata(userId, { + const client = await clerkClient() + + await client.users.updateUserMetadata(userId, { publicMetadata: { role, }, @@ -239,7 +250,9 @@ Public metadata is accessible by both the frontend and the backend, but can only export default async function handler(req: NextApiRequest, res: NextApiResponse) { const { role, userId } = req.body - await clerkClient.users.updateUserMetadata(userId, { + const client = await clerkClient() + + await client.users.updateUserMetadata(userId, { publicMetadata: { role, }, @@ -349,7 +362,10 @@ Updating this value overrides the previous value; it does not merge. To perform export async function POST() { const { stripeId, userId } = await body.json() - await clerkClient.users.updateUserMetadata(userId, { + + const client = await clerkClient() + + await client.users.updateUserMetadata(userId, { unsafeMetadata: { birthday: '11-30-1969', }, @@ -365,7 +381,9 @@ Updating this value overrides the previous value; it does not merge. To perform export default async function handler(req: NextApiRequest, res: NextApiResponse) { const { stripeId, userId } = req.body - await clerkClient.users.updateUserMetadata(userId, { + const client = await clerkClient() + + await client.users.updateUserMetadata(userId, { unsafeMetadata: { birthday: '11-30-1969', }, diff --git a/docs/users/web3.mdx b/docs/users/web3.mdx index aa3200951a..93d24d8298 100644 --- a/docs/users/web3.mdx +++ b/docs/users/web3.mdx @@ -75,7 +75,9 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse) return res.status(401).json({ error: 'Unauthorized' }) } - const user = userId ? await clerkClient.users.getUser(userId) : null + const client = await clerkClient() + + const user = userId ? await client.users.getUser(userId) : null // Retrieve the user's web3Wallet const walletId = user ? user.primaryWeb3WalletId : null