| 1 | "use client"; |
| 2 | |
| 3 | import { |
| 4 | type ApiKeyAuthClient, |
| 5 | apiKeyExpirationDaysToSeconds, |
| 6 | } from "@better-auth-ui/core/plugins/api-key"; |
| 7 | import { useAuth, useAuthPlugin |
| 8 | import { useCreateApiKey } |
| 9 | import { Key } |
| 10 | import { useState } |
| 11 | import { buttonVariants } |
| 12 | import { |
| 13 | Dialog, |
| 14 | DialogClose, |
| 15 | DialogContent, |
| 16 | DialogDescription, |
| 17 | DialogFooter, |
| 18 | DialogHeader, |
| 19 | DialogTitle, |
| 20 | } from "@/components/ui/dialog"; |
| 21 | import { |
| 22 | Field, |
| 23 | FieldError, |
| 24 | FieldGroup, |
| 25 | FieldLabel, |
| 26 | } from "@/components/ui/field"; |
| 27 | import { Input } |
| 28 | import { |
| 29 | Select, |
| 30 | SelectContent, |
| 31 | SelectGroup, |
| 32 | SelectItem, |
| 33 | SelectTrigger, |
| 34 | SelectValue, |
| 35 | } from "@/components/ui/select"; |
| 36 | import { apiKeyPlugin } |
| 37 | import { useAuthForm } |
| 38 | import { NewApiKeyDialog } |
| 39 | |
| 40 | export type CreateApiKeyDialogProps |
| 41 | open: boolean |
| 42 | onOpenChange: (open |
| 43 | /** Create an organization-owned key by passing the organization id. */ |
| 44 | organizationId?: string |
| 45 | }; |
| 46 | |
| 47 | export function CreateApiKeyDialog({ |
| 48 | open, |
| 49 | onOpenChange, |
| 50 | organizationId, |
| 51 | }: CreateApiKeyDialogProps) |
| 52 | const { authClient, |
| 53 | const { |
| 54 | configurations, |
| 55 | keyExpiration, |
| 56 | localization: apiKeyLocalization, |
| 57 | } = useAuthPlugin(apiKeyPlugin |
| 58 | |
| 59 | const { mutateAsync: |
| 60 | useCreateApiKey(authClient); |
| 61 | |
| 62 | const [isNewKeyDialogOpen, |
| 63 | const [keyName, |
| 64 | const [secretKey, |
| 65 | const availableConfigurations = |
| 66 | (configuration) => |
| 67 | ); |
| 68 | |
| 69 | const form = |
| 70 | defaultValues: { |
| 71 | configId: availableConfigurations[0] |
| 72 | expiration: |
| 73 | keyExpiration === false || |
| 74 | ? "never" |
| 75 | : String(keyExpiration. |
| 76 | name: "", |
| 77 | }, |
| 78 | onSubmit: async ( |
| 79 | const name = |
| 80 | const expirationDays = |
| 81 | value.expiration !== |
| 82 | const expiresIn = |
| 83 | ? apiKeyExpirationDaysToSeconds(expirationDays) |
| 84 | : undefined; |
| 85 | const configId = |
| 86 | const resolvedConfigId = |
| 87 | configId || (organizationId |
| 88 | const payload = |
| 89 | ...(name ? |
| 90 | ...(expiresIn ? |
| 91 | ...(resolvedConfigId ? |
| 92 | ...(organizationId ? |
| 93 | }; |
| 94 | await createApiKey( |
| 95 | Object.keys(payload |
| 96 | { |
| 97 | onSuccess: (result) |
| 98 | handleOpenChange(false); |
| 99 | setKeyName(name); |
| 100 | setSecretKey(result.key |
| 101 | setIsNewKeyDialogOpen(true); |
| 102 | }, |
| 103 | }, |
| 104 | ); |
| 105 | }, |
| 106 | }); |
| 107 | |
| 108 | const handleOpenChange = |
| 109 | if (!nextOpen) |
| 110 | setKeyName(null); |
| 111 | setSecretKey(null); |
| 112 | form.reset(); |
| 113 | } |
| 114 | |
| 115 | onOpenChange(nextOpen); |
| 116 | }; |
| 117 | |
| 118 | const handleNewKeyDialogOpenChange = |
| 119 | setIsNewKeyDialogOpen(nextOpen); |
| 120 | |
| 121 | if (!nextOpen) |
| 122 | setKeyName(null); |
| 123 | setSecretKey(null); |
| 124 | } |
| 125 | }; |
| 126 | |
| 127 | return ( |
| 128 | <> |
| 129 | <Dialog open={ |
| 130 | <DialogContent> |
| 131 | <form.AppForm> |
| 132 | <form.AuthFormRoot className=" |
| 133 | <DialogHeader> |
| 134 | <DialogTitle className=" |
| 135 | <Key /> |
| 136 | {apiKeyLocalization.createApiKey} |
| 137 | </DialogTitle> |
| 138 | |
| 139 | <DialogDescription> |
| 140 | {apiKeyLocalization.apiKeysDescription} |
| 141 | </DialogDescription> |
| 142 | </DialogHeader> |
| 143 | |
| 144 | <FieldGroup> |
| 145 | <form.AppField name=" |
| 146 | {(field) => |
| 147 | <Field> |
| 148 | <FieldLabel htmlFor=" |
| 149 | {apiKeyLocalization.name} |
| 150 | </FieldLabel> |
| 151 | |
| 152 | <Input |
| 153 | id="api-key-name" |
| 154 | name={field.name |
| 155 | autoFocus |
| 156 | placeholder={localization.settings |
| 157 | disabled={isCreating} |
| 158 | value={field.state |
| 159 | onBlur={field.handleBlur |
| 160 | onChange={(event) |
| 161 | field.handleChange(event |
| 162 | } |
| 163 | /> |
| 164 | |
| 165 | <FieldError /> |
| 166 | </Field> |
| 167 | )} |
| 168 | </form.AppField> |
| 169 | |
| 170 | {availableConfigurations.length > |
| 171 | <form.AppField name=" |
| 172 | {(field) => |
| 173 | <Field> |
| 174 | <FieldLabel htmlFor=" |
| 175 | {apiKeyLocalization.configuration} |
| 176 | </FieldLabel> |
| 177 | <Select |
| 178 | name={field.name |
| 179 | value={field.state |
| 180 | onValueChange={(value) |
| 181 | disabled={isCreating} |
| 182 | > |
| 183 | <SelectTrigger |
| 184 | id="api-key-configuration" |
| 185 | className="w-full" |
| 186 | > |
| 187 | <SelectValue /> |
| 188 | </SelectTrigger> |
| 189 | <SelectContent> |
| 190 | <SelectGroup> |
| 191 | {availableConfigurations.map(( |
| 192 | <SelectItem |
| 193 | key={configuration.id |
| 194 | value={configuration.id |
| 195 | > |
| 196 | {configuration.label} |
| 197 | </SelectItem> |
| 198 | ))} |
| 199 | </SelectGroup> |
| 200 | </SelectContent> |
| 201 | </Select> |
| 202 | </Field> |
| 203 | )} |
| 204 | </form.AppField> |
| 205 | )} |
| 206 | |
| 207 | {keyExpiration ? ( |
| 208 | <form.AppField name=" |
| 209 | {(field) => |
| 210 | <Field> |
| 211 | <FieldLabel htmlFor=" |
| 212 | {apiKeyLocalization.expiration} |
| 213 | </FieldLabel> |
| 214 | |
| 215 | <Select |
| 216 | name={field.name |
| 217 | value={field.state |
| 218 | onValueChange={(value) |
| 219 | disabled={isCreating} |
| 220 | > |
| 221 | <SelectTrigger |
| 222 | id="api-key-expiration" |
| 223 | className="w-full" |
| 224 | > |
| 225 | <SelectValue /> |
| 226 | </SelectTrigger> |
| 227 | |
| 228 | <SelectContent> |
| 229 | <SelectGroup> |
| 230 | {keyExpiration.intervals.map |
| 231 | <SelectItem key={ |
| 232 | {days.toLocaleString()}{ |
| 233 | {days === 1 |
| 234 | ? apiKeyLocalization.day |
| 235 | : apiKeyLocalization.days} |
| 236 | </SelectItem> |
| 237 | ))} |
| 238 | |
| 239 | {keyExpiration.allowNever ? |
| 240 | <SelectItem value=" |
| 241 | {apiKeyLocalization.never} |
| 242 | </SelectItem> |
| 243 | ) : null} |
| 244 | </SelectGroup> |
| 245 | </SelectContent> |
| 246 | </Select> |
| 247 | </Field> |
| 248 | )} |
| 249 | </form.AppField> |
| 250 | ) : null} |
| 251 | </FieldGroup> |
| 252 | |
| 253 | <DialogFooter> |
| 254 | <DialogClose |
| 255 | className={buttonVariants({ |
| 256 | disabled={isCreating} |
| 257 | type="button" |
| 258 | > |
| 259 | {localization.settings.cancel |
| 260 | </DialogClose> |
| 261 | |
| 262 | <form.AuthFormSubmitButton |
| 263 | isPending={isCreating} |
| 264 | disabled={isCreating} |
| 265 | > |
| 266 | {apiKeyLocalization.createApiKey} |
| 267 | </form.AuthFormSubmitButton> |
| 268 | </DialogFooter> |
| 269 | </form.AuthFormRoot> |
| 270 | </form.AppForm> |
| 271 | </DialogContent> |
| 272 | </Dialog> |
| 273 | |
| 274 | <NewApiKeyDialog |
| 275 | open={isNewKeyDialogOpen} |
| 276 | onOpenChange={handleNewKeyDialogOpenChange} |
| 277 | secretKey={secretKey} |
| 278 | name={keyName} |
| 279 | /> |
| 280 | </> |
| 281 | ); |
| 282 | } |