mirror of
https://github.com/logto-io/logto.git
synced 2024-12-16 20:26:19 -05:00
parent
9fd49c6caa
commit
61d1894fa9
4 changed files with 20 additions and 7 deletions
|
@ -115,7 +115,7 @@ const App = () => {
|
||||||
|
|
||||||
{/* Single sign on */}
|
{/* Single sign on */}
|
||||||
{isDevelopmentFeaturesEnabled && (
|
{isDevelopmentFeaturesEnabled && (
|
||||||
<Route path={singleSignOnPath}>
|
<Route path={singleSignOnPath} element={<LoadingLayerProvider />}>
|
||||||
<Route path="email" element={<SingleSignOnEmail />} />
|
<Route path="email" element={<SingleSignOnEmail />} />
|
||||||
<Route path="connectors" element={<SingleSignOnConnectors />} />
|
<Route path="connectors" element={<SingleSignOnConnectors />} />
|
||||||
</Route>
|
</Route>
|
||||||
|
|
|
@ -36,10 +36,11 @@ const useCheckSingleSignOn = () => {
|
||||||
/**
|
/**
|
||||||
* Check if the email is registered with any SSO connectors
|
* Check if the email is registered with any SSO connectors
|
||||||
* @param {string} email
|
* @param {string} email
|
||||||
|
* @param {boolean} continueSignIn - whether to continue the single sign-on flow if the email is registered with any SSO connectors
|
||||||
* @returns {Promise<boolean>} - true if the email is registered with any SSO connectors
|
* @returns {Promise<boolean>} - true if the email is registered with any SSO connectors
|
||||||
*/
|
*/
|
||||||
const onSubmit = useCallback(
|
const onSubmit = useCallback(
|
||||||
async (email: string) => {
|
async (email: string, continueSignIn = false) => {
|
||||||
clearContext();
|
clearContext();
|
||||||
|
|
||||||
const [error, result] = await request(email);
|
const [error, result] = await request(email);
|
||||||
|
@ -62,6 +63,10 @@ const useCheckSingleSignOn = () => {
|
||||||
setSsoConnectors(connectors);
|
setSsoConnectors(connectors);
|
||||||
setEmail(email);
|
setEmail(email);
|
||||||
|
|
||||||
|
if (!continueSignIn) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
// If there is only one connector, we can directly invoke the SSO flow
|
// If there is only one connector, we can directly invoke the SSO flow
|
||||||
if (connectors.length === 1 && connectors[0]?.id) {
|
if (connectors.length === 1 && connectors[0]?.id) {
|
||||||
await singleSignOn(connectors[0].id);
|
await singleSignOn(connectors[0].id);
|
||||||
|
|
|
@ -3,6 +3,7 @@ import { useState, useMemo, useCallback } from 'react';
|
||||||
import type { PasswordSignInPayload } from '@/apis/interaction';
|
import type { PasswordSignInPayload } from '@/apis/interaction';
|
||||||
import { signInWithPasswordIdentifier } from '@/apis/interaction';
|
import { signInWithPasswordIdentifier } from '@/apis/interaction';
|
||||||
import useApi from '@/hooks/use-api';
|
import useApi from '@/hooks/use-api';
|
||||||
|
import useCheckSingleSignOn from '@/hooks/use-check-single-sign-on';
|
||||||
import type { ErrorHandlers } from '@/hooks/use-error-handler';
|
import type { ErrorHandlers } from '@/hooks/use-error-handler';
|
||||||
import useErrorHandler from '@/hooks/use-error-handler';
|
import useErrorHandler from '@/hooks/use-error-handler';
|
||||||
|
|
||||||
|
@ -10,6 +11,7 @@ import usePreSignInErrorHandler from './use-pre-sign-in-error-handler';
|
||||||
|
|
||||||
const usePasswordSignIn = () => {
|
const usePasswordSignIn = () => {
|
||||||
const [errorMessage, setErrorMessage] = useState<string>();
|
const [errorMessage, setErrorMessage] = useState<string>();
|
||||||
|
const { onSubmit: checkSingleSignOn } = useCheckSingleSignOn();
|
||||||
|
|
||||||
const clearErrorMessage = useCallback(() => {
|
const clearErrorMessage = useCallback(() => {
|
||||||
setErrorMessage('');
|
setErrorMessage('');
|
||||||
|
@ -24,9 +26,6 @@ const usePasswordSignIn = () => {
|
||||||
'session.invalid_credentials': (error) => {
|
'session.invalid_credentials': (error) => {
|
||||||
setErrorMessage(error.message);
|
setErrorMessage(error.message);
|
||||||
},
|
},
|
||||||
'session.sso_enabled': (_error) => {
|
|
||||||
// Hide the toast and do nothing
|
|
||||||
},
|
|
||||||
...preSignInErrorHandler,
|
...preSignInErrorHandler,
|
||||||
}),
|
}),
|
||||||
[preSignInErrorHandler]
|
[preSignInErrorHandler]
|
||||||
|
@ -34,6 +33,15 @@ const usePasswordSignIn = () => {
|
||||||
|
|
||||||
const onSubmit = useCallback(
|
const onSubmit = useCallback(
|
||||||
async (payload: PasswordSignInPayload) => {
|
async (payload: PasswordSignInPayload) => {
|
||||||
|
// Check if the email is registered with any SSO connectors. If the email is registered with any SSO connectors, we should not proceed to the next step
|
||||||
|
if (payload.email) {
|
||||||
|
const result = await checkSingleSignOn(payload.email);
|
||||||
|
|
||||||
|
if (result) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
const [error, result] = await asyncSignIn(payload);
|
const [error, result] = await asyncSignIn(payload);
|
||||||
|
|
||||||
if (error) {
|
if (error) {
|
||||||
|
@ -46,7 +54,7 @@ const usePasswordSignIn = () => {
|
||||||
window.location.replace(result.redirectTo);
|
window.location.replace(result.redirectTo);
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
[asyncSignIn, errorHandlers, handleError]
|
[asyncSignIn, checkSingleSignOn, errorHandlers, handleError]
|
||||||
);
|
);
|
||||||
|
|
||||||
return {
|
return {
|
||||||
|
|
|
@ -40,7 +40,7 @@ const SingleSignOnEmail = () => {
|
||||||
const onSubmitHandler = useCallback(
|
const onSubmitHandler = useCallback(
|
||||||
async (event?: React.FormEvent<HTMLFormElement>) => {
|
async (event?: React.FormEvent<HTMLFormElement>) => {
|
||||||
clearErrorMessage();
|
clearErrorMessage();
|
||||||
await handleSubmit(async ({ identifier: { value } }) => onSubmit(value))(event);
|
await handleSubmit(async ({ identifier: { value } }) => onSubmit(value, true))(event);
|
||||||
},
|
},
|
||||||
[clearErrorMessage, handleSubmit, onSubmit]
|
[clearErrorMessage, handleSubmit, onSubmit]
|
||||||
);
|
);
|
||||||
|
|
Loading…
Reference in a new issue