mirror of
https://github.com/SableClient/Sable.git
synced 2026-12-02 23:02:33 -08:00
fix: adjust login auth page flow
This commit is contained in:
1 parent
787d1e1aaf
commit
f05a11db42
3 files changed
+39
-27
No files matched your search
@@ -5,8 +5,13 @@ import { createClient } from '$types/matrix-sdk';
|
||||
import { AsyncStatus, useAsyncCallback } from '$hooks/useAsyncCallback';
|
||||
import { useAutoDiscoveryInfo } from '$hooks/useAutoDiscoveryInfo';
|
||||
import { promiseFulfilledResult, promiseRejectedResult } from '$utils/common';
|
||||
import type { AuthFlows, RegisterFlowsResponse } from '$hooks/useAuthFlows';
|
||||
import { RegisterFlowStatus, parseRegisterErrResp } from '$hooks/useAuthFlows';
|
||||
import {
|
||||
isUsableOAuthMetadata,
|
||||
type AuthFlows,
|
||||
RegisterFlowStatus,
|
||||
parseRegisterErrResp,
|
||||
type RegisterFlowsResponse,
|
||||
} from '$hooks/useAuthFlows';
|
||||
import { fetch } from '$utils/fetch';
|
||||
|
||||
type AuthFlowsLoaderProps = {
|
||||
@@ -29,7 +34,10 @@ export function AuthFlowsLoader({ fallback, error, children }: AuthFlowsLoaderPr
|
||||
]);
|
||||
const loginFlows = promiseFulfilledResult(result[0]);
|
||||
const registerResp = promiseRejectedResult(result[1]) as MatrixError | undefined;
|
||||
const authMetadata = promiseFulfilledResult(result[2]);
|
||||
const discoveredAuthMetadata = promiseFulfilledResult(result[2]);
|
||||
const authMetadata = isUsableOAuthMetadata(discoveredAuthMetadata)
|
||||
? discoveredAuthMetadata
|
||||
: undefined;
|
||||
let registerFlows: RegisterFlowsResponse = {
|
||||
status: RegisterFlowStatus.InvalidRequest,
|
||||
};
|
||||
|
||||
@@ -51,6 +51,15 @@ export type AuthFlows = {
|
||||
authMetadata?: ValidatedAuthMetadata;
|
||||
};
|
||||
|
||||
/** True only when the homeserver advertises the OAuth authorization-code flow Sable uses. */
|
||||
export const isUsableOAuthMetadata = (metadata: ValidatedAuthMetadata | undefined): boolean =>
|
||||
metadata !== undefined &&
|
||||
metadata.grant_types_supported.includes('authorization_code') &&
|
||||
metadata.grant_types_supported.includes('refresh_token') &&
|
||||
metadata.response_types_supported.includes('code') &&
|
||||
metadata.response_modes_supported.includes('fragment') &&
|
||||
metadata.code_challenge_methods_supported.includes('S256');
|
||||
|
||||
const AuthFlowsContext = createContext<AuthFlows | null>(null);
|
||||
|
||||
export const AuthFlowsProvider = AuthFlowsContext.Provider;
|
||||
|
||||
@@ -2,10 +2,10 @@ import { useMemo, useState } from 'react';
|
||||
import { Box, Switch, Text, Tooltip, TooltipProvider, color, config } from 'folds';
|
||||
import { Link, useSearchParams } from 'react-router-dom';
|
||||
import { SSOAction } from '$types/matrix-sdk';
|
||||
import { RegisterFlowStatus, useAuthFlows } from '$hooks/useAuthFlows';
|
||||
import { isUsableOAuthMetadata, RegisterFlowStatus, useAuthFlows } from '$hooks/useAuthFlows';
|
||||
import { useAuthServer } from '$hooks/useAuthServer';
|
||||
import { useAutoDiscoveryInfo } from '$hooks/useAutoDiscoveryInfo';
|
||||
import { isOauthAwarePreferred, useParsedLoginFlows } from '$hooks/useParsedLoginFlows';
|
||||
import { useParsedLoginFlows } from '$hooks/useParsedLoginFlows';
|
||||
import { getLoginPath, getRegisterPath, withSearchParam } from '$pages/pathUtils';
|
||||
import { usePathWithOrigin } from '$hooks/usePathWithOrigin';
|
||||
import type { LoginPathSearchParams } from '$pages/paths';
|
||||
@@ -131,11 +131,10 @@ export function Login() {
|
||||
? (oidcErrorDescription ?? `Sign-in was not completed (${oidcError}).`)
|
||||
: undefined;
|
||||
|
||||
const showOidc = authMetadata !== undefined;
|
||||
const hidePassword =
|
||||
hideUsernamePasswordFields || (showOidc && isOauthAwarePreferred(parsedFlows.sso));
|
||||
const showOidc = isUsableOAuthMetadata(authMetadata);
|
||||
const hidePassword = hideUsernamePasswordFields;
|
||||
const showPassword = !hidePassword && parsedFlows.password !== undefined;
|
||||
const showLegacySso = !showOidc && parsedFlows.sso !== undefined;
|
||||
const showLegacySso = parsedFlows.sso !== undefined;
|
||||
|
||||
if (showOidc && oidcCode && oidcState) {
|
||||
return <OidcCallback code={oidcCode} state={oidcState} slidingSyncOptIn={useSlidingSync} />;
|
||||
@@ -146,26 +145,10 @@ export function Login() {
|
||||
<Text size="H2" priority="400">
|
||||
Login
|
||||
</Text>
|
||||
{!showPassword && (
|
||||
<SlidingSyncLoginOption value={useSlidingSync} onChange={handleSlidingSyncChange} />
|
||||
)}
|
||||
<SlidingSyncLoginOption value={useSlidingSync} onChange={handleSlidingSyncChange} />
|
||||
{parsedFlows.token && loginSearchParams.loginToken && (
|
||||
<TokenLogin token={loginSearchParams.loginToken} slidingSyncOptIn={useSlidingSync} />
|
||||
)}
|
||||
{showPassword && (
|
||||
<>
|
||||
<PasswordLoginForm
|
||||
defaultUsername={loginSearchParams.username}
|
||||
defaultEmail={loginSearchParams.email}
|
||||
slidingSyncOptIn={useSlidingSync}
|
||||
slidingSyncOption={
|
||||
<SlidingSyncLoginOption value={useSlidingSync} onChange={handleSlidingSyncChange} />
|
||||
}
|
||||
/>
|
||||
<span data-spacing-node />
|
||||
{(showLegacySso || showOidc) && <OrDivider />}
|
||||
</>
|
||||
)}
|
||||
{showOidc && (
|
||||
<>
|
||||
<OidcLoginButton
|
||||
@@ -181,11 +164,23 @@ export function Login() {
|
||||
)}
|
||||
{showLegacySso && (
|
||||
<>
|
||||
{showOidc && <OrDivider />}
|
||||
<SSOLogin
|
||||
providers={parsedFlows.sso!.identity_providers}
|
||||
redirectUrl={ssoRedirectUrl}
|
||||
action={SSOAction.LOGIN}
|
||||
saveScreenSpace={showPassword}
|
||||
saveScreenSpace={showPassword || showOidc}
|
||||
/>
|
||||
<span data-spacing-node />
|
||||
</>
|
||||
)}
|
||||
{showPassword && (
|
||||
<>
|
||||
{(showLegacySso || (showOidc && !showLegacySso)) && <OrDivider />}
|
||||
<PasswordLoginForm
|
||||
defaultUsername={loginSearchParams.username}
|
||||
defaultEmail={loginSearchParams.email}
|
||||
slidingSyncOptIn={useSlidingSync}
|
||||
/>
|
||||
<span data-spacing-node />
|
||||
</>
|
||||
|
||||
Reference in new issue
Block a user