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.

Select the default workspace

Kiran K05675455a140
diff --git a/apps/web/app/app.dub.co/(auth)/oauth/authorize/authorize-form.tsx b/apps/web/app/app.dub.co/(auth)/oauth/authorize/authorize-form.tsx
index 88fc4769c2..7fbb736666 100644
--- a/apps/web/app/app.dub.co/(auth)/oauth/authorize/authorize-form.tsx
+++ b/apps/web/app/app.dub.co/(auth)/oauth/authorize/authorize-form.tsx
@@ -1,12 +1,13 @@
"use client";
+import useUser from "@/lib/swr/use-user";
import useWorkspaces from "@/lib/swr/use-workspaces";
import z from "@/lib/zod";
import { authorizeRequestSchema } from "@/lib/zod/schemas/oauth";
import { Button, InputSelect, InputSelectItemProps } from "@dub/ui";
import { DICEBEAR_AVATAR_URL } from "@dub/utils";
import { OAuthApp } from "@prisma/client";
-import { useMemo, useState } from "react";
+import { useEffect, useMemo, useState } from "react";
import { toast } from "sonner";
interface AuthorizeFormProps extends z.infer<typeof authorizeRequestSchema> {
@@ -22,6 +23,7 @@ export const AuthorizeForm = ({
code_challenge,
code_challenge_method,
}: AuthorizeFormProps) => {
+ const { user } = useUser();
const { workspaces } = useWorkspaces();
const [submitting, setSubmitting] = useState(false);
const [selectedWorkspace, setSelectedWorkspace] =
@@ -30,13 +32,20 @@ export const AuthorizeForm = ({
const selectOptions = useMemo(() => {
return workspaces
? workspaces.map((workspace) => ({
- id: workspace.id,
+ id: workspace.slug,
value: workspace.name,
image: workspace.logo || `${DICEBEAR_AVATAR_URL}${workspace.name}`,
}))
: [];
}, [workspaces]);
+ useEffect(() => {
+ setSelectedWorkspace(
+ selectOptions.find((option) => option.id === user?.defaultWorkspace) ||
+ null,
+ );
+ }, [selectOptions, user]);
+
// Decline the request
const onDecline = () => {
const searchParams = new URLSearchParams({
@@ -50,10 +59,20 @@ export const AuthorizeForm = ({
// Approve the
const onAuthorize = async (e: React.FormEvent<HTMLFormElement>) => {
e.preventDefault();
+
+ if (!selectedWorkspace) {
+ toast.error("Please select a workspace to continue");
+ return;
+ }
+
setSubmitting(true);
+ const workspaceId = workspaces?.find(
+ (workspace) => workspace.slug === selectedWorkspace.id,
+ )?.id;
+
const response = await fetch(
- `/api/oauth/authorize?workspaceId=${selectedWorkspace?.id}`,
+ `/api/oauth/authorize?workspaceId=${workspaceId}`,
{
method: "POST",
body: JSON.stringify(Object.fromEntries(new FormData(e.currentTarget))),