Skip to content

Commit 5e7f94b

Browse files
authored
Merge pull request #13 from unkeyed/unkey-clerk-v2
working with v2 api
2 parents 1ebc5fb + 10ec545 commit 5e7f94b

File tree

8 files changed

+4403
-25
lines changed

8 files changed

+4403
-25
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
11
**/node_modules
22
.vercel
3+
.env
4+
.next/

unkey-clerk/.env.example

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
NEXT_PUBLIC_CLERK_PUBLISHABLE_KEY=pk_test_xxxxxxxxxxxxxx
2+
CLERK_SECRET_KEY=sk_test_xxxxxxxxxxxxxx
3+
UNKEY_API_ID=api_xxxxxxxxxxxxxx
4+
UNKEY_ROOT_KEY=unkey_xxxxxxxxxxxxxx

unkey-clerk/README.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# Unkey with Clerk
22

3+
This example demonstrates how to use Unkey v2 API with Clerk authentication.
4+
35
## Getting Started
46

57
First, make sure you have a Clerk and Unkey account, and add them to the `.env` you can see an example in the `.env.example`.
@@ -13,7 +15,7 @@ UNKEY_ROOT_KEY=unkey_xxxxxxxxxxxxxx
1315

1416
Then you can run the server locally
1517
```bash
16-
npm run dev
18+
pnpm run dev
1719
```
1820

1921
Open [http://localhost:3000](http://localhost:3000) with your browser to see the result.
@@ -22,3 +24,4 @@ Open [http://localhost:3000](http://localhost:3000) with your browser to see the
2224

2325
- [Blog Post](https://unkey.dev/blog/using-unkey-with-auth) - learn about how this project works
2426
- [Getting Started](https://unkey.dev/docs/quickstart) - A quickstart guide to Unkey
27+
- [Unkey v2 API Documentation](https://unkey.dev/docs) - Documentation for the v2 API

unkey-clerk/app/api/secret/route.ts

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,30 @@
1-
import { verifyKey } from "@unkey/api";
1+
import { Unkey } from "@unkey/api";
22
import { NextResponse } from "next/server";
3+
34
export async function GET(request: Request) {
45
const header = request.headers.get("Authorization");
56
if (!header) {
67
return new Response("No Authorization header", { status: 401 });
78
}
89
const token = header.replace("Bearer ", "");
9-
const { result, error } = await verifyKey(token);
10+
11+
const unkey = new Unkey({ rootKey: process.env.UNKEY_ROOT_KEY! });
12+
13+
try {
14+
const response = await unkey.keys.verifyKey({
15+
key: token,
16+
});
1017

11-
if (error) {
12-
console.error(error.message);
13-
return new Response("Internal Server Error", { status: 500 });
14-
}
18+
// In v2, the response structure is different
19+
if (!response.data.valid) {
20+
// do not grant access
21+
return new Response("Unauthorized", { status: 401 });
22+
}
1523

16-
if (!result.valid) {
17-
// do not grant access
18-
return new Response("Unauthorized", { status: 401 });
24+
// process request
25+
return NextResponse.json({ result: response });
26+
} catch (error) {
27+
console.error(error);
28+
return new Response("Internal Server Error", { status: 500 });
1929
}
20-
21-
// process request
22-
return NextResponse.json({ result });
2330
}

unkey-clerk/app/keys/client.tsx

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,12 @@ import { create } from "./create";
1717
const UnkeyElements = () => {
1818
const [key, setKey] = useState<string>("");
1919
const [secret, setSecret] = useState<string>("");
20+
const [meta, setMeta] = useState<any>(null);
21+
const [data, setData] = useState<any>(null);
2022
async function onCreate(formData: FormData) {
2123
const res = await create(formData);
22-
if (res) {
23-
setKey(res.key?.key ?? "");
24+
if (res && res.data) {
25+
setKey(res.data.key ?? "");
2426
}
2527
}
2628
const getData = async () => {
@@ -30,7 +32,9 @@ const UnkeyElements = () => {
3032
},
3133
});
3234
const data = await res.json();
33-
setSecret(data.result);
35+
setSecret(data.result.data.keyId);
36+
setMeta(data.result.meta);
37+
setData(data.result.data);
3438
};
3539
return (
3640
<div className="mt-8">
@@ -44,7 +48,7 @@ const UnkeyElements = () => {
4448
<div className="grid items-center w-full gap-4">
4549
<div className="flex flex-col space-y-1.5">
4650
<Label htmlFor="name">API Key Name</Label>
47-
<Input name="name" placeholder="My Awesome API " />
51+
<Input name="name" placeholder="My Awesome API" />
4852
</div>
4953
</div>
5054
</CardContent>
@@ -79,11 +83,17 @@ const UnkeyElements = () => {
7983
Get Data
8084
</Button>
8185
<div className="grid items-center w-full gap-4">
82-
<div className="flex flex-col space-y-1.5">
86+
<div className="flex flex-col space-y-1.5 py-2">
8387
<Label htmlFor="name">Secret Data</Label>
8488
<Input name="name" value={JSON.stringify(secret)} />
8589
</div>
8690
</div>
91+
<div className="mt-4 border p-4 rounded-md bg-muted">
92+
<Label htmlFor="name">Meta Data</Label>
93+
<pre className="text-xs">{JSON.stringify(meta, null, 2)}</pre>
94+
<Label htmlFor="name">Key Data</Label>
95+
<pre className="text-xs">{JSON.stringify(data, null, 2)}</pre>
96+
</div>
8797
</CardContent>
8898
</Card>
8999
</>

unkey-clerk/app/keys/create.ts

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,14 @@ export async function create(formData: FormData) {
1414
return null;
1515
}
1616

17-
const name = (formData.get("name") as string) ?? "My Awesome API";
18-
const unkey = new Unkey({ token });
19-
const key = await unkey.keys.create({
20-
name: name,
21-
ownerId: userId,
17+
const name = formData.get("name") as string;
18+
// Ensure name is never empty - use default if empty or null
19+
const keyName = name && name.trim().length > 0 ? name.trim() : "My Awesome API";
20+
21+
const unkey = new Unkey({ rootKey: token });
22+
const res = await unkey.keys.createKey({
23+
name: keyName,
2224
apiId,
2325
});
24-
return { key: key.result };
26+
return { data: res.data, meta: res.meta };
2527
}

unkey-clerk/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
"@types/node": "20.8.5",
1717
"@types/react": "18.2.28",
1818
"@types/react-dom": "18.2.13",
19-
"@unkey/api": "^0.22.1",
19+
"@unkey/api": "^2.0.1",
2020
"autoprefixer": "10.4.16",
2121
"class-variance-authority": "^0.7.0",
2222
"clsx": "^2.0.0",

0 commit comments

Comments
 (0)