Skip to content

Commit

Permalink
feat: api key dashboard
Browse files Browse the repository at this point in the history
  • Loading branch information
Siddharth committed Nov 3, 2023
1 parent 94262f0 commit 532ff4c
Show file tree
Hide file tree
Showing 11 changed files with 287 additions and 168 deletions.
2 changes: 0 additions & 2 deletions apps/dashboard/app/api-keys/page.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import { getServerSession } from "next-auth"
import { Box, Link, Typography } from "@mui/joy"

import { authOptions } from "@/app/api/auth/[...nextauth]/route"
import ContentContainer from "@/components/content-container"
import ApiKeysList from "@/components/api-keys/list"
import ApiKeyCreate from "@/components/api-keys/create"
Expand Down
88 changes: 88 additions & 0 deletions apps/dashboard/app/api-keys/server-actions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
"use server"
import { getServerSession } from "next-auth"
import { revalidatePath } from "next/cache"

import { authOptions } from "@/app/api/auth/[...nextauth]/route"
import { createApiKey, revokeApiKey } from "@/services/graphql/mutations/api-keys"

export const revokeApiKeyServerAction = async (id: string) => {
if (!id || typeof id !== "string") {
return {
error: true,
message: "API Key ID to revoke is not present",
data: null,
}
}

const session = await getServerSession(authOptions)
const token = session?.accessToken
if (!token || typeof token !== "string") {
return {
error: true,
message: "Token is not present",
data: null,
}
}

try {
await revokeApiKey(token, id)
} catch (err) {
console.log("error in revokeApiKey ", err)
return {
error: true,
message:
"Something went wrong Please try again and if error persist contact support",
data: null,
}
}

revalidatePath("/api-keys")

return {
error: false,
message: "API Key revoked successfully",
data: { ok: true },
}
}

export const createApiKeyServerAction = async (_prevState: unknown, form: FormData) => {
const apiKeyName = form.get("apiKeyName")
if (!apiKeyName || typeof apiKeyName !== "string") {
return {
error: true,
message: "API Key name to create is not present",
data: null,
}
}

const session = await getServerSession(authOptions)
const token = session?.accessToken
if (!token || typeof token !== "string") {
return {
error: true,
message: "Token is not present",
data: null,
}
}

let data
try {
data = await createApiKey(token, apiKeyName)
} catch (err) {
console.log("error in createApiKey ", err)
return {
error: true,
message:
"Something went wrong Please try again and if error persist contact support",
data: null,
}
}

revalidatePath("/api-keys")

return {
error: false,
message: "API Key created successfully",
data: { apiKeySecret: data?.apiKeyCreate.apiKeySecret },
}
}
59 changes: 0 additions & 59 deletions apps/dashboard/app/api/api-keys/route.ts

This file was deleted.

2 changes: 1 addition & 1 deletion apps/dashboard/app/security/email/add/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export default function AddEmail() {
const [state, formAction] = useFormState(emailRegisterInitiateServerAction, {
error: null,
message: null,
responsePayload: {},
data: null,
})

return (
Expand Down
1 change: 0 additions & 1 deletion apps/dashboard/app/security/server-actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ export const emailRegisterInitiateServerAction = async (
}

if (data?.userEmailRegistrationInitiate.errors.length) {
console.log()
return {
error: true,
message: data?.userEmailRegistrationInitiate.errors[0].message,
Expand Down
Loading

0 comments on commit 532ff4c

Please sign in to comment.