0
Fork 0
mirror of https://github.com/logto-io/logto.git synced 2025-01-06 20:40:08 -05:00

refactor(experience): rename SingleSignOnContext to UserInteractionContext (#6163)

This commit is contained in:
Xiao Yijun 2024-07-04 10:52:54 +08:00 committed by GitHub
parent 61781062c1
commit 8635f1b774
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
10 changed files with 62 additions and 52 deletions

View file

@ -6,7 +6,7 @@ import AppBoundary from './Providers/AppBoundary';
import LoadingLayerProvider from './Providers/LoadingLayerProvider'; import LoadingLayerProvider from './Providers/LoadingLayerProvider';
import PageContextProvider from './Providers/PageContextProvider'; import PageContextProvider from './Providers/PageContextProvider';
import SettingsProvider from './Providers/SettingsProvider'; import SettingsProvider from './Providers/SettingsProvider';
import SingleSignOnContextProvider from './Providers/SingleSignOnContextProvider'; import UserInteractionContextProvider from './Providers/UserInteractionContextProvider';
import Callback from './pages/Callback'; import Callback from './pages/Callback';
import Consent from './pages/Consent'; import Consent from './pages/Consent';
import Continue from './pages/Continue'; import Continue from './pages/Continue';
@ -45,7 +45,7 @@ const App = () => {
<BrowserRouter> <BrowserRouter>
<PageContextProvider> <PageContextProvider>
<SettingsProvider> <SettingsProvider>
<SingleSignOnContextProvider> <UserInteractionContextProvider>
<AppBoundary> <AppBoundary>
<Routes> <Routes>
<Route element={<LoadingLayerProvider />}> <Route element={<LoadingLayerProvider />}>
@ -125,7 +125,7 @@ const App = () => {
</Route> </Route>
</Routes> </Routes>
</AppBoundary> </AppBoundary>
</SingleSignOnContextProvider> </UserInteractionContextProvider>
</SettingsProvider> </SettingsProvider>
</PageContextProvider> </PageContextProvider>
</BrowserRouter> </BrowserRouter>

View file

@ -2,20 +2,20 @@ import { type SsoConnectorMetadata } from '@logto/schemas';
import { noop } from '@silverhand/essentials'; import { noop } from '@silverhand/essentials';
import { createContext } from 'react'; import { createContext } from 'react';
export type SingleSignOnContextType = { export type UserInteractionContextType = {
// All the enabled sso connectors // All the enabled sso connectors
availableSsoConnectorsMap: Map<string, SsoConnectorMetadata>; availableSsoConnectorsMap: Map<string, SsoConnectorMetadata>;
email?: string; ssoEmail?: string;
setEmail: React.Dispatch<React.SetStateAction<string | undefined>>; setSsoEmail: React.Dispatch<React.SetStateAction<string | undefined>>;
// The sso connectors that are enabled for the current domain // The sso connectors that are enabled for the current domain
ssoConnectors: SsoConnectorMetadata[]; ssoConnectors: SsoConnectorMetadata[];
setSsoConnectors: React.Dispatch<React.SetStateAction<SsoConnectorMetadata[]>>; setSsoConnectors: React.Dispatch<React.SetStateAction<SsoConnectorMetadata[]>>;
}; };
export default createContext<SingleSignOnContextType>({ export default createContext<UserInteractionContextType>({
email: undefined, ssoEmail: undefined,
availableSsoConnectorsMap: new Map(), availableSsoConnectorsMap: new Map(),
ssoConnectors: [], ssoConnectors: [],
setEmail: noop, setSsoEmail: noop,
setSsoConnectors: noop, setSsoConnectors: noop,
}); });

View file

@ -4,28 +4,37 @@ import { type ReactNode, useEffect, useMemo, useState } from 'react';
import useSessionStorage, { StorageKeys } from '@/hooks/use-session-storages'; import useSessionStorage, { StorageKeys } from '@/hooks/use-session-storages';
import { useSieMethods } from '@/hooks/use-sie'; import { useSieMethods } from '@/hooks/use-sie';
import SingleSignOnContext, { type SingleSignOnContextType } from './SingleSignOnContext'; import UserInteractionContext, { type UserInteractionContextType } from './UserInteractionContext';
type Props = { type Props = {
readonly children: ReactNode; readonly children: ReactNode;
}; };
const SingleSignOnContextProvider = ({ children }: Props) => { /**
* UserInteractionContextProvider
*
* This component manages user interaction data during the sign-in process,
* combining React's Context API with session storage to enable cross-page
* data persistence and access.
*
* The cached data provided by this provider primarily helps improve the sign-in experience for end users.
*/
const UserInteractionContextProvider = ({ children }: Props) => {
const { ssoConnectors } = useSieMethods(); const { ssoConnectors } = useSieMethods();
const { get, set, remove } = useSessionStorage(); const { get, set, remove } = useSessionStorage();
const [email, setEmail] = useState<string | undefined>(get(StorageKeys.SsoEmail)); const [ssoEmail, setSsoEmail] = useState<string | undefined>(get(StorageKeys.SsoEmail));
const [domainFilteredConnectors, setDomainFilteredConnectors] = useState<SsoConnectorMetadata[]>( const [domainFilteredConnectors, setDomainFilteredConnectors] = useState<SsoConnectorMetadata[]>(
get(StorageKeys.SsoConnectors) ?? [] get(StorageKeys.SsoConnectors) ?? []
); );
useEffect(() => { useEffect(() => {
if (!email) { if (!ssoEmail) {
remove(StorageKeys.SsoEmail); remove(StorageKeys.SsoEmail);
return; return;
} }
set(StorageKeys.SsoEmail, email); set(StorageKeys.SsoEmail, ssoEmail);
}, [email, remove, set]); }, [ssoEmail, remove, set]);
useEffect(() => { useEffect(() => {
if (domainFilteredConnectors.length === 0) { if (domainFilteredConnectors.length === 0) {
@ -41,22 +50,22 @@ const SingleSignOnContextProvider = ({ children }: Props) => {
[ssoConnectors] [ssoConnectors]
); );
const singleSignOnContext = useMemo<SingleSignOnContextType>( const userInteractionContext = useMemo<UserInteractionContextType>(
() => ({ () => ({
email, ssoEmail,
setEmail, setSsoEmail,
availableSsoConnectorsMap: ssoConnectorsMap, availableSsoConnectorsMap: ssoConnectorsMap,
ssoConnectors: domainFilteredConnectors, ssoConnectors: domainFilteredConnectors,
setSsoConnectors: setDomainFilteredConnectors, setSsoConnectors: setDomainFilteredConnectors,
}), }),
[domainFilteredConnectors, email, ssoConnectorsMap] [ssoEmail, ssoConnectorsMap, domainFilteredConnectors]
); );
return ( return (
<SingleSignOnContext.Provider value={singleSignOnContext}> <UserInteractionContext.Provider value={userInteractionContext}>
{children} {children}
</SingleSignOnContext.Provider> </UserInteractionContext.Provider>
); );
}; };
export default SingleSignOnContextProvider; export default UserInteractionContextProvider;

View file

@ -3,7 +3,7 @@ import { useCallback, useState, useContext } from 'react';
import { useTranslation } from 'react-i18next'; import { useTranslation } from 'react-i18next';
import { useNavigate } from 'react-router-dom'; import { useNavigate } from 'react-router-dom';
import SingleSignOnContext from '@/Providers/SingleSignOnContextProvider/SingleSignOnContext'; import UserInteractionContext from '@/Providers/UserInteractionContextProvider/UserInteractionContext';
import { getSingleSignOnConnectors } from '@/apis/single-sign-on'; import { getSingleSignOnConnectors } from '@/apis/single-sign-on';
import useApi from '@/hooks/use-api'; import useApi from '@/hooks/use-api';
import useErrorHandler from '@/hooks/use-error-handler'; import useErrorHandler from '@/hooks/use-error-handler';
@ -15,7 +15,8 @@ const useCheckSingleSignOn = () => {
const navigate = useNavigate(); const navigate = useNavigate();
const request = useApi(getSingleSignOnConnectors); const request = useApi(getSingleSignOnConnectors);
const [errorMessage, setErrorMessage] = useState<string | undefined>(); const [errorMessage, setErrorMessage] = useState<string | undefined>();
const { setEmail, setSsoConnectors, availableSsoConnectorsMap } = useContext(SingleSignOnContext); const { setSsoEmail, setSsoConnectors, availableSsoConnectorsMap } =
useContext(UserInteractionContext);
const singleSignOn = useSingleSignOn(); const singleSignOn = useSingleSignOn();
const handleError = useErrorHandler(); const handleError = useErrorHandler();
@ -26,9 +27,9 @@ const useCheckSingleSignOn = () => {
// Should clear the context and storage if the user trying to resubmit the form // Should clear the context and storage if the user trying to resubmit the form
const clearContext = useCallback(() => { const clearContext = useCallback(() => {
setEmail(undefined); setSsoEmail(undefined);
setSsoConnectors([]); setSsoConnectors([]);
}, [setEmail, setSsoConnectors]); }, [setSsoEmail, setSsoConnectors]);
/** /**
* Check if the email is registered with any SSO connectors * Check if the email is registered with any SSO connectors
@ -66,7 +67,7 @@ const useCheckSingleSignOn = () => {
} }
setSsoConnectors(connectors); setSsoConnectors(connectors);
setEmail(email); setSsoEmail(email);
if (!continueSignIn) { if (!continueSignIn) {
return true; return true;
@ -87,7 +88,7 @@ const useCheckSingleSignOn = () => {
handleError, handleError,
navigate, navigate,
request, request,
setEmail, setSsoEmail,
setSsoConnectors, setSsoConnectors,
singleSignOn, singleSignOn,
t, t,

View file

@ -7,8 +7,8 @@ import {
import { useEffect, useCallback, useContext } from 'react'; import { useEffect, useCallback, useContext } from 'react';
import { useNavigate } from 'react-router-dom'; import { useNavigate } from 'react-router-dom';
import SingleSignOnContext from '@/Providers/SingleSignOnContextProvider/SingleSignOnContext';
import SingleSignOnFormModeContext from '@/Providers/SingleSignOnFormModeContextProvider/SingleSignOnFormModeContext'; import SingleSignOnFormModeContext from '@/Providers/SingleSignOnFormModeContextProvider/SingleSignOnFormModeContext';
import UserInteractionContext from '@/Providers/UserInteractionContextProvider/UserInteractionContext';
import { getSingleSignOnConnectors } from '@/apis/single-sign-on'; import { getSingleSignOnConnectors } from '@/apis/single-sign-on';
import type { IdentifierInputValue } from '@/components/InputFields/SmartInputField'; import type { IdentifierInputValue } from '@/components/InputFields/SmartInputField';
import useApi from '@/hooks/use-api'; import useApi from '@/hooks/use-api';
@ -23,8 +23,8 @@ const useSingleSignOnWatch = (identifierInput?: IdentifierInputValue) => {
const { singleSignOnEnabled } = useSieMethods(); const { singleSignOnEnabled } = useSieMethods();
const { setEmail, setSsoConnectors, ssoConnectors, availableSsoConnectorsMap } = const { setSsoEmail, setSsoConnectors, ssoConnectors, availableSsoConnectorsMap } =
useContext(SingleSignOnContext); useContext(UserInteractionContext);
const { showSingleSignOnForm, setShowSingleSignOnForm } = useContext(SingleSignOnFormModeContext); const { showSingleSignOnForm, setShowSingleSignOnForm } = useContext(SingleSignOnFormModeContext);
@ -53,10 +53,10 @@ const useSingleSignOnWatch = (identifierInput?: IdentifierInputValue) => {
} }
setSsoConnectors(connectors); setSsoConnectors(connectors);
setEmail(email); setSsoEmail(email);
return true; return true;
}, },
[availableSsoConnectorsMap, request, setEmail, setSsoConnectors] [availableSsoConnectorsMap, request, setSsoEmail, setSsoConnectors]
); );
// Reset the ssoContext // Reset the ssoContext
@ -64,9 +64,9 @@ const useSingleSignOnWatch = (identifierInput?: IdentifierInputValue) => {
if (!showSingleSignOnForm) { if (!showSingleSignOnForm) {
setSsoConnectors([]); setSsoConnectors([]);
setEmail(undefined); setSsoEmail(undefined);
} }
}, [setEmail, setSsoConnectors, showSingleSignOnForm]); }, [setSsoEmail, setSsoConnectors, showSingleSignOnForm]);
const navigateToSingleSignOn = useCallback(async () => { const navigateToSingleSignOn = useCallback(async () => {
if (!showSingleSignOnForm) { if (!showSingleSignOnForm) {

View file

@ -3,8 +3,8 @@ import { assert } from '@silverhand/essentials';
import { fireEvent, act, waitFor } from '@testing-library/react'; import { fireEvent, act, waitFor } from '@testing-library/react';
import ConfirmModalProvider from '@/Providers/ConfirmModalProvider'; import ConfirmModalProvider from '@/Providers/ConfirmModalProvider';
import SingleSignOnContextProvider from '@/Providers/SingleSignOnContextProvider';
import SingleSignOnFormModeContextProvider from '@/Providers/SingleSignOnFormModeContextProvider'; import SingleSignOnFormModeContextProvider from '@/Providers/SingleSignOnFormModeContextProvider';
import UserInteractionContextProvider from '@/Providers/UserInteractionContextProvider';
import renderWithPageContext from '@/__mocks__/RenderWithPageContext'; import renderWithPageContext from '@/__mocks__/RenderWithPageContext';
import SettingsProvider from '@/__mocks__/RenderWithPageContext/SettingsProvider'; import SettingsProvider from '@/__mocks__/RenderWithPageContext/SettingsProvider';
import { mockSignInExperienceSettings, mockSsoConnectors } from '@/__mocks__/logto'; import { mockSignInExperienceSettings, mockSsoConnectors } from '@/__mocks__/logto';
@ -53,11 +53,11 @@ const renderForm = (
}} }}
> >
<ConfirmModalProvider> <ConfirmModalProvider>
<SingleSignOnContextProvider> <UserInteractionContextProvider>
<SingleSignOnFormModeContextProvider> <SingleSignOnFormModeContextProvider>
<IdentifierRegisterForm signUpMethods={signUpMethods} /> <IdentifierRegisterForm signUpMethods={signUpMethods} />
</SingleSignOnFormModeContextProvider> </SingleSignOnFormModeContextProvider>
</SingleSignOnContextProvider> </UserInteractionContextProvider>
</ConfirmModalProvider> </ConfirmModalProvider>
</SettingsProvider> </SettingsProvider>
); );

View file

@ -3,8 +3,8 @@ import { SignInIdentifier, experience } from '@logto/schemas';
import { assert } from '@silverhand/essentials'; import { assert } from '@silverhand/essentials';
import { fireEvent, act, waitFor } from '@testing-library/react'; import { fireEvent, act, waitFor } from '@testing-library/react';
import SingleSignOnContextProvider from '@/Providers/SingleSignOnContextProvider';
import SingleSignOnFormModeContextProvider from '@/Providers/SingleSignOnFormModeContextProvider'; import SingleSignOnFormModeContextProvider from '@/Providers/SingleSignOnFormModeContextProvider';
import UserInteractionContextProvider from '@/Providers/UserInteractionContextProvider';
import renderWithPageContext from '@/__mocks__/RenderWithPageContext'; import renderWithPageContext from '@/__mocks__/RenderWithPageContext';
import SettingsProvider from '@/__mocks__/RenderWithPageContext/SettingsProvider'; import SettingsProvider from '@/__mocks__/RenderWithPageContext/SettingsProvider';
import { import {
@ -52,11 +52,11 @@ const renderForm = (signInMethods: SignIn['methods'], ssoConnectors: SsoConnecto
ssoConnectors, ssoConnectors,
}} }}
> >
<SingleSignOnContextProvider> <UserInteractionContextProvider>
<SingleSignOnFormModeContextProvider> <SingleSignOnFormModeContextProvider>
<IdentifierSignInForm signInMethods={signInMethods} /> <IdentifierSignInForm signInMethods={signInMethods} />
</SingleSignOnFormModeContextProvider> </SingleSignOnFormModeContextProvider>
</SingleSignOnContextProvider> </UserInteractionContextProvider>
</SettingsProvider> </SettingsProvider>
); );

View file

@ -3,8 +3,8 @@ import { assert } from '@silverhand/essentials';
import { fireEvent, waitFor } from '@testing-library/react'; import { fireEvent, waitFor } from '@testing-library/react';
import { act } from 'react-dom/test-utils'; import { act } from 'react-dom/test-utils';
import SingleSignOnContextProvider from '@/Providers/SingleSignOnContextProvider';
import SingleSignOnFormModeContextProvider from '@/Providers/SingleSignOnFormModeContextProvider'; import SingleSignOnFormModeContextProvider from '@/Providers/SingleSignOnFormModeContextProvider';
import UserInteractionContextProvider from '@/Providers/UserInteractionContextProvider';
import renderWithPageContext from '@/__mocks__/RenderWithPageContext'; import renderWithPageContext from '@/__mocks__/RenderWithPageContext';
import SettingsProvider from '@/__mocks__/RenderWithPageContext/SettingsProvider'; import SettingsProvider from '@/__mocks__/RenderWithPageContext/SettingsProvider';
import { mockSignInExperienceSettings, mockSsoConnectors } from '@/__mocks__/logto'; import { mockSignInExperienceSettings, mockSsoConnectors } from '@/__mocks__/logto';
@ -50,11 +50,11 @@ describe('UsernamePasswordSignInForm', () => {
) => ) =>
renderWithPageContext( renderWithPageContext(
<SettingsProvider settings={{ ...mockSignInExperienceSettings, ...settings }}> <SettingsProvider settings={{ ...mockSignInExperienceSettings, ...settings }}>
<SingleSignOnContextProvider> <UserInteractionContextProvider>
<SingleSignOnFormModeContextProvider> <SingleSignOnFormModeContextProvider>
<PasswordSignInForm signInMethods={signInMethods} /> <PasswordSignInForm signInMethods={signInMethods} />
</SingleSignOnFormModeContextProvider> </SingleSignOnFormModeContextProvider>
</SingleSignOnContextProvider> </UserInteractionContextProvider>
</SettingsProvider> </SettingsProvider>
); );

View file

@ -3,7 +3,7 @@ import { useNavigate } from 'react-router-dom';
import SecondaryPageLayout from '@/Layout/SecondaryPageLayout'; import SecondaryPageLayout from '@/Layout/SecondaryPageLayout';
import PageContext from '@/Providers/PageContextProvider/PageContext'; import PageContext from '@/Providers/PageContextProvider/PageContext';
import SingleSignOnContext from '@/Providers/SingleSignOnContextProvider/SingleSignOnContext'; import UserInteractionContext from '@/Providers/UserInteractionContextProvider/UserInteractionContext';
import SocialLinkButton from '@/components/Button/SocialLinkButton'; import SocialLinkButton from '@/components/Button/SocialLinkButton';
import useNativeMessageListener from '@/hooks/use-native-message-listener'; import useNativeMessageListener from '@/hooks/use-native-message-listener';
import useSingleSignOn from '@/hooks/use-single-sign-on'; import useSingleSignOn from '@/hooks/use-single-sign-on';
@ -13,7 +13,7 @@ import * as styles from './index.module.scss';
const SingleSignOnConnectors = () => { const SingleSignOnConnectors = () => {
const { theme } = useContext(PageContext); const { theme } = useContext(PageContext);
const { email, ssoConnectors } = useContext(SingleSignOnContext); const { ssoEmail, ssoConnectors } = useContext(UserInteractionContext);
const navigate = useNavigate(); const navigate = useNavigate();
const onSubmit = useSingleSignOn(); const onSubmit = useSingleSignOn();
@ -22,18 +22,18 @@ const SingleSignOnConnectors = () => {
useEffect(() => { useEffect(() => {
// Return to the previous page if no email and no connectors are available in the context // Return to the previous page if no email and no connectors are available in the context
if (!email || ssoConnectors.length === 0) { if (!ssoEmail || ssoConnectors.length === 0) {
navigate('../email', { navigate('../email', {
replace: true, replace: true,
}); });
} }
}, [email, navigate, ssoConnectors.length]); }, [ssoEmail, navigate, ssoConnectors.length]);
return ( return (
<SecondaryPageLayout <SecondaryPageLayout
title="action.single_sign_on" title="action.single_sign_on"
description="description.single_sign_on_connectors_list" description="description.single_sign_on_connectors_list"
descriptionProps={{ email }} descriptionProps={{ email: ssoEmail }}
> >
<div className={styles.ssoLinkList}> <div className={styles.ssoLinkList}>
{ssoConnectors.map((connector) => { {ssoConnectors.map((connector) => {

View file

@ -3,7 +3,7 @@ import { useCallback, useContext, useEffect } from 'react';
import { Controller, useForm } from 'react-hook-form'; import { Controller, useForm } from 'react-hook-form';
import SecondaryPageLayout from '@/Layout/SecondaryPageLayout'; import SecondaryPageLayout from '@/Layout/SecondaryPageLayout';
import SingleSignOnContext from '@/Providers/SingleSignOnContextProvider/SingleSignOnContext'; import UserInteractionContext from '@/Providers/UserInteractionContextProvider/UserInteractionContext';
import LockIcon from '@/assets/icons/lock.svg'; import LockIcon from '@/assets/icons/lock.svg';
import Button from '@/components/Button'; import Button from '@/components/Button';
import ErrorMessage from '@/components/ErrorMessage'; import ErrorMessage from '@/components/ErrorMessage';
@ -21,7 +21,7 @@ type FormState = {
const SingleSignOnEmail = () => { const SingleSignOnEmail = () => {
const { errorMessage, clearErrorMessage, onSubmit } = useOnSubmit(); const { errorMessage, clearErrorMessage, onSubmit } = useOnSubmit();
const { email } = useContext(SingleSignOnContext); const { ssoEmail } = useContext(UserInteractionContext);
const { const {
handleSubmit, handleSubmit,
@ -73,7 +73,7 @@ const SingleSignOnEmail = () => {
className={styles.inputField} className={styles.inputField}
{...field} {...field}
isDanger={!!errors.identifier} isDanger={!!errors.identifier}
defaultValue={email} defaultValue={ssoEmail}
errorMessage={errors.identifier?.message} errorMessage={errors.identifier?.message}
enabledTypes={[SignInIdentifier.Email]} enabledTypes={[SignInIdentifier.Email]}
/> />