Skip to content

Commit d601de9

Browse files
authored
add clerk multi-tenant jwt (#49)
* add clerk multi-tenant jwt * update next * update clerk * update deps * fallback if no clerk is configured * linter * update readme * fix clerk load * clerk fallback * add default dates
1 parent 8795210 commit d601de9

File tree

18 files changed

+1359
-744
lines changed

18 files changed

+1359
-744
lines changed

README.md

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,15 @@ This is a template for a Logs Explorer web application. It is built with Next.js
44

55
Use this template to bootstrap a multi-tenant, user-facing logs explorer for any software project. Fork it and make it your own!
66

7+
Tech Stack:
8+
- Next.js
9+
- Tinybird
10+
- Clerk
11+
- Vercel
12+
- zod-bird
13+
- Tailwind CSS
14+
- Shadcn UI
15+
716
## Live Demo
817

918
- [https://logs.tinybird.app](https://logs.tinybird.app)
@@ -77,6 +86,66 @@ http://localhost:3000
7786

7887
Read the [dashboard/log-analyzer/README.md](./dashboard/log-analyzer/README.md) file for more information on how to use the application and [tinybird/README.md](./tinybird/README.md) for more information on how to customize the template.
7988

89+
## Multi-tenancy
90+
91+
The template is designed to be multi-tenant. It uses Clerk for authentication and user management.
92+
93+
Configure the `.env` file with your Clerk publishable key and secret.
94+
95+
```bash
96+
CLERK_PUBLISHABLE_KEY=<YOUR_CLERK_PUBLISHABLE_KEY>
97+
CLERK_SECRET_KEY=<YOUR_CLERK_SECRET_KEY>
98+
```
99+
100+
Then set the Tinybird JWT secret and workspace ID in the `.env` file.
101+
102+
```bash
103+
TINYBIRD_JWT_SECRET=<YOUR_TINYBIRD_ADMIN_TOKEN>
104+
TINYBIRD_WORKSPACE_ID=<YOUR_TINYBIRD_WORKSPACE_ID>
105+
```
106+
107+
Modify the middleware to adapt the Tinybird token to your tenants.
108+
109+
```typescript
110+
// dashboard/log-analyzer/src/middleware.ts
111+
112+
const token = await new jose.SignJWT({
113+
workspace_id: process.env.TINYBIRD_WORKSPACE_ID,
114+
name: `frontend_jwt_user_${userId}`,
115+
exp: Math.floor(Date.now() / 1000) + (60 * 15), // 15 minute expiration
116+
iat: Math.floor(Date.now() / 1000),
117+
scopes: [
118+
{
119+
type: "PIPES:READ",
120+
resource: "log_analysis",
121+
fixed_params: { user_id: userId, org_permission: orgName, service: "web" }
122+
},
123+
{
124+
type: "PIPES:READ",
125+
resource: "log_explorer",
126+
fixed_params: { user_id: userId, org_permission: orgName, service: "web" }
127+
},
128+
{
129+
type: "PIPES:READ",
130+
resource: "generic_counter",
131+
fixed_params: { user_id: userId, org_permission: orgName, service: "web" }
132+
},
133+
{
134+
type: "PIPES:READ",
135+
resource: "log_timeseries",
136+
fixed_params: { user_id: userId, org_permission: orgName, service: "web" }
137+
}
138+
],
139+
limits: {
140+
rps: 10
141+
}
142+
})
143+
.setProtectedHeader({ alg: 'HS256' })
144+
.sign(secret);
145+
```
146+
147+
Read more about how to integrate Clerk and Tinybird JWT tokens in [this guide](https://www.tinybird.co/docs/publish/api-endpoints/guides/multitenant-real-time-apis-with-clerk-and-tinybird).
148+
80149
## Instrumenting your application
81150

82151
To instrument your application, just send JSON objects to the Tinybird [Events API](https://www.tinybird.co/docs/get-data-in/ingest-apis/events-api).
Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,20 @@
1+
# Tinybird default key for unauthenticated requests
12
NEXT_PUBLIC_TINYBIRD_API_KEY=
2-
NEXT_PUBLIC_TINYBIRD_API_URL=http://localhost:7181
3+
# Tinybird API URL (replace with your Tinybird region host)
4+
NEXT_PUBLIC_TINYBIRD_API_URL=http://localhost:7181
5+
6+
7+
# Tinybird workspace ID for multi-tenant JWT tokens
8+
TINYBIRD_WORKSPACE_ID=
9+
# Tinybird workspace admin token for multi-tenant JWT tokens
10+
TINYBIRD_JWT_SECRET=
11+
12+
# Clerk publishable key
13+
NEXT_PUBLIC_CLERK_PUBLISHABLE_KEY=
14+
# Clerk secret key
15+
CLERK_SECRET_KEY=
16+
# Clerk sign in URL
17+
NEXT_PUBLIC_CLERK_SIGN_IN_URL=/sign-in
18+
NEXT_PUBLIC_CLERK_SIGN_UP_URL=/sign-up
19+
NEXT_PUBLIC_CLERK_AFTER_SIGN_IN_URL=/
20+
NEXT_PUBLIC_CLERK_AFTER_SIGN_UP_URL=/

dashboard/log-analyzer/.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
2+
# clerk configuration (can include secrets)
3+
/.clerk/

0 commit comments

Comments
 (0)