|
| 1 | +--- |
| 2 | +description: Integrating with Clerk. |
| 3 | +sidebar_position: 2 |
| 4 | +sidebar_label: Clerk |
| 5 | +--- |
| 6 | + |
| 7 | +# Clerk Integration |
| 8 | + |
| 9 | +[Clerk](https://clerk.com/) is a comprehensive authentication and user management platform, providing both APIs and pre-made UI components. This guide will show you how to integrate Clerk with ZenStack's [access control system](../../orm/access-control/). |
| 10 | + |
| 11 | +## Set up Clerk |
| 12 | + |
| 13 | +First, follow Clerk's [quick start guides](https://clerk.com/docs/quickstarts/overview) to set up your project if you haven't already. |
| 14 | + |
| 15 | +## Adjust your ZModel |
| 16 | + |
| 17 | +Since Clerk manages both user authentication and storage, you don't need to store users in your database anymore. However, you still need to provide a type that the `auth()` function can resolve to. Instead of using a regular model, we can declare a `type` instead: |
| 18 | + |
| 19 | +You can include any field you want in the `User` type, as long as you provide the same set of fields in the context object when calling `ZenStackClient`'s `$setAuth()` method. |
| 20 | + |
| 21 | +The following code shows an example blog post schema: |
| 22 | + |
| 23 | +```zmodel |
| 24 | +type User { |
| 25 | + id String @id |
| 26 | +
|
| 27 | + @@auth |
| 28 | +} |
| 29 | +
|
| 30 | +model Post { |
| 31 | + id String @id @default(cuid()) |
| 32 | + createdAt DateTime @default(now()) |
| 33 | + updatedAt DateTime @updatedAt |
| 34 | + title String |
| 35 | + published Boolean @default(false) |
| 36 | + authorId String // stores Clerk's user ID |
| 37 | +
|
| 38 | + // author has full access |
| 39 | + @@allow('all', auth() != null && auth().id == authorId) |
| 40 | +
|
| 41 | + // logged-in users can view published posts |
| 42 | + @@allow('read', auth() != null && published) |
| 43 | +} |
| 44 | +``` |
| 45 | + |
| 46 | +If you choose to [synchronize user data to your database](https://clerk.com/docs/users/sync-data-to-your-backend), you can define `User` as a regular `model` since it's then backed by a database table. |
| 47 | + |
| 48 | +## Create a user-bound ORM client |
| 49 | + |
| 50 | +When using ZenStack's built-in access control, you often use the `auth()` function in policy rules to reference the current user's identity. The evaluation of `auth()` at runtime requires you to call the `$setAuth()` method and pass in the validated user identity from Clerk. |
| 51 | + |
| 52 | +Please refer to clerk's documentation on how to fetch the current user on the server side for your specific framework. The following code shows an example for Next.js (app router): |
| 53 | + |
| 54 | +```ts |
| 55 | +import { auth } from "@clerk/nextjs/server"; |
| 56 | + |
| 57 | +// ZenStack ORM client with access policy plugin installed |
| 58 | +import { authDb } from './db'; |
| 59 | + |
| 60 | +async function getUserDb() { |
| 61 | + // get the validated user identity from Clerk |
| 62 | + const authObject = await auth(); |
| 63 | + |
| 64 | + // create a user-bound ORM client |
| 65 | + return authDb.$setAuth( |
| 66 | + authObject.userId ? { id: authObject.userId } : undefined); |
| 67 | +} |
| 68 | +``` |
0 commit comments