From e4dba6d5c8ce030e559282260a01a3276b69228c Mon Sep 17 00:00:00 2001 From: Daniel Hougaard <62331820+DanielHougaard@users.noreply.github.com> Date: Tue, 12 Mar 2024 14:29:17 +0100 Subject: [PATCH] Fix: Formatting and support for selecting org (line 109-122) --- .../components/PasswordStep/PasswordStep.tsx | 342 +++++++++--------- 1 file changed, 177 insertions(+), 165 deletions(-) diff --git a/frontend/src/views/Login/components/PasswordStep/PasswordStep.tsx b/frontend/src/views/Login/components/PasswordStep/PasswordStep.tsx index 6924da3e7..32e5d29c9 100644 --- a/frontend/src/views/Login/components/PasswordStep/PasswordStep.tsx +++ b/frontend/src/views/Login/components/PasswordStep/PasswordStep.tsx @@ -1,8 +1,8 @@ import { useState } from "react"; import { useTranslation } from "react-i18next"; import Link from "next/link"; -import { useRouter } from "next/router" -import axios from "axios" +import { useRouter } from "next/router"; +import axios from "axios"; import jwt_decode from "jwt-decode"; import { useNotificationContext } from "@app/components/context/Notifications/NotificationProvider"; @@ -10,179 +10,191 @@ import attemptCliLogin from "@app/components/utilities/attemptCliLogin"; import attemptLogin from "@app/components/utilities/attemptLogin"; import { Button, Input } from "@app/components/v2"; import { useUpdateUserAuthMethods } from "@app/hooks/api"; +import { fetchOrganizations } from "@app/hooks/api/organization/queries"; import { fetchUserDetails } from "@app/hooks/api/users/queries"; -import { navigateUserToOrg } from "../../Login.utils"; +import { navigateUserToOrg, navigateUserToSelectOrg } from "../../Login.utils"; -type Props = { - providerAuthToken: string; - email: string; - password: string; - setPassword: (password: string) => void; - setStep: (step: number) => void; -} +type Props = { + providerAuthToken: string; + email: string; + password: string; + setPassword: (password: string) => void; + setStep: (step: number) => void; +}; export const PasswordStep = ({ - providerAuthToken, - email, - password, - setPassword, - setStep, + providerAuthToken, + email, + password, + setPassword, + setStep }: Props) => { - const { createNotification } = useNotificationContext(); - const [isLoading, setIsLoading] = useState(false); - const { t } = useTranslation(); - const router = useRouter(); - const { mutateAsync } = useUpdateUserAuthMethods(); - - const { - callbackPort, - isLinkingRequired, - authMethod, - organizationId - } = jwt_decode(providerAuthToken) as any; - - const handleLogin = async (e:React.FormEvent) => { - e.preventDefault() - try { - setIsLoading(true); - - if (callbackPort) { - // attemptCliLogin - const isCliLoginSuccessful = await attemptCliLogin({ - email, - password, - providerAuthToken - }) + const { createNotification } = useNotificationContext(); + const [isLoading, setIsLoading] = useState(false); + const { t } = useTranslation(); + const router = useRouter(); + const { mutateAsync } = useUpdateUserAuthMethods(); - if (isCliLoginSuccessful && isCliLoginSuccessful.success) { + const { callbackPort, isLinkingRequired, authMethod, organizationId } = jwt_decode( + providerAuthToken + ) as any; - if (isCliLoginSuccessful.mfaEnabled) { - // case: login requires MFA step - setStep(2); - setIsLoading(false); - return; - } - // case: login was successful - const cliUrl = `http://127.0.0.1:${callbackPort}/` + const handleLogin = async (e: React.FormEvent) => { + e.preventDefault(); + try { + setIsLoading(true); - // send request to server endpoint - const instance = axios.create() - await instance.post(cliUrl, { ...isCliLoginSuccessful.loginResponse }) + if (callbackPort) { + // attemptCliLogin + const isCliLoginSuccessful = await attemptCliLogin({ + email, + password, + providerAuthToken + }); - // cli page - router.push("/cli-redirect"); - - // on success, router.push to cli Login Successful page - } - } else { - const loginAttempt = await attemptLogin({ - email, - password, - providerAuthToken, - }); - - if (loginAttempt && loginAttempt.success) { - // case: login was successful - - if (loginAttempt.mfaEnabled) { - // TODO: deal with MFA - // case: login requires MFA step - setIsLoading(false); - setStep(2); - return; - } - - // case: login does not require MFA step - setIsLoading(false); - createNotification({ - text: "Successfully logged in", - type: "success" - }); - - if (isLinkingRequired) { - const user = await fetchUserDetails(); - const newAuthMethods = [...user.authMethods, authMethod] - await mutateAsync({ - authMethods: newAuthMethods - }); - } - - await navigateUserToOrg(router, organizationId); - } - } - } catch (err) { + if (isCliLoginSuccessful && isCliLoginSuccessful.success) { + if (isCliLoginSuccessful.mfaEnabled) { + // case: login requires MFA step + setStep(2); setIsLoading(false); - createNotification({ - text: "Login unsuccessful. Double-check your master password and try again.", - type: "error" - }); - console.error(err); + return; + } + // case: login was successful + const cliUrl = `http://127.0.0.1:${callbackPort}/`; + + // send request to server endpoint + const instance = axios.create(); + await instance.post(cliUrl, { ...isCliLoginSuccessful.loginResponse }); + + // cli page + router.push("/cli-redirect"); + + // on success, router.push to cli Login Successful page } - }; - - return ( -
+ ); +};