-
Notifications
You must be signed in to change notification settings - Fork 447
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create guide for node sdk to express migration
- Loading branch information
1 parent
7c58828
commit 00656c9
Showing
3 changed files
with
112 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
103 changes: 103 additions & 0 deletions
103
docs/references/express/migrate-from-node-to-express.mdx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |