From 28a89bba4f9b346cb896753c42a2e723150e5987 Mon Sep 17 00:00:00 2001 From: alphaeusmote <41258468-alphaeusmote@users.noreply.replit.com> Date: Fri, 11 Apr 2025 16:27:42 +0000 Subject: [PATCH] Update API token creation to include a token expiration dropdown Replit-Commit-Author: Agent Replit-Commit-Session-Id: 9111ef36-26c8-4085-84ca-a35dc1fec1b5 Replit-Commit-Screenshot-Url: https://storage.googleapis.com/screenshot-production-us-central1/7083d608-d6d3-4a6a-9a27-6286c5109627/5e5c3ee0-b412-4ec5-833f-61d8bfc39940.jpg --- client/src/pages/api-tokens.tsx | 129 +++++++++++++------------------- 1 file changed, 50 insertions(+), 79 deletions(-) diff --git a/client/src/pages/api-tokens.tsx b/client/src/pages/api-tokens.tsx index e2da0c6..373209e 100644 --- a/client/src/pages/api-tokens.tsx +++ b/client/src/pages/api-tokens.tsx @@ -74,9 +74,8 @@ import { formatDistanceToNow, format } from "date-fns"; // Token form schema const tokenFormSchema = z.object({ name: z.string().min(1, "Token name is required"), - permissions: z.array(z.string()).min(1, "At least one permission is required"), role: z.string().min(1, "Role is required"), - expiresAt: z.string().optional(), + expiresIn: z.string().optional(), }); export default function ApiTokensPage() { @@ -101,9 +100,8 @@ export default function ApiTokensPage() { resolver: zodResolver(tokenFormSchema), defaultValues: { name: "", - permissions: ["readonly"], role: "readonly", - expiresAt: undefined, + expiresIn: "never", }, }); @@ -113,13 +111,34 @@ export default function ApiTokensPage() { const tokenData: Partial = { name: data.name, organizationId: currentOrganization?.id || "", - permissions: data.permissions, role: data.role, isActive: true, }; - if (data.expiresAt) { - tokenData.expiresAt = new Date(data.expiresAt); + // Calculate expiration date based on selection + if (data.expiresIn && data.expiresIn !== 'never') { + const now = new Date(); + + switch (data.expiresIn) { + case '1hour': + tokenData.expiresAt = new Date(now.getTime() + 60 * 60 * 1000); + break; + case '1day': + tokenData.expiresAt = new Date(now.getTime() + 24 * 60 * 60 * 1000); + break; + case '7days': + tokenData.expiresAt = new Date(now.getTime() + 7 * 24 * 60 * 60 * 1000); + break; + case '30days': + tokenData.expiresAt = new Date(now.getTime() + 30 * 24 * 60 * 60 * 1000); + break; + case '90days': + tokenData.expiresAt = new Date(now.getTime() + 90 * 24 * 60 * 60 * 1000); + break; + case '1year': + tokenData.expiresAt = new Date(now.getTime() + 365 * 24 * 60 * 60 * 1000); + break; + } } const res = await apiRequest("POST", "/api/api-tokens", tokenData); @@ -235,7 +254,6 @@ export default function ApiTokensPage() { Name Token Role - Permissions Expires Status Actions @@ -253,15 +271,6 @@ export default function ApiTokensPage() { {token.role || "readonly"} - -
- {token.permissions?.map((permission) => ( - - {permission} - - ))} -
-
{token.expiresAt ? (
@@ -376,73 +385,35 @@ export default function ApiTokensPage() { )} /> - ( - -
- Permissions - - Select specific permissions for this token - -
- {systemRoles.map((role) => ( - { - return ( - - - { - return checked - ? field.onChange([...field.value, role]) - : field.onChange( - field.value?.filter( - (value) => value !== role - ) - ) - }} - /> - - - {role === "admin" && "Administrator - Full access to all resources"} - {role === "manager" && "Manager - Can manage domains and records"} - {role === "user" && "User - Can manage records only"} - {role === "readonly" && "Read-only - Can only view resources"} - - - ) - }} - /> - ))} - -
- )} - /> + ( - Expiration (Optional) - - - + Expiration + - Leave blank for a non-expiring token + Select how long this token should remain valid @@ -494,7 +465,7 @@ export default function ApiTokensPage() {
- Security note: Store this token securely. It provides access to your account based on the permissions you selected. + Security note: Store this token securely. It provides access to your account based on the role you selected.