Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Clerk Express SDK docs #1201

Merged
merged 82 commits into from
Sep 30, 2024
Merged
Show file tree
Hide file tree
Changes from 78 commits
Commits
Show all changes
82 commits
Select commit Hold shift + click to select a range
8e764ed
Create docs/references/express
EmmanouelaPothitou Jun 24, 2024
e19c50b
Create guide for node sdk to express migration
EmmanouelaPothitou Jun 26, 2024
bc1c359
Update clerk express SDK docs
EmmanouelaPothitou Jun 30, 2024
a79d032
remove the removed pages; update overview
alexisintech Jul 17, 2024
ab1ea30
remove pages from sidenav
alexisintech Jul 17, 2024
51f4fcb
remove error handling section; fix clerkmiddleware explanation
alexisintech Jul 18, 2024
bc5d9e8
add requireauth reference doc
alexisintech Jul 23, 2024
fe743b5
update migration guide
alexisintech Jul 23, 2024
817eba8
address jeff's suggestions
alexisintech Jul 23, 2024
e236fa6
Update docs/references/express/migrate-from-node-to-express.mdx
alexisintech Jul 23, 2024
e9ffbaa
update available methods
alexisintech Jul 23, 2024
afc9a31
add example to requireauth
alexisintech Jul 23, 2024
0c75f1c
add getauth example
alexisintech Jul 23, 2024
790d818
requireauth is not invoked
alexisintech Jul 25, 2024
efa237f
fix formatting
alexisintech Jul 25, 2024
31cce6a
fix env var
alexisintech Jul 31, 2024
887d292
new migration guide in progress take 1
alexisintech Aug 1, 2024
0b42c29
replace node with express in backend sdk overview
alexisintech Aug 2, 2024
da6eca0
remove dedicated requireauth page; add code example for clerkclient; …
alexisintech Aug 2, 2024
f239633
update migration guide
alexisintech Aug 2, 2024
7b76cf4
final edits
alexisintech Aug 2, 2024
3ed3058
fix broke link
alexisintech Aug 2, 2024
4ff3a22
fix broken link
alexisintech Aug 2, 2024
e0a3f53
update overview
alexisintech Aug 2, 2024
874520a
Update the icon name for navbar icon
EmmanouelaPothitou Aug 6, 2024
89159a2
docs(express): Fix conflicts
wobsoriano Sep 19, 2024
a18f034
docs(express): Add clerk secret key process env
wobsoriano Sep 19, 2024
13173fd
docs(express): Add initial Express quickstart page
wobsoriano Sep 19, 2024
1941a93
docs(express): Add missing parentheses
wobsoriano Sep 19, 2024
3af92d0
docs(express): Quickstart dotenv clean ups
wobsoriano Sep 19, 2024
bf2ed14
docs(express): Fix getAuth example
wobsoriano Sep 19, 2024
49fef65
docs(express): Add clerk middleware page reference
wobsoriano Sep 19, 2024
33a2ad9
docs(express): Update migration guide
wobsoriano Sep 19, 2024
b141dd5
docs(express): Remove extra files
wobsoriano Sep 19, 2024
314a404
docs(express): Use new diffing snippets
wobsoriano Sep 19, 2024
2362d61
docs(express): Move migration guide to upgrade guides section
wobsoriano Sep 19, 2024
e9cdcd1
docs(express): Fix diffs
wobsoriano Sep 19, 2024
bc80ff3
docs(express): Add next steps to quickstart
wobsoriano Sep 19, 2024
35ac28b
docs(express): Add back methods to overview page
wobsoriano Sep 20, 2024
a333118
docs(express): Add missing requireAuth imports
wobsoriano Sep 20, 2024
471c0e7
docs(express): Add error middleware to getAuth example
wobsoriano Sep 20, 2024
5b4d7d1
docs(express): Add create first user section
wobsoriano Sep 20, 2024
8e00380
docs(express): Revert previous commit
wobsoriano Sep 20, 2024
ee31a1c
docs(express): Clean up quickstart
wobsoriano Sep 20, 2024
38d960d
docs(express): Add more context to quickstart
wobsoriano Sep 20, 2024
5971b3c
docs(express): Update ports
wobsoriano Sep 20, 2024
e497eb9
docs(express): Update incorrect property
wobsoriano Sep 20, 2024
e564feb
docs(express): Revert to previous updates with corrections
wobsoriano Sep 20, 2024
1779157
Update docs/quickstarts/express.mdx
alexisintech Sep 25, 2024
adf283b
Update docs/quickstarts/express.mdx
alexisintech Sep 25, 2024
f42248b
Update docs/quickstarts/express.mdx
alexisintech Sep 25, 2024
b9ffce6
Update docs/quickstarts/express.mdx
alexisintech Sep 25, 2024
900c7d7
remove type=installer from codeblocks
alexisintech Sep 25, 2024
085d1a9
fix copy
alexisintech Sep 25, 2024
3387f1d
Update docs/quickstarts/express.mdx
alexisintech Sep 25, 2024
b2f790f
format
alexisintech Sep 25, 2024
2fa3a59
updates to copy
alexisintech Sep 25, 2024
03a2105
chore: update docs/quickstarts/express.mdx
wobsoriano Sep 26, 2024
c2f57ef
docs(express): Remove .local extension in env var extension
wobsoriano Sep 26, 2024
873b629
docs(express): Update docs/upgrade-guides/node-to-express.mdx
wobsoriano Sep 26, 2024
91ed061
docs(express): Update docs/upgrade-guides/node-to-express.mdx
wobsoriano Sep 26, 2024
54db639
docs(express): Update docs/references/express/overview.mdx
wobsoriano Sep 26, 2024
5ae3f9f
docs(express): Update docs/references/express/overview.mdx
wobsoriano Sep 26, 2024
cdf9517
docs(express): Update docs/references/express/overview.mdx
wobsoriano Sep 26, 2024
f3886b4
docs(express): Run format
wobsoriano Sep 26, 2024
0aec049
docs(express): Replace lax middleware text to something more descriptive
wobsoriano Sep 26, 2024
19d45df
docs(express): Update quickstart with refactored middlewares
wobsoriano Sep 27, 2024
1e0c5d0
docs(express): More quickstart improvements
wobsoriano Sep 27, 2024
b7bbd35
docs(express): Update overview
wobsoriano Sep 27, 2024
b7d3cef
docs(express): Upgrade migration guide to match refactored middlewares
wobsoriano Sep 27, 2024
cf732b2
docs(express): Run format
wobsoriano Sep 27, 2024
337cefb
Update docs/upgrade-guides/node-to-express.mdx
wobsoriano Sep 28, 2024
8734c18
Update docs/upgrade-guides/node-to-express.mdx
wobsoriano Sep 28, 2024
b1b5fbb
Update docs/upgrade-guides/node-to-express.mdx
wobsoriano Sep 28, 2024
4a37ff5
Update docs/references/express/overview.mdx
wobsoriano Sep 28, 2024
cd8818c
Update docs/references/express/overview.mdx
wobsoriano Sep 28, 2024
3c11b23
Update docs/references/express/overview.mdx
wobsoriano Sep 28, 2024
36321c6
docs(express): Run format and update requrieAuth error
wobsoriano Sep 28, 2024
de11246
Update docs/quickstarts/express.mdx
wobsoriano Sep 30, 2024
f1893a0
Update docs/references/express/overview.mdx
wobsoriano Sep 30, 2024
7f5caf0
Update docs/references/express/overview.mdx
wobsoriano Sep 30, 2024
82c45a9
docs(express): Run format
wobsoriano Sep 30, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions docs/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,10 @@
"title": "Backend",
"items": [
[
{
"title": "Express",
"href": "/docs/quickstarts/express"
},
{
"title": "Fastify",
"href": "/docs/quickstarts/fastify"
Expand Down Expand Up @@ -1169,6 +1173,17 @@
]
]
},
{
"title": "Upgrading from Node to Express SDK",
"items": [
[
{
"title": "Overview",
"href": "/docs/upgrade-guides/node-to-express"
}
]
]
},
{
"title": "Dashboard",
"items": [
Expand Down Expand Up @@ -2163,6 +2178,12 @@
]
]
},
{
"title": "Express",
"collapse": true,
"icon": "expressjs",
"items": [[{ "title": "Overview", "href": "/docs/references/express/overview" }]]
},
{
"title": "Remix",
"collapse": true,
Expand Down
1 change: 1 addition & 0 deletions docs/manifest.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@
"cog-6-teeth",
"door",
"elysia",
"expressjs",
"globe",
"go",
"home",
Expand Down
170 changes: 170 additions & 0 deletions docs/quickstarts/express.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,170 @@
---
title: Express Quickstart
description: Learn how to use Clerk to quickly and easily add secure authentication and user management to your Express server.
---

<TutorialHero
framework="express"
exampleRepo={[
{
title: "Express Quickstart Repo",
link: "https://github.com/clerk/clerk-express-quickstart"
}
]}
beforeYouStart={[
{
title: "Set up a Clerk application",
link: "/docs/quickstarts/setup-clerk",
icon: "clerk",
},
{
title: "Create a Express application",
link: "https://expressjs.com/en/starter/installing.html",
icon: "expressjs",
}
]}
>
- Install `@clerk/express`
- Set your Clerk API keys
- Add `clerkMiddleware()` to your application
- Protect your routes using `requireAuth()`
</TutorialHero>

Learn how to integrate Clerk into your Express backend for secure user authentication and management. This guide covers backend implementation only and requires a Clerk frontend SDK in order for any of this to work.

jescalan marked this conversation as resolved.
Show resolved Hide resolved
<Steps>
### Install `@clerk/express`

Clerk's [Express SDK](/docs/references/express/overview) ships with a variety of helpers for the backend to make user authentication easier.

To get started using Clerk with Express, add the SDK to your project:

<CodeBlockTabs options={["npm", "yarn", "pnpm"]}>
```bash {{ filename: 'terminal' }}
npm install @clerk/express
```

```bash {{ filename: 'terminal' }}
yarn add @clerk/express
```

```bash {{ filename: 'terminal' }}
pnpm add @clerk/express
```
</CodeBlockTabs>

### Set your Clerk API keys

<SignedIn>
Add the following keys to your `.env` file. These keys can always be retrieved from the [API Keys](https://dashboard.clerk.com/last-active?path=api-keys) page of your Clerk Dashboard.
</SignedIn>

<SignedOut>
1. Navigate to the Clerk Dashboard.
1. In the navigation sidebar, select [API Keys](https://dashboard.clerk.com/last-active?path=api-keys).
1. In the **Quick Copy** section, copy your Clerk publishable and secret key.
1. Paste your keys into your `.env` file.

The final result should resemble the following:
</SignedOut>

```sh {{ filename: '.env' }}
CLERK_PUBLISHABLE_KEY={{pub_key}}
CLERK_SECRET_KEY={{secret}}
```

This guide uses `dotenv` to load the environment variables. Install the package by running the following command:

<CodeBlockTabs options={["npm", "yarn", "pnpm"]}>
```bash {{ filename: 'terminal' }}
npm install dotenv
```

```bash {{ filename: 'terminal' }}
yarn add dotenv
```

```bash {{ filename: 'terminal' }}
pnpm add dotenv
```
</CodeBlockTabs>

### Add `clerkMiddleware()` to your application

The [`clerkMiddleware()`](/docs/references/express/overview#clerk-middleware) function checks the request's cookies and headers for a session JWT and, if found, attaches the [Auth](/docs/references/nextjs/auth-object#auth-object) object to the request object under the `auth` key.

```ts {{ filename: 'index.ts', mark: [3, 7] }}
import 'dotenv/config'
import express from 'express'
import { clerkMiddleware } from '@clerk/express'

const app = express()

app.use(clerkMiddleware())

app.listen(3000, () => {
console.log(`Example app listening at http://localhost:${PORT}`)
})
```

You can then use the [`getAuth`](/docs/references/express/overview#get-auth) helper to access the authentication state in any route:

```ts
import { getAuth } from '@clerk/express'

app.get('/auth', (req, res) => {
res.json({ auth: getAuth(req) })
})
```

### Protect your routes using `requireAuth()`

To protect your routes, use the [`requireAuth`](/docs/references/express/overview#require-auth) middleware. This middleware functions similarly to `clerkMiddleware()`, but also protects your routes by redirecting unauthenticated users to the sign-in page.

In this example, if the user is authenticated, it retrieves the `userId` using the `getAuth()` helper and uses it with `clerkClient.users.getUser()` to fetch the current user's `User` object. If the user is not authenticated, they are redirected to the '/sign-in' route.

```ts {{ filename: 'index.ts', mark: [3, [7, 11], [13, 16]] }}
import 'dotenv/config'
import express from 'express'
import { clerkClient, getAuth, requireAuth } from '@clerk/express'

const app = express()

app.get('/protected', requireAuth({ signInUrl: '/sign-in' }), (req, res) => {
const { userId } = getAuth(req)
const user = await clerkClient.users.getUser()
return res.json({ user })
})

app.get('/sign-in', (req, res) => {
// Assuming you have a template engine installed
wobsoriano marked this conversation as resolved.
Show resolved Hide resolved
res.render('sign-in')
})

app.listen(3000, () => {
console.log(`Example app listening at http://localhost:${PORT}`)
})
```
</Steps>

## Next steps

<Cards>
- [Use middleware to protect routes](/docs/references/express/overview#require-auth)
- Learn how to protect specific routes from unauthenticated users.

---

- [Protect routes based on authorization status](/docs/references/express/overview#get-auth)
- Learn how to protect a route based on both authentication and authorization status.

---

- [Express SDK reference](/docs/references/express/overview)
- Learn more about additional Express SDK methods.

---

- [Deploy to Production](/docs/deployments/overview)
- Learn how to deploy your Clerk app to production.
</Cards>
22 changes: 7 additions & 15 deletions docs/references/backend/overview.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ To access a resource, you must first instantiate a `clerkClient` instance.

To use the default `clerkClient` instance, set your `CLERK_SECRET_KEY` [environment variable](/docs/deployments/clerk-environment-variables#clerk-publishable-and-secret-keys) and then import the `clerkClient` instance from the SDK as shown in the following example:

<Tabs type="framework" items={["Next.js", "Remix", "Fastify", "Astro", "Node"]}>
<Tabs items={["Next.js", "Remix", "Fastify", "Astro", "Express"]}>
<Tab>
```jsx
import { clerkClient } from '@clerk/nextjs/server'
Expand All @@ -85,7 +85,7 @@ To access a resource, you must first instantiate a `clerkClient` instance.

<Tab>
```js
import { clerkClient } from '@clerk/clerk-sdk-node'
import { clerkClient } from '@clerk/express'
```
</Tab>
</Tabs>
Expand All @@ -94,7 +94,7 @@ To access a resource, you must first instantiate a `clerkClient` instance.

If you would like to customize the behavior of the JavaScript Backend SDK, you can instantiate a `clerkClient` instance yourself by calling `createClerkClient()` and passing in [`options`](#create-clerk-client-options).

<Tabs type="framework" items={["Next.js", "Remix", "Fastify", "Astro", "Tanstack Start", "Node"]}>
<Tabs type="framework" items={["Next.js", "Remix", "Fastify", "Astro", "Tanstack Start", "Express"]}>
<Tab>
```jsx
import { createClerkClient } from '@clerk/nextjs/server'
Expand Down Expand Up @@ -224,17 +224,17 @@ To access a resource, you must first instantiate a `clerkClient` instance.
<Tab>
<CodeBlockTabs options={["ESM", "CJS"]}>
```js
import { createClerkClient } from '@clerk/clerk-sdk-node'
import { createClerkClient } from '@clerk/express'

const clerkClient = createClerkClient({ secretKey: '{{secret}}' })
const clerkClient = createClerkClient({ secretKey: process.env.CLERK_SECRET_KEY })

const userList = await clerkClient.users.getUserList()
```

```js
const Clerk = require('@clerk/clerk-sdk-node/cjs/instance').default
const Clerk = require('@clerk/express')

const clerkClient = Clerk({ secretKey: '{{secret}}' })
const clerkClient = Clerk.createClerkClient({ secretKey: process.env.CLERK_SECRET_KEY })

clerkClient.sessions
.getSessionList()
Expand All @@ -245,14 +245,6 @@ To access a resource, you must first instantiate a `clerkClient` instance.
</Tab>
</Tabs>
</Tab>

<Tab>
The Backend SDK is accessible via the `clerkClient` function if you are using Astro.

```js
import { clerkClient } from '@clerk/astro/server'
```
</Tab>
</Tabs>

## Error handling
Expand Down
Loading
Loading