fix(dashboard): creation of new org when user is apart of no orgs

This commit is contained in:
Daniel Hougaard
2024-12-16 15:55:14 +01:00
parent c00f6601bd
commit 181ba75f2a
8 changed files with 89 additions and 54 deletions
+7 -2
View File
@@ -77,11 +77,16 @@ export const selectOrganization = async (data: {
export const useSelectOrganization = () => {
const queryClient = useQueryClient();
return useMutation({
mutationFn: async (details: { organizationId: string; userAgent?: UserAgentType }) => {
mutationFn: async (details: {
organizationId: string;
userAgent?: UserAgentType;
forceSetCredentials?: boolean;
}) => {
const data = await selectOrganization(details);
// If a custom user agent is set, then this session is meant for another consuming application, not the web application.
if (!details.userAgent && !data.isMfaEnabled) {
if ((!details.userAgent && !data.isMfaEnabled) || details.forceSetCredentials) {
localStorage.setItem("orgData.id", details.organizationId);
SecurityClient.setToken(data.token);
SecurityClient.setProviderAuthToken("");
}
@@ -1,5 +1,6 @@
import { useMutation, useQuery, useQueryClient, UseQueryOptions } from "@tanstack/react-query";
import SecurityClient from "@app/components/utilities/SecurityClient";
import { apiRequest } from "@app/config/request";
import { OrderByDirection } from "@app/hooks/api/generic/types";
@@ -67,7 +68,7 @@ export const useCreateOrg = (options: { invalidate: boolean } = { invalidate: tr
mutationFn: async ({ name }: { name: string }) => {
const {
data: { organization }
} = await apiRequest.post("/api/v2/organizations", {
} = await apiRequest.post<{ organization: { id: string } }>("/api/v2/organizations", {
name
});
@@ -437,10 +438,13 @@ export const useDeleteOrgById = () => {
return useMutation({
mutationFn: async ({ organizationId }: { organizationId: string }) => {
const {
data: { organization }
} = await apiRequest.delete<{ organization: Organization }>(
data: { organization, accessToken }
} = await apiRequest.delete<{ organization: Organization; accessToken: string }>(
`/api/v2/organizations/${organizationId}`
);
SecurityClient.setToken(accessToken);
localStorage.removeItem("orgData.id");
return organization;
},
onSuccess(_, dto) {
+1 -1
View File
@@ -7,7 +7,7 @@ import { ProjectType } from "@app/hooks/api/workspace/types";
import { queryClient } from "@app/reactQuery";
export const navigateUserToOrg = async (router: NextRouter, organizationId?: string) => {
const userOrgs = await fetchOrganizations();
const userOrgs = await fetchOrganizations().catch(() => []);
const nonAuthEnforcedOrgs = userOrgs.filter((org) => !org.authEnforced);
@@ -6,7 +6,7 @@ import z from "zod";
import { createNotification } from "@app/components/notifications";
import { Button, FormControl, Input, Modal, ModalContent } from "@app/components/v2";
import { useCreateOrg, useSelectOrganization } from "@app/hooks/api";
import { useCreateOrg, useGetOrganizations, useSelectOrganization } from "@app/hooks/api";
import { ProjectType } from "@app/hooks/api/workspace/types";
const schema = z
@@ -23,9 +23,10 @@ interface CreateOrgModalProps {
}
export const CreateOrgModal: FC<CreateOrgModalProps> = ({ isOpen, onClose }) => {
const router = useRouter();
const { refetch: refetchOrganizations } = useGetOrganizations();
const {
control,
handleSubmit,
@@ -50,19 +51,21 @@ export const CreateOrgModal: FC<CreateOrgModalProps> = ({ isOpen, onClose }) =>
});
await selectOrg({
organizationId: organization.id
organizationId: organization.id,
forceSetCredentials: true
});
await refetchOrganizations();
createNotification({
text: "Successfully created organization",
type: "success"
});
if (router.isReady) router.push(`/org/${organization.id}/${ProjectType.SecretManager}/overview`);
if (router.isReady)
router.push(`/org/${organization.id}/${ProjectType.SecretManager}/overview`);
else window.location.href = `/org/${organization.id}/${ProjectType.SecretManager}/overview`;
localStorage.setItem("orgData.id", organization.id);
reset();
onClose();
} catch (err) {