From 36170aa56427661d4efac59b041e56fbeb7830fc Mon Sep 17 00:00:00 2001 From: mistahj67 <26472282+mistahj67@users.noreply.github.com> Date: Wed, 6 Nov 2024 10:07:30 -0700 Subject: [PATCH] BED-4978 feat: add instant redirect if only 1 sso provider is configured (#932) --- cmd/ui/src/views/Login.tsx | 11 +-- .../components/LoginForm/LoginForm.test.tsx | 2 +- .../src/components/LoginForm/LoginForm.tsx | 0 .../src/components/LoginForm/index.ts | 0 .../LoginViaSSOForm/LoginViaSSOForm.test.tsx | 2 +- .../LoginViaSSOForm/LoginViaSSOForm.tsx | 16 ++--- .../bh-shared-ui/src/components/index.ts | 71 ++++++++++--------- 7 files changed, 54 insertions(+), 48 deletions(-) rename {cmd/ui => packages/javascript/bh-shared-ui}/src/components/LoginForm/LoginForm.test.tsx (98%) rename {cmd/ui => packages/javascript/bh-shared-ui}/src/components/LoginForm/LoginForm.tsx (100%) rename {cmd/ui => packages/javascript/bh-shared-ui}/src/components/LoginForm/index.ts (100%) diff --git a/cmd/ui/src/views/Login.tsx b/cmd/ui/src/views/Login.tsx index a7c831872a..ee9a978e59 100644 --- a/cmd/ui/src/views/Login.tsx +++ b/cmd/ui/src/views/Login.tsx @@ -16,11 +16,10 @@ import React, { useEffect, useState } from 'react'; import { Navigate } from 'react-router-dom'; -import LoginForm from 'src/components/LoginForm'; import LoginPage from 'src/components/LoginPage'; import { useQuery, useQueryClient } from 'react-query'; import { Box, CircularProgress } from '@mui/material'; -import { OneTimePasscodeForm, LoginViaSSOForm, apiClient } from 'bh-shared-ui'; +import { OneTimePasscodeForm, LoginViaSSOForm, LoginForm, apiClient } from 'bh-shared-ui'; import { login as loginAction, logout } from 'src/ducks/auth/authSlice'; import { ROUTE_HOME, ROUTE_USER_DISABLED } from 'src/ducks/global/routes'; @@ -67,8 +66,8 @@ const Login: React.FC = () => { dispatch(loginAction({ username: lastUsername, password: lastPassword, otp })); }; - const handleSubmitLoginViaSSOForm = (redirectURL: string) => { - window.location.assign(redirectURL); + const handleSubmitLoginViaSSOForm = (providerSlug: string) => { + window.location.assign(`/api/v2/sso/${providerSlug}/login`); }; /* Implementation */ @@ -113,6 +112,10 @@ const Login: React.FC = () => { } if (useSSO) { + if (listSSOProvidersQuery.data?.length === 1) { + handleSubmitLoginViaSSOForm(listSSOProvidersQuery.data[0].slug); + return; + } return ( { await user.click(screen.getByText(testSSOProviders[0].name)); expect(screen.getByRole('button', { name: /continue$/i })).not.toBeDisabled(); await user.click(screen.getByRole('button', { name: /continue$/i })); - expect(testOnSubmit).toHaveBeenCalledWith(`/api/v2/sso/${testSSOProviders[0].slug}/login`); + expect(testOnSubmit).toHaveBeenCalledWith(testSSOProviders[0].slug); }); it('should call onCancel when cancel button clicked', async () => { diff --git a/packages/javascript/bh-shared-ui/src/components/LoginViaSSOForm/LoginViaSSOForm.tsx b/packages/javascript/bh-shared-ui/src/components/LoginViaSSOForm/LoginViaSSOForm.tsx index 3865ddbefb..3027ad35cf 100644 --- a/packages/javascript/bh-shared-ui/src/components/LoginViaSSOForm/LoginViaSSOForm.tsx +++ b/packages/javascript/bh-shared-ui/src/components/LoginViaSSOForm/LoginViaSSOForm.tsx @@ -21,23 +21,23 @@ import { SSOProvider } from 'js-client-library'; interface LoginViaSSOFormProps { providers: SSOProvider[] | undefined; - onSubmit: (redirectURL: string) => void; + onSubmit: (providerSlug: string) => void; onCancel: () => void; } const LoginViaSSOForm: React.FC = ({ providers, onSubmit, onCancel }) => { /* Hooks */ - const [redirectURL, setRedirectURL] = React.useState(''); + const [selectedProviderSlug, setSelectedProviderSlug] = React.useState(''); /* Event Handlers */ const handleSubmit: React.FormEventHandler = (e) => { e.preventDefault(); - if (redirectURL === null) { + if (selectedProviderSlug === null) { return; } - onSubmit(redirectURL); + onSubmit(selectedProviderSlug); }; /* Implementation */ @@ -50,12 +50,12 @@ const LoginViaSSOForm: React.FC = ({ providers, onSubmit,