Skip to content

Commit

Permalink
docs: Replace Node SDK examples with Express SDK
Browse files Browse the repository at this point in the history
  • Loading branch information
wobsoriano committed Jan 10, 2025
1 parent 73c8821 commit 192f515
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 92 deletions.
36 changes: 13 additions & 23 deletions docs/references/backend/sessions/get-token.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ _Token {

## Examples with frameworks

<Tabs items={["Next.js", "Node", "Remix"]}>
<Tabs items={["Next.js", "Express", "Remix"]}>
<Tab>
<CodeBlockTabs options={["App Router", "Pages Router"]}>
```js {{ filename: 'app/api/get-token-example/route.ts' }}
Expand Down Expand Up @@ -110,39 +110,29 @@ _Token {
</Tab>
<Tab>
> [!CAUTION]
> On January 8, 2025, the Node SDK will no longer be available. [Upgrade to the Express SDK](/docs/upgrade-guides/node-to-express).
{/* TODO: Update Node example - SDK is being deprecated */}
```js {{ filename: 'getToken.ts' }}
import { clerkClient } from '@clerk/clerk-sdk-node'

app.post(
'/api/get-token',
// ClerkExpressRequireAuth returns an error for unauthorized requests
ClerkExpressRequireAuth(),
import { clerkClient } from '@clerk/express'

// Optionally ClerkExpressWithAuth returns an empty user with no error
// ClerkExpressWithAuth(),
app.post('/api/get-token', async (req, res) => {
const sessionId = req.auth.sessionId

async (req, res) => {
const sessionId = req.auth.sessionId
if (!sessionId) {
return res.status(401).json({ error: 'Unauthorized' })
}

const template = 'test'
const template = 'test'

const token = await clerkClient.sessions.getToken(sessionId, template)
const token = await clerkClient.sessions.getToken(sessionId, template)

console.log(token)
/*
console.log(token)
/*
_Token {
jwt: 'eyJhbG...'
}
*/

res.json({ token })
},
)
res.json({ token })
})
```
</Tab>
Expand Down
9 changes: 2 additions & 7 deletions docs/users/creating-users.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ To create users in the Clerk Dashboard:

To create users using the Clerk API, you can use the [`createUser()`](/docs/references/backend/user/create-user) method from the `users` sub-api of the `clerkClient` instance.

<Tabs items={["Next.js", "Node", "cURL"]}>
<Tabs items={["Next.js", "Express", "cURL"]}>
<Tab>
```ts {{ filename: 'app/api/create-user/route.ts' }}
import { clerkClient } from '@clerk/nextjs/server'
Expand All @@ -41,13 +41,8 @@ To create users using the Clerk API, you can use the [`createUser()`](/docs/refe
</Tab>

<Tab>
> [!CAUTION]
> On January 8, 2025, the Node SDK will no longer be available. [Upgrade to the Express SDK](/docs/upgrade-guides/node-to-express).
{/* TODO: Update Node example - SDK is being deprecated */}

```ts {{ filename: 'create-user.ts' }}
import { clerkClient } from '@clerk/clerk-sdk-node'
import { clerkClient } from '@clerk/express'

app.post('/createUser', async (req, res) => {
const userData = req.body
Expand Down
11 changes: 3 additions & 8 deletions docs/users/deleting-users.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ To delete users in the Clerk Dashboard:

To delete users using the Clerk API, you can use the [`deleteUser()`](/docs/references/backend/user/delete-user) method from the `users` sub-api of the `clerkClient` instance.

<Tabs items={["Next.js", "Node", "cURL"]}>
<Tabs items={["Next.js", "Express", "cURL"]}>
<Tab>
```ts {{ filename: 'app/api/delete-user/route.ts' }}
import { clerkClient } from '@clerk/nextjs/server'
Expand All @@ -38,15 +38,10 @@ To delete users using the Clerk API, you can use the [`deleteUser()`](/docs/refe
</Tab>

<Tab>
> [!CAUTION]
> On January 8, 2025, the Node SDK will no longer be available. [Upgrade to the Express SDK](/docs/upgrade-guides/node-to-express).
{/* TODO: Update Node example - SDK is being deprecated */}

```ts {{ filename: 'delete-user.ts' }}
import { clerkClient } from '@clerk/clerk-sdk-node'
import { clerkClient } from '@clerk/express'

app.post('/deleteUser', (req, res) => {
app.post('/deleteUser', async (req, res) => {
const userId = req.body.userId

try {
Expand Down
44 changes: 12 additions & 32 deletions docs/users/metadata.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ Private metadata is only accessible by the backend, which makes this useful for

### Set private metadata

<Tabs items={["Next.js", "Node", "Go", "Ruby", "cURL"]}>
<Tabs items={["Next.js", "Express", "Go", "Ruby", "cURL"]}>
<Tab>
```ts {{ filename: 'app/private/route.ts' }}
import { NextRequest, NextResponse } from 'next/server'
Expand All @@ -43,16 +43,11 @@ Private metadata is only accessible by the backend, which makes this useful for
</Tab>

<Tab>
> [!CAUTION]
> On January 8, 2025, the Node SDK will no longer be available. [Upgrade to the Express SDK](/docs/upgrade-guides/node-to-express).
{/* TODO: Update Node example - SDK is being deprecated */}

```ts {{ filename: 'private.ts' }}
import { clerkClient } from '@clerk/clerk-sdk-node'
import { clerkClient } from '@clerk/express'

app.post('/updateStripe', async (req, res) => {
const { stripeId, userId } = await body.json()
const { stripeId, userId } = req.body
await clerkClient.users.updateUserMetadata(userId, {
privateMetadata: {
stripeId: stripeId,
Expand Down Expand Up @@ -113,7 +108,7 @@ Private metadata is only accessible by the backend, which makes this useful for

You can retrieve the private metadata for a user by using the JavaScript Backend SDK's [`getUser()`](/docs/references/backend/user/get-user) method. This method will return the `User` object which contains the private metadata.

<Tabs items={["Next.js", "Node", "cURL", "Go", "Ruby"]}>
<Tabs items={["Next.js", "Express", "cURL", "Go", "Ruby"]}>
<Tab>
```ts {{ filename: 'app/private/route.ts' }}
import { NextRequest, NextResponse } from 'next/server'
Expand All @@ -132,16 +127,11 @@ You can retrieve the private metadata for a user by using the JavaScript Backend
</Tab>

<Tab>
> [!CAUTION]
> On January 8, 2025, the Node SDK will no longer be available. [Upgrade to the Express SDK](/docs/upgrade-guides/node-to-express).
{/* TODO: Update Node example - SDK is being deprecated */}

```ts {{ filename: 'private.ts' }}
import { clerkClient } from '@clerk/clerk-sdk-node'
import { clerkClient } from '@clerk/express'

app.post('/updateStripe', async (req, res) => {
const { userId } = await req.body.json()
const { userId } = req.body

const user = await clerkClient.users.getUser(userId)

Expand Down Expand Up @@ -186,7 +176,7 @@ Public metadata is accessible by both the frontend and the backend, but can only

### Set public metadata

<Tabs items={["Next.js", "Node", "Go", "Ruby", "cURL"]}>
<Tabs items={["Next.js", "Express", "Go", "Ruby", "cURL"]}>
<Tab>
```ts {{ filename: 'app/public/route.ts' }}
import { NextRequest, NextResponse } from 'next/server'
Expand All @@ -209,15 +199,10 @@ Public metadata is accessible by both the frontend and the backend, but can only
</Tab>

<Tab>
> [!CAUTION]
> On January 8, 2025, the Node SDK will no longer be available. [Upgrade to the Express SDK](/docs/upgrade-guides/node-to-express).
{/* TODO: Update Node example - SDK is being deprecated */}

```ts {{ filename: 'public.ts' }}
import { clerkClient } from '@clerk/clerk-sdk-node'
import { clerkClient } from '@clerk/express'

app.post('/updateRole', (req, res) => {
app.post('/updateRole', async (req, res) => {
const { role, userId } = req.body

await clerkClient.users.updateUserMetadata(userId, {
Expand Down Expand Up @@ -297,7 +282,7 @@ The following examples demonstrate how to update `unsafeMetadata` using [the Bac

#### Using the Backend API

<Tabs items={["Next.js", "Node", "Go", "Ruby", "cURL"]}>
<Tabs items={["Next.js", "Express", "Go", "Ruby", "cURL"]}>
<Tab>
```ts {{ filename: 'app/unsafe/route.ts' }}
import { NextRequest, NextResponse } from 'next/server'
Expand All @@ -320,16 +305,11 @@ The following examples demonstrate how to update `unsafeMetadata` using [the Bac
</Tab>

<Tab>
> [!CAUTION]
> On January 8, 2025, the Node SDK will no longer be available. [Upgrade to the Express SDK](/docs/upgrade-guides/node-to-express).
{/* TODO: Update Node example - SDK is being deprecated */}

```ts {{ filename: 'private.ts' }}
import { clerkClient } from '@clerk/clerk-sdk-node'
import { clerkClient } from '@clerk/express'

app.post('/updateStripe', async (req, res) => {
const { stripeId, userId } = await body.json()
const { stripeId, userId } = await req.body
await clerkClient.users.updateUserMetadata(userId, {
unsafeMetadata: {
birthday: '11-30-1969',
Expand Down
34 changes: 12 additions & 22 deletions docs/users/user-impersonation.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ To detect impersonated sessions in the frontend, the `actor` object contains the

### Detect impersonated sessions in the backend

<Tabs items={["Next.js", "Node"]}>
<Tabs items={["Next.js", "Express"]}>
<Tab>
The Next.js [`auth()`](/docs/references/nextjs/auth) helper provides the `actor` object in the authentication context.

Expand All @@ -146,33 +146,23 @@ To detect impersonated sessions in the frontend, the `actor` object contains the
</Tab>

<Tab>
> [!CAUTION]
> On January 8, 2025, the Node SDK will no longer be available. [Upgrade to the Express SDK](/docs/upgrade-guides/node-to-express).
{/* TODO: Update Node example - SDK is being deprecated */}

The Node.js SDK provides a [middleware](/docs/backend-requests/handling/nodejs) that augments the request object with the authentication context.
The Express SDK provides a [middleware](/docs/references/express/overview#clerk-middleware) that augments the request object with the authentication context.

```js
import express from 'express'
import { ClerkExpressWithAuth } from '@clerk/clerk-sdk-node'
import { clerkMiddleware } from '@clerk/express'

const app = express()

// Apply the Clerk express middleware
app.get(
'/protected-endpoint',
ClerkExpressWithAuth({
// ...options
}),
(req, res) => {
// The request object is augmented with the
// Clerk authentication context.
const { userId, actor } = req.auth

res.json({ userId, actor })
},
)
app.use(clerkMiddleware())

app.get('/protected-endpoint', (req, res) => {
// The request object is augmented with the
// Clerk authentication context.
const { userId, actor } = req.auth

res.json({ userId, actor })
})

app.listen(3000, () => {
console.log('Booted.')
Expand Down

0 comments on commit 192f515

Please sign in to comment.