fix: suborg routing

This commit is contained in:
Piyush Gupta
2025-11-27 03:40:43 +05:30
parent 85f70e5e20
commit e98b35fc0a
4 changed files with 255 additions and 0 deletions
+43
View File
@@ -107,6 +107,49 @@ export const useSelectOrganization = () => {
});
};
export const selectSubOrganization = async (data: {
subOrganizationId: string;
userAgent?: UserAgentType;
}) => {
const { data: res } = await apiRequest.post<{
token: string;
isMfaEnabled: boolean;
mfaMethod?: MfaMethod;
subOrganization?: {
id: string;
name: string;
slug: string;
};
}>("/api/v3/auth/select-sub-organization", data);
return res;
};
export const useSelectSubOrganization = () => {
const queryClient = useQueryClient();
return useMutation({
mutationFn: async (details: { subOrganizationId: string; userAgent?: UserAgentType }) => {
const data = await selectSubOrganization(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) {
SecurityClient.setToken(data.token);
SecurityClient.setProviderAuthToken("");
}
if (data.token && !data.isMfaEnabled) {
setAuthToken(data.token);
}
return data;
},
onSuccess: () => {
queryClient.invalidateQueries({
queryKey: [organizationKeys.getUserOrganizations, projectKeys.getAllUserProjects]
});
}
});
};
export const useLogin2 = () => {
return useMutation({
mutationFn: async (details: {
@@ -196,6 +196,8 @@ export const Navbar = () => {
const breadcrumbs = matches && "breadcrumbs" in matches ? matches.breadcrumbs : undefined;
const handleOrgChange = async (orgId: string, onSuccess?: () => void | Promise<void>) => {
if (orgId === currentOrg.id) return;
const { token, isMfaEnabled, mfaMethod } = await selectOrganization({
organizationId: orgId
});
@@ -243,6 +245,8 @@ export const Navbar = () => {
};
const handleSubOrgChange = async (subOrgId: string) => {
if (subOrgId === currentOrg.id) return;
const { token, isMfaEnabled, mfaMethod } = await selectSubOrganization({
subOrganizationId: subOrgId
});