Skip to content

Commit

Permalink
BED-4978 feat: add instant redirect if only 1 sso provider is configu…
Browse files Browse the repository at this point in the history
…red (#932)
  • Loading branch information
mistahj67 authored Nov 6, 2024
1 parent 76c9d5d commit 36170aa
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 48 deletions.
11 changes: 7 additions & 4 deletions cmd/ui/src/views/Login.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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 */
Expand Down Expand Up @@ -113,6 +112,10 @@ const Login: React.FC = () => {
}

if (useSSO) {
if (listSSOProvidersQuery.data?.length === 1) {
handleSubmitLoginViaSSOForm(listSSOProvidersQuery.data[0].slug);
return;
}
return (
<LoginPage>
<LoginViaSSOForm
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
// SPDX-License-Identifier: Apache-2.0

import userEvent from '@testing-library/user-event';
import { render, screen } from 'src/test-utils';
import { render, screen } from '../../test-utils';

import LoginForm from './LoginForm';

Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ describe('LoginViaSSOForm', () => {
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 () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<LoginViaSSOFormProps> = ({ providers, onSubmit, onCancel }) => {
/* Hooks */
const [redirectURL, setRedirectURL] = React.useState('');
const [selectedProviderSlug, setSelectedProviderSlug] = React.useState('');

/* Event Handlers */
const handleSubmit: React.FormEventHandler<HTMLFormElement> = (e) => {
e.preventDefault();

if (redirectURL === null) {
if (selectedProviderSlug === null) {
return;
}

onSubmit(redirectURL);
onSubmit(selectedProviderSlug);
};

/* Implementation */
Expand All @@ -50,20 +50,20 @@ const LoginViaSSOForm: React.FC<LoginViaSSOFormProps> = ({ providers, onSubmit,
<Select
labelId='selected-saml-provider-label'
id='selected-saml-provider'
value={redirectURL}
value={selectedProviderSlug}
label='Choose your SSO Provider'
onChange={(e) => setRedirectURL(e.target.value as string)}
onChange={(e) => setSelectedProviderSlug(e.target.value as string)}
fullWidth>
{providers?.map((provider) => (
<MenuItem key={provider.id} value={`/api/v2/sso/${provider.slug}/login`}>
<MenuItem key={provider.id} value={provider.slug}>
{provider.name}
</MenuItem>
))}
</Select>
</FormControl>
</Grid>
<Grid item xs={8}>
<Button size='large' type='submit' className='w-full' disabled={redirectURL === ''}>
<Button size='large' type='submit' className='w-full' disabled={selectedProviderSlug === ''}>
CONTINUE
</Button>
<Box mt={2}>
Expand Down
71 changes: 37 additions & 34 deletions packages/javascript/bh-shared-ui/src/components/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,17 @@
//
// SPDX-License-Identifier: Apache-2.0

export { default as ApiVersion } from './ApiVersion';

export * from './AppNotifications';
export { default as AppNotifications } from './AppNotifications';

export * from './AssetGroupEdit';
export { default as AssetGroupEdit } from './AssetGroupEdit';

export * from './AssetGroupMemberList';
export { default as AssetGroupMemberList } from './AssetGroupMemberList';

export * from './CardWithSwitch';
export { default as CardWithSwitch } from './CardWithSwitch';

Expand Down Expand Up @@ -47,12 +55,26 @@ export { default as CreateSAMLProviderDialog } from './CreateSAMLProviderDialog'
export * from './CreateSAMLProviderForm';
export { default as CreateSAMLProviderForm } from './CreateSAMLProviderForm';

export * from './CreateUserForm';
export { default as CreateUserForm } from './CreateUserForm';

export * from './CreateUserDialog';
export { default as CreateUserDialog } from './CreateUserDialog';

export * from './DataTable';
export { default as DataTable } from './DataTable';

export * from './Disable2FADialog';
export { default as Disable2FADialog } from './Disable2FADialog';

export * from './DocumentationLinks';
export { default as DocumentationLinks } from './DocumentationLinks';

export * from './DropdownSelector';
export { default as DropdownSelector } from './DropdownSelector';

export { default as EdgeInfoComponents } from './HelpTexts/index';

export * from './Enable2FADialog';
export { default as Enable2FADialog } from './Enable2FADialog';

Expand All @@ -62,6 +84,9 @@ export { default as EnterpriseIcon } from './EnterpriseIcon';
export * from './FeatureFlag';
export { default as FeatureFlag } from './FeatureFlag';

export * from './FileIngest';
export { default as FileIngest } from './FileIngest';

export * from './FileUploadDialog';
export { default as FileUploadDialog } from './FileUploadDialog';

Expand All @@ -80,7 +105,8 @@ export { default as GraphMenu } from './GraphMenu';
export * from './GraphProgress';
export { default as GraphProgress } from './GraphProgress';

export { default as EdgeInfoComponents } from './HelpTexts/index';
export * from './GroupManagementContent';
export { default as GroupManagementContent } from './GroupManagementContent';

export * from './HighlightedText';
export { default as HighlightedText } from './HighlightedText';
Expand All @@ -97,6 +123,12 @@ export { default as LabelWithCopy } from './LabelWithCopy';
export * from './LoadingOverlay';
export { default as LoadingOverlay } from './LoadingOverlay';

export * from './LoginViaSSOForm';
export { default as LoginViaSSOForm } from './LoginViaSSOForm';

export * from './LoginForm';
export { default as LoginForm } from './LoginForm';

export * from './MenuItem';
export { default as MenuItem } from './MenuItem';

Expand Down Expand Up @@ -138,43 +170,14 @@ export { default as SetupKeyDialog } from './SetupKeyDialog';
export * from './TextWithFallback';
export { default as TextWithFallback } from './TextWithFallback';

export * from './UserTokenManagementDialog';
export { default as UserTokenManagementDialog } from './UserTokenManagementDialog';

export * from './AssetGroupMemberList';
export { default as AssetGroupMemberList } from './AssetGroupMemberList';

export * from './DropdownSelector';
export { default as DropdownSelector } from './DropdownSelector';

export * from './AssetGroupEdit';
export { default as AssetGroupEdit } from './AssetGroupEdit';

export * from './GroupManagementContent';
export { default as GroupManagementContent } from './GroupManagementContent';

export * from './FileIngest';
export { default as FileIngest } from './FileIngest';

export * from './WebGLDisabledAlert';
export { default as WebGLDisabledAlert } from './WebGLDisabledAlert';

export * from './DocumentationLinks';
export { default as DocumentationLinks } from './DocumentationLinks';

export * from './UpdateUserDialog';
export { default as UpdateUserDialog } from './UpdateUserDialog';

export * from './UpdateUserForm';
export { default as UpdateUserForm } from './UpdateUserForm';

export * from './LoginViaSSOForm';
export { default as LoginViaSSOForm } from './LoginViaSSOForm';

export * from './CreateUserForm';
export { default as CreateUserForm } from './CreateUserForm';

export * from './CreateUserDialog';
export { default as CreateUserDialog } from './CreateUserDialog';
export * from './UserTokenManagementDialog';
export { default as UserTokenManagementDialog } from './UserTokenManagementDialog';

export { default as ApiVersion } from './ApiVersion';
export * from './WebGLDisabledAlert';
export { default as WebGLDisabledAlert } from './WebGLDisabledAlert';

0 comments on commit 36170aa

Please sign in to comment.