Skip to content

WIP client management. Needs typescript work. - #57

Draft
matthewpereira wants to merge 1 commit into
mainfrom
mp-client-management
Draft

WIP client management. Needs typescript work.#57
matthewpereira wants to merge 1 commit into
mainfrom
mp-client-management

Conversation

@matthewpereira

Copy link
Copy Markdown
Contributor

Playing with API Client management, but I'm not a pro engineer, so I might be doing this wrong.

@vercel

vercel Bot commented Sep 23, 2024

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
auth0-b2b-saas-starter ❌ Failed (Inspect) Sep 23, 2024 6:41pm

@eatplaysleep

Copy link
Copy Markdown

You will also need to add the following to the createClientGrantArgs in scripts/bootstrap.mjs:

        ...,
        "create:users",
        // Add this
        "read:clients",
        "create:clients",
        "update:clients",
        "delete:clients",
        "create:client_keys",
        "read:client_keys",
        "update:client_keys",
        "delete:client_keys",
        "read:client_credentials",
        ...

@eatplaysleep

Copy link
Copy Markdown

Make sure to bump sonner to ^1.7.0

@eatplaysleep eatplaysleep left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think I got everything here. Just a few changes and I think it's good.

}

try {
const newClient = await managementClient.clients.create({

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

managementClient responses have a data object containing the actual response.

Suggested change
const newClient = await managementClient.clients.create({
const { data: newClient } = await managementClient.clients.create({


revalidatePath("/dashboard/organization/api-clients")
return {
clientId: newClient.client_id!,

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

unnecessary once you get the response object corrected.

Suggested change
clientId: newClient.client_id!,
clientId: newClient.client_id,

revalidatePath("/dashboard/organization/api-clients")
return {
clientId: newClient.client_id!,
clientSecret: newClient.client_secret!

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
clientSecret: newClient.client_secret!
clientSecret: newClient.client_secret

export const rotateApiClientSecret = withServerActionAuth(
async function rotateApiClientSecret(clientId: string, session: Session) {
try {
const result = await managementClient.clients.rotateSecret({ client_id: clientId })

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
const result = await managementClient.clients.rotateSecret({ client_id: clientId })
const { data: result } = await managementClient.clients.rotateClientSecret({ client_id })

import { Session } from "@auth0/nextjs-auth0"
import { ClientCreateAppTypeEnum } from "auth0"

import { managementClient } from "@/lib/auth0"

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
import { managementClient } from "@/lib/auth0"
import { managementClient } from "@/lib/auth0"
import { Client } from "@/lib/clients"

@@ -0,0 +1,135 @@
import { revalidatePath } from "next/cache"

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
import { revalidatePath } from "next/cache"
"use server"
import { revalidatePath } from "next/cache"

} from "@/components/ui/select"
import { SubmitButton } from "@/components/submit-button"

import { createApiClient } from "./actions"

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
import { createApiClient } from "./actions"
import { createApiClient, CreateApiClientSuccess } from "./actions"

Comment on lines +35 to +44
const result = await createApiClient(formData)

if ('error' in result) {
toast.error(result.error)
} else {
toast.success(`API Client created: ${formData.get("name")}`)
toast.info(`Client ID: ${result.clientId}`)
toast.info(`Client Secret: ${result.clientSecret}`)
ref.current?.reset()
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
const result = await createApiClient(formData)
if ('error' in result) {
toast.error(result.error)
} else {
toast.success(`API Client created: ${formData.get("name")}`)
toast.info(`Client ID: ${result.clientId}`)
toast.info(`Client Secret: ${result.clientSecret}`)
ref.current?.reset()
}
toast.promise(createApiClient(formData), {
loading: "Creating client...",
success: (result) => {
if ("error" in result) {
throw result.error
}
toast(() => <h5>Your client secret is:</h5>, {
description: () => (
<code>
{
(result as unknown as CreateApiClientSuccess)
?.clientSecret
}
</code>
),
duration: Infinity,
action: {
label: "Dismiss",
onClick: () => {},
},
})
return `API Client created: ${formData.get("name")}`
},
error: (err) => err,
})

Comment on lines +8 to +10
type CreateApiClientResult =
| { error: string }
| { clientId: string; clientSecret: string }

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
type CreateApiClientResult =
| { error: string }
| { clientId: string; clientSecret: string }
interface CreateApiClientError {
error: string
}
export interface CreateApiClientSuccess {
clientId: string
clientSecret: string
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just realized this is not used. Remove it entirely.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants