v2: fixed oauth errors, oauth clients native control added
This commit is contained in:
@@ -0,0 +1,256 @@
|
||||
"use client"
|
||||
|
||||
import { Button } from "@/components/ui/button";
|
||||
import {
|
||||
Dialog,
|
||||
DialogContent,
|
||||
DialogDescription,
|
||||
DialogFooter,
|
||||
DialogHeader,
|
||||
DialogTitle,
|
||||
DialogTrigger,
|
||||
} from "@/components/ui/dialog";
|
||||
import { Checkbox } from "@/components/ui/checkbox";
|
||||
import { Field, FieldError, FieldGroup, FieldLabel, FieldDescription } from "@/components/ui/field";
|
||||
import { Input } from "@/components/ui/input";
|
||||
import { Spinner } from "@/components/ui/spinner";
|
||||
import { useTranslations } from "next-intl";
|
||||
import { Dispatch, SetStateAction, useState } from "react";
|
||||
import { useFieldArray, useForm, Controller, SubmitHandler } from "react-hook-form";
|
||||
import { Plus, Trash2, Copy, Check } from "lucide-react";
|
||||
import { createClient } from "@/actions/clients";
|
||||
import type { CreatedOAuthClient, OAuthClient } from "@/types";
|
||||
|
||||
interface FormData {
|
||||
name: string;
|
||||
redirect_uris: { value: string }[];
|
||||
scopes: string[];
|
||||
}
|
||||
|
||||
interface Props {
|
||||
setClients: Dispatch<SetStateAction<OAuthClient[] | []>>;
|
||||
}
|
||||
|
||||
const GRANT_TYPES = ["authorization_code", "refresh_token"] as const;
|
||||
const SCOPES = ["profile:read", "profile:edit"] as const;
|
||||
|
||||
export function CreateClientDialog({ setClients }: Props) {
|
||||
const t = useTranslations("MePage.items.applications.dialog");
|
||||
const [isOpen, setIsOpen] = useState(false);
|
||||
const [error, setError] = useState<string | null>(null);
|
||||
const [createdClient, setCreatedClient] = useState<CreatedOAuthClient | null>(null);
|
||||
const [copied, setCopied] = useState<"id" | "secret" | null>(null);
|
||||
|
||||
const { register, handleSubmit, control, reset, formState: { errors, isSubmitting } } = useForm<FormData>({
|
||||
defaultValues: {
|
||||
name: "",
|
||||
redirect_uris: [{ value: "" }],
|
||||
scopes: [],
|
||||
},
|
||||
});
|
||||
|
||||
const { fields, append, remove } = useFieldArray({
|
||||
control,
|
||||
name: "redirect_uris",
|
||||
});
|
||||
|
||||
const onSubmit: SubmitHandler<FormData> = async (data) => {
|
||||
setError(null);
|
||||
|
||||
const result = await createClient({
|
||||
name: data.name,
|
||||
redirect_uris: data.redirect_uris.map((r) => r.value).filter(Boolean),
|
||||
grant_types: [...GRANT_TYPES],
|
||||
scopes: data.scopes,
|
||||
});
|
||||
|
||||
if (!result.success || !result.client) {
|
||||
setError(result.message ?? null);
|
||||
return;
|
||||
}
|
||||
|
||||
setClients((prev) => (prev ? [...prev, result.client!] : [result.client!]));
|
||||
setCreatedClient(result.client);
|
||||
};
|
||||
|
||||
const handleCopy = async (value: string, key: "id" | "secret") => {
|
||||
await navigator.clipboard.writeText(value);
|
||||
setCopied(key);
|
||||
setTimeout(() => setCopied(null), 2000);
|
||||
};
|
||||
|
||||
const handleClose = (open: boolean) => {
|
||||
setIsOpen(open);
|
||||
if (!open) {
|
||||
reset();
|
||||
setCreatedClient(null);
|
||||
setError(null);
|
||||
}
|
||||
};
|
||||
|
||||
return (
|
||||
<Dialog open={isOpen} onOpenChange={handleClose}>
|
||||
<DialogTrigger asChild>
|
||||
<Button className="w-max">{t("trigger")}</Button>
|
||||
</DialogTrigger>
|
||||
<DialogContent onInteractOutside={(e) => createdClient && e.preventDefault()}>
|
||||
{!createdClient ? (
|
||||
<>
|
||||
<DialogHeader>
|
||||
<DialogTitle>{t("title")}</DialogTitle>
|
||||
<DialogDescription>{t("subtitle")}</DialogDescription>
|
||||
</DialogHeader>
|
||||
<form onSubmit={handleSubmit(onSubmit)}>
|
||||
<FieldGroup>
|
||||
<Field data-invalid={!!errors.name}>
|
||||
<FieldLabel htmlFor="name">{t("name.title")}</FieldLabel>
|
||||
<Input
|
||||
id="name"
|
||||
disabled={isSubmitting}
|
||||
aria-invalid={!!errors.name}
|
||||
{...register("name", {
|
||||
required: t("required"),
|
||||
minLength: { value: 2, message: t("name.minLength") },
|
||||
maxLength: { value: 128, message: t("name.maxLength") },
|
||||
})}
|
||||
/>
|
||||
{errors.name && <FieldError>{errors.name.message}</FieldError>}
|
||||
</Field>
|
||||
|
||||
<Field>
|
||||
<FieldLabel>{t("redirectUris.title")}</FieldLabel>
|
||||
<div className="flex flex-col gap-2">
|
||||
{fields.map((field, index) => (
|
||||
<div key={field.id} className="flex gap-2">
|
||||
<Input
|
||||
disabled={isSubmitting}
|
||||
placeholder={t("redirectUris.placeholder")}
|
||||
{...register(`redirect_uris.${index}.value` as const, {
|
||||
required: index === 0 ? t("required") : false,
|
||||
pattern: {
|
||||
value: /^https?:\/\/.+/,
|
||||
message: t("redirectUris.invalid"),
|
||||
},
|
||||
})}
|
||||
/>
|
||||
<Button
|
||||
type="button"
|
||||
variant="outline"
|
||||
size="icon"
|
||||
disabled={isSubmitting || fields.length === 1}
|
||||
onClick={() => remove(index)}
|
||||
>
|
||||
<Trash2 />
|
||||
</Button>
|
||||
</div>
|
||||
))}
|
||||
{errors.redirect_uris?.[0]?.value && (
|
||||
<FieldError>{errors.redirect_uris[0]?.value?.message}</FieldError>
|
||||
)}
|
||||
<Button
|
||||
type="button"
|
||||
variant="outline"
|
||||
size="sm"
|
||||
disabled={isSubmitting}
|
||||
onClick={() => append({ value: "" })}
|
||||
>
|
||||
<Plus />
|
||||
{t("redirectUris.add")}
|
||||
</Button>
|
||||
</div>
|
||||
</Field>
|
||||
|
||||
<Field data-invalid={!!errors.scopes}>
|
||||
<FieldLabel>{t("scopes.title")}</FieldLabel>
|
||||
<Controller
|
||||
name="scopes"
|
||||
control={control}
|
||||
rules={{ required: t("required") }}
|
||||
render={({ field }) => (
|
||||
<div className="flex flex-col gap-2">
|
||||
{SCOPES.map((scope) => (
|
||||
<label key={scope} className="flex items-center gap-2 text-sm">
|
||||
<Checkbox
|
||||
disabled={isSubmitting}
|
||||
checked={field.value.includes(scope)}
|
||||
onCheckedChange={(checked) => {
|
||||
field.onChange(
|
||||
checked
|
||||
? [...field.value, scope]
|
||||
: field.value.filter((s) => s !== scope)
|
||||
);
|
||||
}}
|
||||
/>
|
||||
{t(`scopes.${scope}`)}
|
||||
</label>
|
||||
))}
|
||||
</div>
|
||||
)}
|
||||
/>
|
||||
{errors.scopes && <FieldError>{errors.scopes.message}</FieldError>}
|
||||
</Field>
|
||||
|
||||
{error && <FieldError className="text-center">{error}</FieldError>}
|
||||
</FieldGroup>
|
||||
|
||||
<DialogFooter className="mt-6">
|
||||
<Button type="submit" disabled={isSubmitting}>
|
||||
{isSubmitting && <Spinner />}
|
||||
{t("submit")}
|
||||
</Button>
|
||||
</DialogFooter>
|
||||
</form>
|
||||
</>
|
||||
) : (
|
||||
<>
|
||||
<DialogHeader>
|
||||
<DialogTitle>{t("secretReveal.title")}</DialogTitle>
|
||||
<DialogDescription>{t("secretReveal.subtitle")}</DialogDescription>
|
||||
</DialogHeader>
|
||||
|
||||
<FieldGroup>
|
||||
<Field>
|
||||
<FieldLabel>{t("secretReveal.clientId")}</FieldLabel>
|
||||
<div className="flex gap-2">
|
||||
<Input readOnly value={createdClient.client_id} className="font-mono text-sm" />
|
||||
<Button
|
||||
type="button"
|
||||
variant="outline"
|
||||
size="icon"
|
||||
onClick={() => handleCopy(createdClient.client_id, "id")}
|
||||
>
|
||||
{copied === "id" ? <Check /> : <Copy />}
|
||||
</Button>
|
||||
</div>
|
||||
</Field>
|
||||
|
||||
<Field>
|
||||
<FieldLabel>{t("secretReveal.clientSecret")}</FieldLabel>
|
||||
<div className="flex gap-2">
|
||||
<Input readOnly value={createdClient.client_secret} className="font-mono text-sm" />
|
||||
<Button
|
||||
type="button"
|
||||
variant="outline"
|
||||
size="icon"
|
||||
onClick={() => handleCopy(createdClient.client_secret, "secret")}
|
||||
>
|
||||
{copied === "secret" ? <Check /> : <Copy />}
|
||||
</Button>
|
||||
</div>
|
||||
<FieldDescription className="text-destructive">
|
||||
{t("secretReveal.warning")}
|
||||
</FieldDescription>
|
||||
</Field>
|
||||
</FieldGroup>
|
||||
|
||||
<DialogFooter className="mt-6">
|
||||
<Button onClick={() => handleClose(false)}>
|
||||
{t("secretReveal.confirm")}
|
||||
</Button>
|
||||
</DialogFooter>
|
||||
</>
|
||||
)}
|
||||
</DialogContent>
|
||||
</Dialog>
|
||||
);
|
||||
}
|
||||
Reference in New Issue
Block a user