No need for custom hook, logic only used on AssumePage
Signed-off-by: Émile Ré <emile@getprobo.com>
This commit is contained in:
@@ -36,7 +36,6 @@
|
||||
},
|
||||
"sources": {
|
||||
"apps/console/src/pages/iam": "iam",
|
||||
"apps/console/src/hooks/iam": "iam",
|
||||
"apps/console/src": "core"
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/**
|
||||
* @generated SignedSource<<b5a89cb2acee801ab7d41cf2afb471b6>>
|
||||
* @generated SignedSource<<754086efd418674e8d92f629f7a01736>>
|
||||
* @lightSyntaxTransform
|
||||
* @nogrep
|
||||
*/
|
||||
@@ -13,10 +13,10 @@ export type ReauthenticationReason = "POLICY_REQUIREMENT" | "SENSITIVE_ACTION" |
|
||||
export type AssumeOrganizationSessionInput = {
|
||||
organizationId: string;
|
||||
};
|
||||
export type useAssumeMutation$variables = {
|
||||
export type AssumePageMutation$variables = {
|
||||
input: AssumeOrganizationSessionInput;
|
||||
};
|
||||
export type useAssumeMutation$data = {
|
||||
export type AssumePageMutation$data = {
|
||||
readonly assumeOrganizationSession: {
|
||||
readonly result: {
|
||||
readonly __typename: "OrganizationSessionCreated";
|
||||
@@ -41,9 +41,9 @@ export type useAssumeMutation$data = {
|
||||
};
|
||||
} | null | undefined;
|
||||
};
|
||||
export type useAssumeMutation = {
|
||||
response: useAssumeMutation$data;
|
||||
variables: useAssumeMutation$variables;
|
||||
export type AssumePageMutation = {
|
||||
response: AssumePageMutation$data;
|
||||
variables: AssumePageMutation$variables;
|
||||
};
|
||||
|
||||
const node: ConcreteRequest = (function(){
|
||||
@@ -171,7 +171,7 @@ return {
|
||||
"argumentDefinitions": (v0/*: any*/),
|
||||
"kind": "Fragment",
|
||||
"metadata": null,
|
||||
"name": "useAssumeMutation",
|
||||
"name": "AssumePageMutation",
|
||||
"selections": (v3/*: any*/),
|
||||
"type": "Mutation",
|
||||
"abstractKey": null
|
||||
@@ -180,20 +180,20 @@ return {
|
||||
"operation": {
|
||||
"argumentDefinitions": (v0/*: any*/),
|
||||
"kind": "Operation",
|
||||
"name": "useAssumeMutation",
|
||||
"name": "AssumePageMutation",
|
||||
"selections": (v3/*: any*/)
|
||||
},
|
||||
"params": {
|
||||
"cacheID": "6c65712730108428976b1d0007ca2993",
|
||||
"cacheID": "cc56faddf33aa8f6b84f139f262647f6",
|
||||
"id": null,
|
||||
"metadata": {},
|
||||
"name": "useAssumeMutation",
|
||||
"name": "AssumePageMutation",
|
||||
"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;
|
||||
@@ -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;
|
||||
}
|
||||
@@ -1,20 +1,98 @@
|
||||
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 { 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 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() {
|
||||
const organizationId = useOrganizationId();
|
||||
const navigate = useNavigate();
|
||||
const [searchParams] = useSearchParams();
|
||||
const { __ } = useTranslate();
|
||||
|
||||
useAssume({
|
||||
afterAssumePath: searchParams.get("redirect-path") ?? "/",
|
||||
onSuccess: () => void navigate(searchParams.get("redirect-path") ?? "/"),
|
||||
});
|
||||
const [assumeOrganizationSession] = useMutation<AssumePageMutation>(assumeMutation);
|
||||
|
||||
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 (
|
||||
<AuthLayout>
|
||||
|
||||
@@ -9,7 +9,6 @@ export default defineConfig({
|
||||
plugins: [
|
||||
react({
|
||||
exclude: [
|
||||
"src/hooks/iam/**/*",
|
||||
"src/pages/iam/**/*",
|
||||
"src/components/connectors/**/*",
|
||||
],
|
||||
@@ -27,7 +26,6 @@ export default defineConfig({
|
||||
}),
|
||||
react({
|
||||
include: [
|
||||
"src/hooks/iam/**/*",
|
||||
"src/pages/iam/**/*",
|
||||
"src/components/connectors/**/*",
|
||||
],
|
||||
|
||||
Reference in New Issue
Block a user