Skip to content

Commit

Permalink
Fix instantiation of clerkclient in nextjs examples (#1718)
Browse files Browse the repository at this point in the history
Co-authored-by: victoria <[email protected]>
  • Loading branch information
alexisintech and victoriaxyz authored Nov 18, 2024
1 parent d7c4365 commit f307b28
Show file tree
Hide file tree
Showing 16 changed files with 74 additions and 25 deletions.
4 changes: 3 additions & 1 deletion docs/authentication/social-connections/oauth.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -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 || ''

Expand Down
4 changes: 3 additions & 1 deletion docs/custom-flows/user-impersonation.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,10 @@ This guide will walk you through how to build a custom flow that handles user im
return <p>You do not have permission to access this page.</p>
}

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
Expand Down
2 changes: 1 addition & 1 deletion docs/deployments/migrate-from-cognito.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
4 changes: 3 additions & 1 deletion docs/guides/add-onboarding-flow.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -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'),
Expand Down
4 changes: 3 additions & 1 deletion docs/references/backend/authenticate-request.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -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'],
})
Expand Down
4 changes: 3 additions & 1 deletion docs/references/backend/overview.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -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 })
}
Expand Down
8 changes: 6 additions & 2 deletions docs/references/backend/sessions/get-token.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -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)
/*
Expand All @@ -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)
/*
Expand Down
4 changes: 3 additions & 1 deletion docs/references/backend/user/update-user.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -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 })
}
Expand Down
4 changes: 3 additions & 1 deletion docs/references/nextjs/build-clerk-props.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -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 }) } }
}
Expand Down
4 changes: 3 additions & 1 deletion docs/references/nextjs/get-auth.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -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({})
}
Expand Down
8 changes: 6 additions & 2 deletions docs/references/nextjs/read-session-data.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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 }) } }
}
Expand Down
4 changes: 3 additions & 1 deletion docs/references/nextjs/route-handlers.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -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 })
}
Expand Down
4 changes: 3 additions & 1 deletion docs/users/creating-users.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -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: ['[email protected]'],
password: 'password',
})
Expand Down
3 changes: 2 additions & 1 deletion docs/users/deleting-users.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
34 changes: 26 additions & 8 deletions docs/users/metadata.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -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,
},
Expand All @@ -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,
},
Expand Down Expand Up @@ -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)
}
```
Expand All @@ -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)
}
Expand Down Expand Up @@ -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,
},
Expand All @@ -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,
},
Expand Down Expand Up @@ -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',
},
Expand All @@ -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',
},
Expand Down
4 changes: 3 additions & 1 deletion docs/users/web3.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit f307b28

Please sign in to comment.