No need for custom hook, logic only used on AssumePage

Signed-off-by: Émile Ré <emile@getprobo.com>
This commit is contained in:
Émile Ré
2026-02-10 18:21:46 +04:00
parent 795b0a8de2
commit 0fca372ed4
5 changed files with 95 additions and 114 deletions

View File

@@ -36,7 +36,6 @@
}, },
"sources": { "sources": {
"apps/console/src/pages/iam": "iam", "apps/console/src/pages/iam": "iam",
"apps/console/src/hooks/iam": "iam",
"apps/console/src": "core" "apps/console/src": "core"
} }
} }

View File

@@ -1,5 +1,5 @@
/** /**
* @generated SignedSource<<b5a89cb2acee801ab7d41cf2afb471b6>> * @generated SignedSource<<754086efd418674e8d92f629f7a01736>>
* @lightSyntaxTransform * @lightSyntaxTransform
* @nogrep * @nogrep
*/ */
@@ -13,10 +13,10 @@ export type ReauthenticationReason = "POLICY_REQUIREMENT" | "SENSITIVE_ACTION" |
export type AssumeOrganizationSessionInput = { export type AssumeOrganizationSessionInput = {
organizationId: string; organizationId: string;
}; };
export type useAssumeMutation$variables = { export type AssumePageMutation$variables = {
input: AssumeOrganizationSessionInput; input: AssumeOrganizationSessionInput;
}; };
export type useAssumeMutation$data = { export type AssumePageMutation$data = {
readonly assumeOrganizationSession: { readonly assumeOrganizationSession: {
readonly result: { readonly result: {
readonly __typename: "OrganizationSessionCreated"; readonly __typename: "OrganizationSessionCreated";
@@ -41,9 +41,9 @@ export type useAssumeMutation$data = {
}; };
} | null | undefined; } | null | undefined;
}; };
export type useAssumeMutation = { export type AssumePageMutation = {
response: useAssumeMutation$data; response: AssumePageMutation$data;
variables: useAssumeMutation$variables; variables: AssumePageMutation$variables;
}; };
const node: ConcreteRequest = (function(){ const node: ConcreteRequest = (function(){
@@ -171,7 +171,7 @@ return {
"argumentDefinitions": (v0/*: any*/), "argumentDefinitions": (v0/*: any*/),
"kind": "Fragment", "kind": "Fragment",
"metadata": null, "metadata": null,
"name": "useAssumeMutation", "name": "AssumePageMutation",
"selections": (v3/*: any*/), "selections": (v3/*: any*/),
"type": "Mutation", "type": "Mutation",
"abstractKey": null "abstractKey": null
@@ -180,20 +180,20 @@ return {
"operation": { "operation": {
"argumentDefinitions": (v0/*: any*/), "argumentDefinitions": (v0/*: any*/),
"kind": "Operation", "kind": "Operation",
"name": "useAssumeMutation", "name": "AssumePageMutation",
"selections": (v3/*: any*/) "selections": (v3/*: any*/)
}, },
"params": { "params": {
"cacheID": "6c65712730108428976b1d0007ca2993", "cacheID": "cc56faddf33aa8f6b84f139f262647f6",
"id": null, "id": null,
"metadata": {}, "metadata": {},
"name": "useAssumeMutation", "name": "AssumePageMutation",
"operationKind": "mutation", "operationKind": "mutation",
"text": "mutation useAssumeMutation(\n $input: AssumeOrganizationSessionInput!\n) {\n assumeOrganizationSession(input: $input) {\n result {\n __typename\n ... on OrganizationSessionCreated {\n membership {\n id\n lastSession {\n id\n expiresAt\n }\n }\n }\n ... on PasswordRequired {\n reason\n }\n ... on SAMLAuthenticationRequired {\n reason\n redirectUrl\n }\n }\n }\n}\n" "text": "mutation AssumePageMutation(\n $input: AssumeOrganizationSessionInput!\n) {\n assumeOrganizationSession(input: $input) {\n result {\n __typename\n ... on OrganizationSessionCreated {\n membership {\n id\n lastSession {\n id\n expiresAt\n }\n }\n }\n ... on PasswordRequired {\n reason\n }\n ... on SAMLAuthenticationRequired {\n reason\n redirectUrl\n }\n }\n }\n}\n"
} }
}; };
})(); })();
(node as any).hash = "1ccd539abfc276084939857e65421be9"; (node as any).hash = "e5799d78e6d954273c07db7c022b31e7";
export default node; export default node;

View File

@@ -1,94 +0,0 @@
import { UnAuthenticatedError } from "@probo/relay";
import { useEffect } from "react";
import { useMutation } from "react-relay";
import { useNavigate } from "react-router";
import { graphql } from "relay-runtime";
import type { useAssumeMutation } from "#/__generated__/iam/useAssumeMutation.graphql";
import { useOrganizationId } from "../useOrganizationId";
interface UseAssumeParameters {
afterAssumePath?: string;
onSuccess: () => void;
}
const assumeMutation = graphql`
mutation useAssumeMutation(
$input: AssumeOrganizationSessionInput!
) {
assumeOrganizationSession(input: $input) {
result {
__typename
... on OrganizationSessionCreated {
membership {
id
lastSession {
id
expiresAt
}
}
}
... on PasswordRequired {
reason
}
... on SAMLAuthenticationRequired {
reason
redirectUrl
}
}
}
}
`;
export function useAssume(params: UseAssumeParameters) {
const { afterAssumePath, onSuccess } = params;
const organizationId = useOrganizationId();
const navigate = useNavigate();
const [assumeOrganizationSession] = useMutation<useAssumeMutation>(assumeMutation);
useEffect(() => {
assumeOrganizationSession({
variables: {
input: { organizationId },
},
onError: (error) => {
if (error instanceof UnAuthenticatedError) {
const search = new URLSearchParams([
["organization-id", organizationId],
["redirect-path", afterAssumePath ?? window.location.pathname + window.location.search],
]);
void navigate({ pathname: "/auth/login", search: "?" + search.toString() });
return;
}
},
onCompleted: ({ assumeOrganizationSession }) => {
if (!assumeOrganizationSession) {
throw new Error("complete mutation result is empty");
}
const { result } = assumeOrganizationSession;
const search = new URLSearchParams();
switch (result.__typename) {
case "PasswordRequired":
search.set("organization-id", organizationId);
search.set("redirect-path", afterAssumePath ?? window.location.pathname + window.location.search);
void navigate({ pathname: "/auth/passord-login", search: "?" + search.toString() });
break;
case "SAMLAuthenticationRequired":
window.location.href = result.redirectUrl;
break;
default:
onSuccess();
}
},
});
}, [afterAssumePath, organizationId, onSuccess, navigate, assumeOrganizationSession]);
return;
}

View File

@@ -1,20 +1,98 @@
import { useTranslate } from "@probo/i18n"; import { useTranslate } from "@probo/i18n";
import { UnAuthenticatedError } from "@probo/relay";
import { useEffect } from "react";
import { useMutation } from "react-relay";
import { useNavigate, useSearchParams } from "react-router"; import { useNavigate, useSearchParams } from "react-router";
import { graphql } from "relay-runtime";
import { useAssume } from "#/hooks/iam/useAssume"; import type { AssumePageMutation } from "#/__generated__/iam/AssumePageMutation.graphql";
import { useOrganizationId } from "#/hooks/useOrganizationId";
import { IAMRelayProvider } from "#/providers/IAMRelayProvider"; import { IAMRelayProvider } from "#/providers/IAMRelayProvider";
import AuthLayout from "../auth/AuthLayout"; import AuthLayout from "../auth/AuthLayout";
const assumeMutation = graphql`
mutation AssumePageMutation(
$input: AssumeOrganizationSessionInput!
) {
assumeOrganizationSession(input: $input) {
result {
__typename
... on OrganizationSessionCreated {
membership {
id
lastSession {
id
expiresAt
}
}
}
... on PasswordRequired {
reason
}
... on SAMLAuthenticationRequired {
reason
redirectUrl
}
}
}
}
`;
function AssumePageInner() { function AssumePageInner() {
const organizationId = useOrganizationId();
const navigate = useNavigate(); const navigate = useNavigate();
const [searchParams] = useSearchParams(); const [searchParams] = useSearchParams();
const { __ } = useTranslate(); const { __ } = useTranslate();
useAssume({ const [assumeOrganizationSession] = useMutation<AssumePageMutation>(assumeMutation);
afterAssumePath: searchParams.get("redirect-path") ?? "/",
onSuccess: () => void navigate(searchParams.get("redirect-path") ?? "/"), useEffect(() => {
assumeOrganizationSession({
variables: {
input: { organizationId },
},
onError: (error) => {
if (error instanceof UnAuthenticatedError) {
const search = new URLSearchParams([
["organization-id", organizationId],
[
"redirect-path",
searchParams.get("redirect-path") ?? window.location.pathname + window.location.search,
],
]);
void navigate({ pathname: "/auth/login", search: "?" + search.toString() });
return;
}
},
onCompleted: ({ assumeOrganizationSession }) => {
if (!assumeOrganizationSession) {
throw new Error("complete mutation result is empty");
}
const { result } = assumeOrganizationSession;
const search = new URLSearchParams();
switch (result.__typename) {
case "PasswordRequired":
search.set("organization-id", organizationId);
search.set(
"redirect-path",
searchParams.get("redirect-path") ?? window.location.pathname + window.location.search,
);
void navigate({ pathname: "/auth/passord-login", search: "?" + search.toString() });
break;
case "SAMLAuthenticationRequired":
window.location.href = result.redirectUrl;
break;
default:
void navigate(searchParams.get("redirect-path") ?? window.location.pathname + window.location.search);
}
},
}); });
}, [organizationId, navigate, assumeOrganizationSession, searchParams]);
return ( return (
<AuthLayout> <AuthLayout>

View File

@@ -9,7 +9,6 @@ export default defineConfig({
plugins: [ plugins: [
react({ react({
exclude: [ exclude: [
"src/hooks/iam/**/*",
"src/pages/iam/**/*", "src/pages/iam/**/*",
"src/components/connectors/**/*", "src/components/connectors/**/*",
], ],
@@ -27,7 +26,6 @@ export default defineConfig({
}), }),
react({ react({
include: [ include: [
"src/hooks/iam/**/*",
"src/pages/iam/**/*", "src/pages/iam/**/*",
"src/components/connectors/**/*", "src/components/connectors/**/*",
], ],