Skip to content

armgit / dub

Public Imported from GitHub
0

The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.

some cleanup

Kiran K6b59b4aabe7b
diff --git a/apps/web/app/app.dub.co/(dashboard)/[slug]/settings/tokens/page-client.tsx b/apps/web/app/app.dub.co/(dashboard)/[slug]/settings/tokens/page-client.tsx
index 23830fb207..0cc013ffbe 100644
--- a/apps/web/app/app.dub.co/(dashboard)/[slug]/settings/tokens/page-client.tsx
+++ b/apps/web/app/app.dub.co/(dashboard)/[slug]/settings/tokens/page-client.tsx
@@ -1,6 +1,6 @@
"use client";
-import { mapScopeToResource, scopesToName } from "@/lib/api/tokens/scopes";
+import { scopesToName } from "@/lib/api/tokens/scopes";
import useWorkspace from "@/lib/swr/use-workspace";
import { TokenProps } from "@/lib/types";
import { useAddEditTokenModal } from "@/ui/modals/add-edit-token-modal";
@@ -99,7 +99,7 @@ const TokenRow = (token: TokenProps) => {
id: token.id,
name: token.name,
isMachine: token.user.isMachine,
- scopes: mapScopeToResource(token.scopes),
+ scopes: token.scopes,
},
});
diff --git a/apps/web/lib/zod/schemas/oauth.ts b/apps/web/lib/zod/schemas/oauth.ts
index 981ef1aa53..1c1d6b183b 100644
--- a/apps/web/lib/zod/schemas/oauth.ts
+++ b/apps/web/lib/zod/schemas/oauth.ts
@@ -17,8 +17,29 @@ export const oAuthClientSchema = z.object({
export const createOAuthClientSchema = z.object({
name: z.string().min(1).max(255),
developer: z.string().min(1).max(255),
- website: z.string().url().max(255),
- redirectUri: z.string().url().max(255),
+ website: z
+ .string()
+ .url({
+ message: "website must be a valid URL",
+ })
+ .max(255),
+ redirectUri: z
+ .string()
+ .url({ message: "redirect_uri must be a valid URL" })
+ .max(255)
+ .refine(
+ (url) => {
+ if (url.startsWith("http://localhost")) {
+ return true;
+ }
+
+ return url.startsWith("https://");
+ },
+ {
+ message:
+ "redirect_uri must be a valid URL starting with 'https://' except for 'http://localhost'",
+ },
+ ),
scopes: z
.array(z.enum(availableScopes))
.min(1, "An OAuth app must have at least one scope"),
diff --git a/apps/web/ui/modals/add-edit-token-modal.tsx b/apps/web/ui/modals/add-edit-token-modal.tsx
index 01fa11878e..7ed35ec5ef 100644
--- a/apps/web/ui/modals/add-edit-token-modal.tsx
+++ b/apps/web/ui/modals/add-edit-token-modal.tsx
@@ -1,5 +1,7 @@
import {
+ ResourceScopeMapping,
Scope,
+ mapScopeToResource,
permissionsByResource,
resources,
scopePresets,
@@ -34,7 +36,7 @@ import { mutate } from "swr";
type APIKeyProps = {
id?: string;
name: string;
- scopes: { [key: string]: Scope };
+ scopes: string[];
isMachine: boolean;
};
@@ -42,7 +44,7 @@ type ScopePreset = "all_access" | "read_only" | "restricted";
const newToken: APIKeyProps = {
name: "",
- scopes: { api: "apis.all" },
+ scopes: [],
isMachine: false,
};
@@ -61,6 +63,9 @@ function AddEditTokenModal({
const { id: workspaceId, logo, slug, betaTester } = useWorkspace();
const [data, setData] = useState<APIKeyProps>(token || newToken);
const [preset, setPreset] = useState<ScopePreset>("all_access");
+ const [selectedScopes, setSelectedScopes] = useState<
+ ResourceScopeMapping | {}
+ >(mapScopeToResource(token?.scopes || []));
useEffect(() => {
if (!token) return;
@@ -105,7 +110,7 @@ function AddEditTokenModal({
},
body: JSON.stringify({
...data,
- scopes: Object.values(scopes).filter((v) => v),
+ scopes: Object.values(selectedScopes).filter((v) => v),
}),
});
@@ -123,7 +128,7 @@ function AddEditTokenModal({
const { name, scopes } = data;
const buttonDisabled =
- (!name || token?.name === name) && token?.scopes === scopes;
+ (!name || token?.name === name) && selectedScopes === token?.scopes;
return (
<>
@@ -234,11 +239,11 @@ function AddEditTokenModal({
setPreset(value);
if (value === "all_access") {
- setData({ ...data, scopes: { api: "apis.all" } });
+ setSelectedScopes(["apis.all"]);
} else if (value === "read_only") {
- setData({ ...data, scopes: { api: "apis.read" } });
+ setSelectedScopes(["apis.read"]);
} else {
- setData({ ...data, scopes: {} });
+ setSelectedScopes([]);
}
}}
/>
@@ -265,15 +270,12 @@ function AddEditTokenModal({
</div>
<div>
<RadioGroup
- defaultValue={scopes[resource.key] || ""}
+ defaultValue={selectedScopes[resource.key] || ""}
className="flex gap-4"
onValueChange={(v: Scope) => {
- setData({
- ...data,
- scopes: {
- ...scopes,
- [resource.key]: v,
- },
+ setSelectedScopes({
+ ...selectedScopes,
+ [resource.key]: v,
});
}}
>