"use client"; import { CopyButton } from "@/components/dashboard/copy-button"; import { EmptyPlaceholder } from "@/components/dashboard/empty-placeholder"; import { Loading } from "@/components/dashboard/loading"; import { VisibleButton } from "@/components/dashboard/visible-button"; import { Alert, AlertDescription, AlertTitle } from "@/components/ui/alert"; import { Button } from "@/components/ui/button"; import { Card, CardContent, CardDescription, CardFooter, CardHeader, CardTitle, } from "@/components/ui/card"; import { Code } from "@/components/ui/code"; import { Separator } from "@/components/ui/separator"; import { toast } from "@/components/ui/toaster"; // import { trpc } from "@/lib/trpc/client"; import { AlertCircle, KeyRound, Lock } from "lucide-react"; import Link from "next/link"; import { useState } from "react"; type Steps = | { step: "CREATE_ROOT_KEY"; key?: never; rootKey?: never; } | { step: "CREATE_KEY"; key?: never; rootKey: string; } | { step: "VERIFY_KEY"; key?: string; rootKey?: never; }; type Props = { apiId: string; keyAuthId: string; }; export const Keys: React.FC = ({ keyAuthId, apiId }) => { const [step, setStep] = useState({ step: "CREATE_ROOT_KEY" }); // const rootKey = trpc.key.createInternalRootKey.useMutation({ // onSuccess(res) { // setStep({ step: "CREATE_KEY", rootKey: res.key }); // }, // onError(err) { // console.error(err); // toast.error(err.message); // }, // }); // const key = trpc.key.create.useMutation({ // onSuccess(res) { // setStep({ step: "VERIFY_KEY", key: res.key }); // }, // onError(err) { // console.error(err); // toast.error(err.message); // }, // }); const rootKey = { data: { key: "" }, isLoading: true } const key = { data: { key: "" } } const [showKey, setShowKey] = useState(false); const [showKeyInSnippet, setShowKeyInSnippet] = useState(false); const createKeySnippet = `curl -XPOST '${process.env.NEXT_PUBLIC_UNKEY_API_URL ?? "https://api.unkey.dev" }/v1/keys.createKey' \\ -H 'Authorization: Bearer ${rootKey.data?.key}' \\ -H 'Content-Type: application/json' \\ -d '{ "apiId": "${apiId}" }' `; const verifyKeySnippet = `curl -XPOST '${process.env.NEXT_PUBLIC_UNKEY_API_URL ?? "https://api.unkey.dev" }/v1/keys.verifyKey' \\ -H 'Content-Type: application/json' \\ -d '{ "key": "${key.data?.key ?? ""}" }' `; function maskKey(key: string): string { if (key.length === 0) { return ""; } const split = key.split("_"); if (split.length === 1) { return "*".repeat(split.at(0)!.length); } return `${split.at(0)}_${"*".repeat(split.at(1)!.length)}`; } function AsideContent() { return (

Root Keys

Root keys create resources such as keys or APIs on Unkey. You should never give this to your users.

Regular Keys

Regular API keys are used to authenticate your users. You can use your root key to create regular API keys and give them to your users.

); } return (
{step.step === "CREATE_ROOT_KEY" ? ( Let's begin by creating a root key ) : step.step === "CREATE_KEY" ? ( Your root key This key is only shown once and can not be recovered Please store it somewhere safe for future use. {showKey ? step.rootKey : maskKey(step.rootKey)}

Try it out

Use your new root key to create a new API key for your users:

{showKeyInSnippet ? createKeySnippet : createKeySnippet.replace(step.rootKey, maskKey(step.rootKey))}
) : step.step === "VERIFY_KEY" ? ( Verify a key Use the key you created and verify it. {step.key ? ( {showKey ? step.key : maskKey(step.key)}
) : null}
{step.key ? showKeyInSnippet ? verifyKeySnippet : verifyKeySnippet.replace(step.key, maskKey(step.key)) : verifyKeySnippet}
{step.key ? ( ) : null}
) : null}
); };