···179179 // });
180180 // }
181181182182- const activeWorkspace = usersToWorkspaces?.find(({ workspace }) => {
183183- // If there is a workspace slug in the cookie, use it to find the workspace
184184- if (workspaceSlug) return workspace.slug === workspaceSlug;
185185- return true;
186186- })?.workspace;
182182+ // NOTE: if no workspace slug fit (cookie manipulation), use the first workspace
183183+ const activeWorkspace =
184184+ usersToWorkspaces?.find(({ workspace }) => {
185185+ // If there is a workspace slug in the cookie, use it to find the workspace
186186+ if (workspaceSlug) return workspace.slug === workspaceSlug;
187187+ return true;
188188+ })?.workspace ?? usersToWorkspaces?.[0]?.workspace;
187189188190 if (!activeWorkspace) {
189191 throw new TRPCError({
190192 code: "UNAUTHORIZED",
191193 message: "Workspace Not Found",
192194 });
195195+ }
196196+197197+ if (activeWorkspace.slug !== workspaceSlug) {
198198+ // properly set the workspace slug cookie
199199+ ctx.req?.cookies.set("workspace-slug", activeWorkspace.slug);
193200 }
194201195202 if (!userProps) {