Skip to content

Commit

Permalink
Create guide for node sdk to express migration
Browse files Browse the repository at this point in the history
  • Loading branch information
EmmanouelaPothitou committed Jun 26, 2024
1 parent 7c58828 commit 00656c9
Show file tree
Hide file tree
Showing 3 changed files with 112 additions and 8 deletions.
7 changes: 5 additions & 2 deletions docs/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -1756,7 +1756,7 @@
{
"title": "Express.js",
"collapse": true,
"icon": "expressjs",
"icon": "nodejs",
"items": [
[
{ "title": "Overview", "href": "/docs/references/express/overview" },
Expand All @@ -1767,7 +1767,10 @@
{
"title": "Networkless token verification",
"href": "/docs/references/express/token-verification"
}
},
{
"title": "Migrating from Clerk Node SDK to Clerk Express",
"href": "/docs/references/express/migrate-from-node-to-express"}
]
]
},
Expand Down
10 changes: 4 additions & 6 deletions docs/references/express/available-methods.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,14 @@ All resource operations are mounted as sub-APIs on the `clerkClient` object. You

If you would like to use the default instance of `clerkClient` provided by the SDK, you can provide the `CLERK_SECRET_KEY` as an environment variable and instantiate `clerkClient` without passing configuration options.

<Tabs items={["ESM", "CJS"]}>
<Tab>
<CodeBlockTabs options={["ESM", "CJS"]}>

```js
import { clerkClient } from '@clerk/express';

const userList = await clerkClient.users.getUserList();
```
</Tab>

<Tab>
```js
const pkg = require('@clerk/express');
const clerkClient = pkg.default;
Expand All @@ -30,8 +28,8 @@ If you would like to use the default instance of `clerkClient` provided by the S
.then((sessions) => console.log(sessions))
.catch((error) => console.error(error));
```
</Tab>
</Tabs>

</CodeBlockTabs>

## Instantiate a custom `clerkClient` instance

Expand Down
103 changes: 103 additions & 0 deletions docs/references/express/migrate-from-node-to-express.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
---
title: Clerk Express.js SDK
description: Learn how to migrate from Node.js to Express.js.
---

# Clerk Express.js SDK

## Migrating from Clerk Node SDK to Clerk Express

<Steps>

### Uninstall @clerk/clerk-sdk-node and install @clerk/express

<CodeBlockTabs type="installer" options={["npm", "yarn", "pnpm"]}>
```sh filename="terminal"
npm uninstall @clerk/clerk-sdk-node
```

```sh filename="terminal"
yarn remove @clerk/clerk-sdk-node
```

```sh filename="terminal"
pnpm remove @clerk/clerk-sdk-node
```

</CodeBlockTabs>

<CodeBlockTabs type="installer" options={["npm", "yarn", "pnpm"]}>
```sh filename="terminal"
npm install @clerk/express
```

```sh filename="terminal"
yarn add @clerk/express
```

```sh filename="terminal"
pnpm add @clerk/express
```
</CodeBlockTabs>
### Require Authentication

**Using Node SDK**

To enforce strict authentication, use `ClerkExpressRequireAuth()`. This middleware will raise an error when an unauthenticated request is made.
You can find an example [here](/docs/backend-requests/handling/nodejs#clerk-express-require-auth).

**Using Express SDK**

- **Centralized Middleware:**
Use `clerkMiddleware` to authenticate all incoming requests without blocking them. This middleware will handle the handshake mechanism.
- **Require Authentication in Specific Routes or Handlers:**
Use `requireAuth()` to enforce authentication on specific routes. This middleware returns an HTTP 401 response for unauthenticated request.
```js
const express = require('express');
const { clerkMiddleware, requireAuth } = require('@clerk/clerk-express');

const app = express();

// Apply centralized middleware
app.use(clerkMiddleware());

// Define a protected route
app.get('/protected', requireAuth(), (req, res) => {
res.send('This is a protected route');
});

app.listen(3000, () => {
console.log('Server is running on port 3000');
});
```

### Get authentication state

**Using Node SDK**

To retrieve the authentication state, use `withAuth()` and `ClerkExpressWithAuth()`. You can find an example [here](/docs/backend-requests/handling/nodejs#clerk-express-with-auth).

**Using Express SDK**

To access the authentication state within your routes or handlers, use `getAuth()`.

```jsx

const express = require('express');
const { getAuth } = require('@clerk/clerk-express');

const app = express();

app.get('/auth-state', (req, res) => {
const authState = getAuth(req);
res.json(authState);
});

app.listen(3000, () => {
console.log('Server is running on port 3000');
});

```

---
</Steps>

0 comments on commit 00656c9

Please sign in to comment.