mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-11-27 23:27:31 +00:00
fix(login): keep the app sign-in screen within the eslint ratchets
Split the other ways in out of one component (complexity), finish the async handlers with void instead of handing promises to React, report a landscape that fails to load instead of logging it, let the handover wait on settled settings rather than catching them, and move the one-time code field's props into a module of their own so MfaCodeStep only exports its component.
This commit is contained in:
@@ -72,7 +72,6 @@ describe('appBoot', () => {
|
||||
|
||||
it('FE-NATIVE-APPBOOT-004: does not wait for settings that never come, nor stop on ones that fail', async () => {
|
||||
vi.useFakeTimers();
|
||||
vi.spyOn(console, 'warn').mockImplementation(() => {});
|
||||
const hanging = vi.fn();
|
||||
void handOverToApp(new Promise(() => {}), hanging);
|
||||
await vi.advanceTimersByTimeAsync(1600);
|
||||
|
||||
@@ -42,10 +42,8 @@ const wait = (ms: number): Promise<void> => new Promise((resolve) => window.setT
|
||||
* the app, which rises in once its first screen is there (endAppBoot).
|
||||
*/
|
||||
export function handOverToApp(settings: Promise<unknown>, go: () => void): Promise<void> {
|
||||
const ready = Promise.race([settings, wait(SETTINGS_WAIT_MS)]).then(
|
||||
() => undefined,
|
||||
(err: unknown) => console.warn('Settings not ready for the handover:', err)
|
||||
);
|
||||
// allSettled: settings that fail to load must not hold the app back either.
|
||||
const ready = Promise.race([Promise.allSettled([settings]), wait(SETTINGS_WAIT_MS)]);
|
||||
return Promise.all([ready, wait(LEAVE_MS)]).then(() => {
|
||||
document.documentElement.setAttribute(HANDOVER, '');
|
||||
go();
|
||||
|
||||
@@ -5,9 +5,10 @@ import { useNativeServer } from '../../components/Settings/useNativeServer';
|
||||
import PasswordChecklist from '../../components/shared/PasswordChecklist';
|
||||
import { useTranslation } from '../../i18n';
|
||||
import { lightStatusBarIcons } from '../../native/appBoot';
|
||||
import { reportNativeFailure } from '../../native/reportFailure';
|
||||
import './appLogin.css';
|
||||
import { heading as headingOf, oidcLoginHref, oidcOnlyHref, submitLabel, type LoginStep } from './loginModel';
|
||||
import { mfaCodeInputProps } from './MfaCodeStep';
|
||||
import { mfaCodeInputProps } from './mfaCodeInput';
|
||||
import { IDP_LOGIN_URL, type useLogin } from './useLogin';
|
||||
|
||||
type Login = ReturnType<typeof useLogin>;
|
||||
@@ -75,7 +76,8 @@ function AppLoginHero() {
|
||||
.then(({ startScenery, cameraForPlan }) => {
|
||||
if (!cancelled) startScenery(box, { frame: 320, camera: cameraForPlan(1), haze: false });
|
||||
})
|
||||
.catch((err: unknown) => console.warn('Landscape unavailable:', err));
|
||||
// Without the landscape the box keeps its plain sea colour.
|
||||
.catch(reportNativeFailure);
|
||||
return () => {
|
||||
cancelled = true;
|
||||
box.querySelector('canvas.scenery')?.remove();
|
||||
@@ -168,7 +170,7 @@ function Credentials({ login }: { login: Login }) {
|
||||
const onAccount = !(login.mode === 'login' && login.mfaStep) && !login.passwordChangeStep;
|
||||
return (
|
||||
<>
|
||||
<form className="al-form" onSubmit={login.handleSubmit}>
|
||||
<form className="al-form" onSubmit={(e) => void login.handleSubmit(e)}>
|
||||
<h1 className="al-title">{heading.title}</h1>
|
||||
{/* The plain sign-in needs no line under its heading; the other steps say what they ask for. */}
|
||||
{!heading.plain && <p className="al-sub">{heading.subtitle}</p>}
|
||||
@@ -184,7 +186,7 @@ function Credentials({ login }: { login: Login }) {
|
||||
<OtherWays login={login} />
|
||||
<ModeSwitch login={login} />
|
||||
{login.mode === 'login' && onAccount && (
|
||||
<button type="button" className="al-forgot" onClick={() => login.navigate('/forgot-password')}>
|
||||
<button type="button" className="al-forgot" onClick={() => void login.navigate('/forgot-password')}>
|
||||
{t('login.forgotPassword')}
|
||||
</button>
|
||||
)}
|
||||
@@ -340,40 +342,44 @@ function LoginOptions({ login }: { login: Login }) {
|
||||
);
|
||||
}
|
||||
|
||||
/** Which of the ways in besides the password this server offers on this step. */
|
||||
function otherWaysOf(login: Login): { sso: boolean; passkey: boolean; demo: boolean } {
|
||||
const config = login.appConfig;
|
||||
const onFirstStep = login.mode === 'login' && !login.mfaStep && !login.passwordChangeStep;
|
||||
return {
|
||||
sso: !!(config?.oidc_configured && config.oidc_login && !login.oidcOnly),
|
||||
passkey: !!(config?.passkey_login && config.passkey_configured && onFirstStep),
|
||||
demo: !!config?.demo_mode,
|
||||
};
|
||||
}
|
||||
|
||||
/** SSO, a passkey and the demo: the ways in besides the password. */
|
||||
function OtherWays({ login }: { login: Login }) {
|
||||
const { t } = useTranslation();
|
||||
const config = login.appConfig;
|
||||
const sso = !!(config?.oidc_configured && config?.oidc_login && !login.oidcOnly);
|
||||
const passkey = !!(
|
||||
config?.passkey_login &&
|
||||
config?.passkey_configured &&
|
||||
login.mode === 'login' &&
|
||||
!login.mfaStep &&
|
||||
!login.passwordChangeStep
|
||||
);
|
||||
if (!sso && !passkey && !config?.demo_mode) return null;
|
||||
const ways = otherWaysOf(login);
|
||||
if (!ways.sso && !ways.passkey && !ways.demo) return null;
|
||||
return (
|
||||
<div className="al-alt">
|
||||
{(sso || passkey) && <div className="al-or">{t('common.or')}</div>}
|
||||
{sso && (
|
||||
<a
|
||||
{(ways.sso || ways.passkey) && <div className="al-or">{t('common.or')}</div>}
|
||||
{ways.sso && <SsoButton login={login} />}
|
||||
{ways.passkey && (
|
||||
<button
|
||||
type="button"
|
||||
className="al-cta2"
|
||||
href={oidcLoginHref(login.inviteToken, login.mode, login.rememberMe)}
|
||||
onClick={login.handleSsoClick}
|
||||
onClick={() => void login.handlePasskeyLogin()}
|
||||
disabled={login.isLoading}
|
||||
>
|
||||
<Shield size={18} aria-hidden="true" />
|
||||
{t('login.oidcSignIn', { name: config?.oidc_display_name || 'SSO' })}
|
||||
</a>
|
||||
)}
|
||||
{passkey && (
|
||||
<button type="button" className="al-cta2" onClick={login.handlePasskeyLogin} disabled={login.isLoading}>
|
||||
<Fingerprint size={18} aria-hidden="true" />
|
||||
{t('login.passkey.signIn')}
|
||||
</button>
|
||||
)}
|
||||
{config?.demo_mode && (
|
||||
<button type="button" className="al-cta al-demo" onClick={login.handleDemoLogin} disabled={login.isLoading}>
|
||||
{ways.demo && (
|
||||
<button
|
||||
type="button"
|
||||
className="al-cta al-demo"
|
||||
onClick={() => void login.handleDemoLogin()}
|
||||
disabled={login.isLoading}
|
||||
>
|
||||
{t('login.demoHint')}
|
||||
</button>
|
||||
)}
|
||||
@@ -381,6 +387,20 @@ function OtherWays({ login }: { login: Login }) {
|
||||
);
|
||||
}
|
||||
|
||||
function SsoButton({ login }: { login: Login }) {
|
||||
const { t } = useTranslation();
|
||||
return (
|
||||
<a
|
||||
className="al-cta2"
|
||||
href={oidcLoginHref(login.inviteToken, login.mode, login.rememberMe)}
|
||||
onClick={login.handleSsoClick}
|
||||
>
|
||||
<Shield size={18} aria-hidden="true" />
|
||||
{t('login.oidcSignIn', { name: login.appConfig?.oidc_display_name || 'SSO' })}
|
||||
</a>
|
||||
);
|
||||
}
|
||||
|
||||
function ModeSwitch({ login }: { login: Login }) {
|
||||
const { t } = useTranslation();
|
||||
const config = login.appConfig;
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
import { KeyRound } from 'lucide-react';
|
||||
import React from 'react';
|
||||
import { useTranslation } from '../../i18n';
|
||||
import { mfaCodeInputProps } from './mfaCodeInput';
|
||||
|
||||
interface MfaCodeStepProps {
|
||||
/** The account being signed in, kept in the form for password managers. */
|
||||
@@ -11,35 +12,6 @@ interface MfaCodeStepProps {
|
||||
onBack: () => void;
|
||||
}
|
||||
|
||||
/**
|
||||
* The one-time code field, for this step and the app's sign-in screen alike.
|
||||
* Text, not numeric: backup codes carry letters. The hints are set here rather
|
||||
* than left to native/keyboardFields.ts, because autoFocus gives the field
|
||||
* focus before any touch could prepare it.
|
||||
*/
|
||||
export function mfaCodeInputProps(
|
||||
code: string,
|
||||
onCode: (code: string) => void,
|
||||
placeholder: string
|
||||
): React.InputHTMLAttributes<HTMLInputElement> {
|
||||
return {
|
||||
type: 'text',
|
||||
id: 'totp',
|
||||
name: 'totp',
|
||||
inputMode: 'text',
|
||||
autoComplete: 'one-time-code',
|
||||
autoCapitalize: 'characters',
|
||||
autoCorrect: 'off',
|
||||
spellCheck: false,
|
||||
enterKeyHint: 'go',
|
||||
value: code,
|
||||
onChange: (e: React.ChangeEvent<HTMLInputElement>) => onCode(e.target.value.toUpperCase().slice(0, 24)),
|
||||
placeholder,
|
||||
required: true,
|
||||
autoFocus: true,
|
||||
};
|
||||
}
|
||||
|
||||
/** The login's second step: the one-time or backup code, and the way back. */
|
||||
export default function MfaCodeStep({ email, code, onCode, inputStyle, onBack }: MfaCodeStepProps) {
|
||||
const { t } = useTranslation();
|
||||
|
||||
@@ -0,0 +1,30 @@
|
||||
import type React from 'react';
|
||||
|
||||
/**
|
||||
* The one-time code field, for this step and the app's sign-in screen alike.
|
||||
* Text, not numeric: backup codes carry letters. The hints are set here rather
|
||||
* than left to native/keyboardFields.ts, because autoFocus gives the field
|
||||
* focus before any touch could prepare it.
|
||||
*/
|
||||
export function mfaCodeInputProps(
|
||||
code: string,
|
||||
onCode: (code: string) => void,
|
||||
placeholder: string
|
||||
): React.InputHTMLAttributes<HTMLInputElement> {
|
||||
return {
|
||||
type: 'text',
|
||||
id: 'totp',
|
||||
name: 'totp',
|
||||
inputMode: 'text',
|
||||
autoComplete: 'one-time-code',
|
||||
autoCapitalize: 'characters',
|
||||
autoCorrect: 'off',
|
||||
spellCheck: false,
|
||||
enterKeyHint: 'go',
|
||||
value: code,
|
||||
onChange: (e: React.ChangeEvent<HTMLInputElement>) => onCode(e.target.value.toUpperCase().slice(0, 24)),
|
||||
placeholder,
|
||||
required: true,
|
||||
autoFocus: true,
|
||||
};
|
||||
}
|
||||
@@ -170,7 +170,9 @@ export function useLogin() {
|
||||
// The app has no takeoff: the sign-in screen steps back and the app rises in
|
||||
// as soon as the settings are there (native/appBoot.ts).
|
||||
toastHaptic('success')
|
||||
void handOverToApp(loadSettings(), () => navigate(redirectTarget))
|
||||
void handOverToApp(loadSettings(), () => {
|
||||
void navigate(redirectTarget)
|
||||
})
|
||||
return
|
||||
}
|
||||
loadSettings()
|
||||
|
||||
Reference in New Issue
Block a user