-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
planning to add automating and a backend to get any request from external tool and automate the work
- Loading branch information
1 parent
ce19945
commit ebbb872
Showing
4 changed files
with
153 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,65 @@ | ||
import { Button } from '@/components/ui/button' | ||
import React from 'react' | ||
"use client"; | ||
|
||
import { Button } from "@/components/ui/button"; | ||
import { Input } from "@/components/ui/input"; | ||
import { api } from "@/convex/_generated/api"; | ||
import useApiMutation from "@/lib/hooks/use-api-mutation"; | ||
import { useQuery } from "convex/react"; | ||
import { Copy, RefreshCcw, Trash } from "lucide-react"; | ||
import { toast } from "sonner"; | ||
|
||
const AccountSettingsPage = () => { | ||
const existingAPIKey = useQuery(api.api_key.get); | ||
|
||
const { mutate: createAPIKey, isPending } = useApiMutation( | ||
api.api_key.create | ||
); | ||
|
||
const { mutate: revokeAPIKey, isPending: isRevoking } = useApiMutation( | ||
api.api_key.revoke | ||
); | ||
|
||
const handleCopy = () => { | ||
navigator.clipboard | ||
.writeText(existingAPIKey?.key || "") | ||
.then(() => toast.success("API key copied to clipboard")) | ||
.catch(() => toast.error("Failed to copy API key to clipboard")); | ||
}; | ||
|
||
const handleCreate = () => { | ||
createAPIKey() | ||
.then(() => toast.success("API key generated")) | ||
.catch(() => toast.error("Failed to generate API key")); | ||
}; | ||
|
||
const handleRevoke = () => { | ||
revokeAPIKey() | ||
.then(() => toast.success("API key revoked")) | ||
.catch(() => toast.error("Failed to revoke API key")); | ||
}; | ||
|
||
return ( | ||
<div> | ||
<Button>Generate a token</Button> | ||
</div> | ||
) | ||
} | ||
<> | ||
{existingAPIKey ? ( | ||
<div className="flex gap-3 items-center"> | ||
<Input value={existingAPIKey.key} disabled className="w-auto" /> | ||
<Button onClick={handleCopy} disabled={isPending} size="icon"> | ||
<Copy /> | ||
</Button> | ||
<Button onClick={handleCreate} disabled={isPending} size="icon"> | ||
<RefreshCcw /> | ||
</Button> | ||
<Button onClick={handleRevoke} disabled={isRevoking} size="icon"> | ||
<Trash /> | ||
</Button> | ||
</div> | ||
) : ( | ||
<Button onClick={handleCreate} disabled={isPending}> | ||
Generate a token | ||
</Button> | ||
)} | ||
</> | ||
); | ||
}; | ||
|
||
export default AccountSettingsPage | ||
export default AccountSettingsPage; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
import { mutation, query } from "./_generated/server"; | ||
|
||
export const create = mutation({ | ||
handler: async (ctx) => { | ||
const identity = await ctx.auth.getUserIdentity(); | ||
if (!identity) { | ||
throw new Error("Unauthenticated user cannot create messages"); | ||
} | ||
|
||
const user = await ctx.db | ||
.query("users") | ||
.withIndex("by_clerk", (q) => q.eq("clerkId", identity.subject)) | ||
.first(); | ||
|
||
if (!user) { | ||
throw new Error("User not found"); | ||
} | ||
|
||
const existingKey = await ctx.db | ||
.query("api_keys") | ||
.withIndex("by_user", (q) => q.eq("userId", user._id)) | ||
.first(); | ||
|
||
if (existingKey) { | ||
ctx.db.delete(existingKey._id); | ||
} | ||
|
||
return ctx.db.insert("api_keys", { | ||
userId: user._id, | ||
key: Math.random().toString(36).substring(2), | ||
}); | ||
}, | ||
}); | ||
|
||
export const revoke = mutation({ | ||
handler: async (ctx) => { | ||
const identity = await ctx.auth.getUserIdentity(); | ||
if (!identity) { | ||
throw new Error("Unauthenticated user cannot create messages"); | ||
} | ||
|
||
const user = await ctx.db | ||
.query("users") | ||
.withIndex("by_clerk", (q) => q.eq("clerkId", identity.subject)) | ||
.first(); | ||
|
||
if (!user) { | ||
throw new Error("User not found"); | ||
} | ||
|
||
const key = await ctx.db | ||
.query("api_keys") | ||
.withIndex("by_user", (q) => q.eq("userId", user._id)) | ||
.first(); | ||
|
||
if (!key) { | ||
throw new Error("API key not found"); | ||
} | ||
|
||
return ctx.db.delete(key._id); | ||
}, | ||
}); | ||
|
||
export const get = query({ | ||
handler: async (ctx) => { | ||
const identity = await ctx.auth.getUserIdentity(); | ||
if (!identity) { | ||
throw new Error("Unauthenticated user cannot create messages"); | ||
} | ||
|
||
const user = await ctx.db | ||
.query("users") | ||
.withIndex("by_clerk", (q) => q.eq("clerkId", identity.subject)) | ||
.first(); | ||
|
||
if (!user) { | ||
throw new Error("User not found"); | ||
} | ||
|
||
return ctx.db | ||
.query("api_keys") | ||
.withIndex("by_user", (q) => q.eq("userId", user?._id)) | ||
.first(); | ||
}, | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters