Convert navigate function to hook

This commit is contained in:
Daniel Hougaard
2024-06-24 19:08:19 +02:00
parent 1930d40be8
commit 45da563465
3 changed files with 33 additions and 21 deletions
+4 -5
View File
@@ -1,16 +1,15 @@
import { useEffect, useState } from "react"; import { useEffect, useState } from "react";
import { useRouter } from "next/router";
import { isLoggedIn } from "@app/reactQuery"; import { isLoggedIn } from "@app/reactQuery";
import { InitialStep, MFAStep, SSOStep } from "./components"; import { InitialStep, MFAStep, SSOStep } from "./components";
import { navigateUserToSelectOrg } from "./Login.utils"; import { useNavigateToSelectOrganization } from "./Login.utils";
export const Login = () => { export const Login = () => {
const router = useRouter();
const [step, setStep] = useState(0); const [step, setStep] = useState(0);
const [email, setEmail] = useState(""); const [email, setEmail] = useState("");
const [password, setPassword] = useState(""); const [password, setPassword] = useState("");
const { navigateToSelectOrganization } = useNavigateToSelectOrganization();
const queryParams = new URLSearchParams(window.location.search); const queryParams = new URLSearchParams(window.location.search);
@@ -21,10 +20,10 @@ export const Login = () => {
const callbackPort = queryParams?.get("callback_port"); const callbackPort = queryParams?.get("callback_port");
// case: a callback port is set, meaning it's a cli login request: redirect to select org with callback port // case: a callback port is set, meaning it's a cli login request: redirect to select org with callback port
if (callbackPort) { if (callbackPort) {
navigateUserToSelectOrg(router, callbackPort); navigateToSelectOrganization(callbackPort);
} else { } else {
// case: no callback port, meaning it's a regular login request: redirect to select org // case: no callback port, meaning it's a regular login request: redirect to select org
navigateUserToSelectOrg(router); navigateToSelectOrganization();
} }
} catch (error) { } catch (error) {
console.log("Error - Not logged in yet"); console.log("Error - Not logged in yet");
@@ -1,4 +1,4 @@
import { FormEvent, useEffect, useRef, useState } from "react"; import { FormEvent, useCallback, useEffect, useRef, useState } from "react";
import { useTranslation } from "react-i18next"; import { useTranslation } from "react-i18next";
import Link from "next/link"; import Link from "next/link";
import { useRouter } from "next/router"; import { useRouter } from "next/router";
@@ -16,7 +16,7 @@ import { Button, Input } from "@app/components/v2";
import { useServerConfig } from "@app/context"; import { useServerConfig } from "@app/context";
import { useFetchServerStatus } from "@app/hooks/api"; import { useFetchServerStatus } from "@app/hooks/api";
import { navigateUserToSelectOrg } from "../../Login.utils"; import { useNavigateToSelectOrganization } from "../../Login.utils";
type Props = { type Props = {
setStep: (step: number) => void; setStep: (step: number) => void;
@@ -39,16 +39,28 @@ export const InitialStep = ({ setStep, email, setEmail, password, setPassword }:
const captchaRef = useRef<HCaptcha>(null); const captchaRef = useRef<HCaptcha>(null);
const { data: serverDetails } = useFetchServerStatus(); const { data: serverDetails } = useFetchServerStatus();
const { navigateToSelectOrganization } = useNavigateToSelectOrganization();
const redirectToSaml = (orgSlug: string) => {
const callbackPort = queryParams.get("callback_port");
const redirectUrl = `/api/v1/sso/redirect/saml2/organizations/${orgSlug}${
callbackPort ? `?callback_port=${callbackPort}` : ""
}`;
router.push(redirectUrl);
};
useEffect(() => { useEffect(() => {
if (serverDetails?.samlDefaultOrgSlug) { if (serverDetails?.samlDefaultOrgSlug) redirectToSaml(serverDetails.samlDefaultOrgSlug);
const callbackPort = queryParams.get("callback_port");
const redirectUrl = `/api/v1/sso/redirect/saml2/organizations/${
serverDetails?.samlDefaultOrgSlug
}${callbackPort ? `?callback_port=${callbackPort}` : ""}`;
router.push(redirectUrl);
}
}, [serverDetails?.samlDefaultOrgSlug]); }, [serverDetails?.samlDefaultOrgSlug]);
const handleSaml = useCallback((step: number) => {
if (config.defaultOrgSlug) {
redirectToSaml(config.defaultOrgSlug);
} else {
setStep(step);
}
}, []);
const handleLogin = async (e: FormEvent<HTMLFormElement>) => { const handleLogin = async (e: FormEvent<HTMLFormElement>) => {
e.preventDefault(); e.preventDefault();
try { try {
@@ -75,7 +87,7 @@ export const InitialStep = ({ setStep, email, setEmail, password, setPassword }:
return; return;
} }
navigateUserToSelectOrg(router, callbackPort!); navigateToSelectOrganization(callbackPort!);
} else { } else {
setLoginError(true); setLoginError(true);
createNotification({ createNotification({
@@ -100,7 +112,7 @@ export const InitialStep = ({ setStep, email, setEmail, password, setPassword }:
return; return;
} }
navigateUserToSelectOrg(router); navigateToSelectOrganization();
// case: login does not require MFA step // case: login does not require MFA step
createNotification({ createNotification({
@@ -211,7 +223,7 @@ export const InitialStep = ({ setStep, email, setEmail, password, setPassword }:
colorSchema="primary" colorSchema="primary"
variant="outline_bg" variant="outline_bg"
onClick={() => { onClick={() => {
setStep(2); handleSaml(2);
}} }}
leftIcon={<FontAwesomeIcon icon={faLock} className="mr-2" />} leftIcon={<FontAwesomeIcon icon={faLock} className="mr-2" />}
className="mx-0 h-10 w-full" className="mx-0 h-10 w-full"
@@ -16,7 +16,7 @@ import { useSelectOrganization, verifyMfaToken } from "@app/hooks/api/auth/queri
import { fetchOrganizations } from "@app/hooks/api/organization/queries"; import { fetchOrganizations } from "@app/hooks/api/organization/queries";
import { fetchMyPrivateKey } from "@app/hooks/api/users/queries"; import { fetchMyPrivateKey } from "@app/hooks/api/users/queries";
import { navigateUserToOrg, navigateUserToSelectOrg } from "../../Login.utils"; import { navigateUserToOrg, useNavigateToSelectOrganization } from "../../Login.utils";
// The style for the verification code input // The style for the verification code input
const props = { const props = {
@@ -50,6 +50,7 @@ export const MFAStep = ({ email, password, providerAuthToken }: Props) => {
const [isLoading, setIsLoading] = useState(false); const [isLoading, setIsLoading] = useState(false);
const [isLoadingResend, setIsLoadingResend] = useState(false); const [isLoadingResend, setIsLoadingResend] = useState(false);
const [mfaCode, setMfaCode] = useState(""); const [mfaCode, setMfaCode] = useState("");
const { navigateToSelectOrganization } = useNavigateToSelectOrganization();
const [triesLeft, setTriesLeft] = useState<number | undefined>(undefined); const [triesLeft, setTriesLeft] = useState<number | undefined>(undefined);
const { t } = useTranslation(); const { t } = useTranslation();
@@ -93,7 +94,7 @@ export const MFAStep = ({ email, password, providerAuthToken }: Props) => {
// case: user has orgs, so we navigate the user to select an org // case: user has orgs, so we navigate the user to select an org
if (userOrgs.length > 0) { if (userOrgs.length > 0) {
navigateUserToSelectOrg(router, callbackPort); navigateToSelectOrganization(callbackPort);
} }
// case: no orgs found, so we navigate the user to create an org // case: no orgs found, so we navigate the user to create an org
// cli login will fail in this case // cli login will fail in this case
@@ -166,7 +167,7 @@ export const MFAStep = ({ email, password, providerAuthToken }: Props) => {
// case: user has orgs, so we navigate the user to select an org // case: user has orgs, so we navigate the user to select an org
if (userOrgs.length > 0) { if (userOrgs.length > 0) {
navigateUserToSelectOrg(router, callbackPort); navigateToSelectOrganization(callbackPort);
} }
// case: no orgs found, so we navigate the user to create an org // case: no orgs found, so we navigate the user to create an org
// cli login will fail in this case // cli login will fail in this case
@@ -195,7 +196,7 @@ export const MFAStep = ({ email, password, providerAuthToken }: Props) => {
if (organizationId) { if (organizationId) {
await navigateUserToOrg(router, organizationId); await navigateUserToOrg(router, organizationId);
} else { } else {
navigateUserToSelectOrg(router); navigateToSelectOrganization();
} }
} else { } else {
createNotification({ createNotification({