auth: register failures in plain words; /register goes to the account service when the switch is on
Some checks failed
Build pull request / Build pull request (pull_request) Has been cancelled
Some checks failed
Build pull request / Build pull request (pull_request) Has been cancelled
The previous commit carried only the fallback split. This one carries the rest of Libby's two blockers and Heimdall's decision: - config.json keeps only the registration block over dev's; clientDefaultServer is dev's again (the gamecult.org default is #86). - registerErrorTrouble maps a register error code to registerTroubleMessage (new troubles: password-weak, password-short, unknown); PasswordRegisterForm shows those, never the server's text. - RegisterRoute: switch on, /register offers the account service link and redirects there; switch off, the in-client form. - Tests: registerErrors.test.ts (code to words, no Matrix code leaks), RegisterRoute.test.tsx (on, off, unusable url). Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
3f86551ba4
commit
fbec0322ef
9 changed files with 162 additions and 29 deletions
|
|
@ -24,5 +24,10 @@
|
|||
|
||||
"dings": {
|
||||
"enabled": false
|
||||
},
|
||||
|
||||
"registration": {
|
||||
"enabled": false,
|
||||
"url": ""
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -50,7 +50,7 @@ export function useClientConfig(): ClientConfig {
|
|||
}
|
||||
|
||||
export const clientDefaultServer = (clientConfig: ClientConfig): string =>
|
||||
clientConfig.homeserverList?.[clientConfig.defaultHomeserver ?? 0] ?? 'gamecult.org';
|
||||
clientConfig.homeserverList?.[clientConfig.defaultHomeserver ?? 0] ?? 'matrix.org';
|
||||
|
||||
export const clientAllowedServer = (clientConfig: ClientConfig, server: string): boolean => {
|
||||
const { homeserverList, allowCustomHomeservers } = clientConfig;
|
||||
|
|
|
|||
|
|
@ -57,7 +57,14 @@ export const serverTroubleMessage = (
|
|||
export const noSignInMethodMessage = (server: string): string =>
|
||||
`${server} does not offer a way to sign in that this app can use. Pick another server from the Homeserver list, or tell whoever runs ${server}.`;
|
||||
|
||||
export type RegisterTrouble = 'closed' | 'rate-limited' | 'invalid' | 'unsupported';
|
||||
export type RegisterTrouble =
|
||||
| 'closed'
|
||||
| 'rate-limited'
|
||||
| 'invalid'
|
||||
| 'unsupported'
|
||||
| 'password-weak'
|
||||
| 'password-short'
|
||||
| 'unknown';
|
||||
|
||||
export const registerTroubleMessage = (trouble: RegisterTrouble, server: string): string => {
|
||||
switch (trouble) {
|
||||
|
|
@ -67,6 +74,12 @@ export const registerTroubleMessage = (trouble: RegisterTrouble, server: string)
|
|||
return 'Too many tries in a short time. Wait a minute, then try again.';
|
||||
case 'invalid':
|
||||
return `${server} did not accept the request to make an account. Reload the page and try again; if it keeps failing, tell whoever invited you.`;
|
||||
case 'password-weak':
|
||||
return 'That password is too easy to guess. Pick a longer one, with more than one kind of character.';
|
||||
case 'password-short':
|
||||
return 'That password is too short. Pick a longer one.';
|
||||
case 'unknown':
|
||||
return `Something went wrong making the account on ${server}. Try again in a minute; if it keeps failing, tell whoever invited you.`;
|
||||
case 'unsupported':
|
||||
default:
|
||||
return `${server} asks for something this app cannot do when making an account. Tell whoever invited you.`;
|
||||
|
|
|
|||
|
|
@ -6,7 +6,7 @@ import { LOGIN_PATH, REGISTER_PATH, RESET_PASSWORD_PATH, SIGNED_UP_PATH } from '
|
|||
import { getHomePath } from '../pathUtils';
|
||||
import { AuthLayout } from './AuthLayout';
|
||||
import { Login } from './login';
|
||||
import { Register } from './register';
|
||||
import { RegisterRoute } from './register/RegisterRoute';
|
||||
import { ResetPassword } from './reset-password';
|
||||
import { SignedUp } from './SignedUp';
|
||||
|
||||
|
|
@ -24,7 +24,7 @@ export const authRoutes = () => (
|
|||
}
|
||||
>
|
||||
<Route path={LOGIN_PATH} element={<Login />} />
|
||||
<Route path={REGISTER_PATH} element={<Register />} />
|
||||
<Route path={REGISTER_PATH} element={<RegisterRoute />} />
|
||||
<Route path={RESET_PASSWORD_PATH} element={<ResetPassword />} />
|
||||
</Route>
|
||||
<Route
|
||||
|
|
|
|||
|
|
@ -30,7 +30,15 @@ import {
|
|||
import { useUIACompleted, useUIAFlow, useUIAParams } from '../../../hooks/useUIAFlows';
|
||||
import { AsyncState, AsyncStatus, useAsyncCallback } from '../../../hooks/useAsyncCallback';
|
||||
import { useAutoDiscoveryInfo } from '../../../hooks/useAutoDiscoveryInfo';
|
||||
import { RegisterError, RegisterResult, register, useRegisterComplete } from './registerUtil';
|
||||
import {
|
||||
RegisterError,
|
||||
RegisterResult,
|
||||
register,
|
||||
registerErrorTrouble,
|
||||
useRegisterComplete,
|
||||
} from './registerUtil';
|
||||
import { registerTroubleMessage } from '../authMessages';
|
||||
import { useAuthServer } from '../../../hooks/useAuthServer';
|
||||
import { FieldError } from '../FiledError';
|
||||
import { TrainingNotice } from '../TrainingNotice';
|
||||
import {
|
||||
|
|
@ -185,6 +193,7 @@ export function PasswordRegisterForm({
|
|||
defaultEmail,
|
||||
defaultRegisterToken,
|
||||
}: PasswordRegisterFormProps) {
|
||||
const server = useAuthServer();
|
||||
const serverDiscovery = useAutoDiscoveryInfo();
|
||||
const baseUrl = serverDiscovery['m.homeserver'].base_url;
|
||||
const mx = useMemo(() => createClient({ baseUrl }), [baseUrl]);
|
||||
|
|
@ -207,6 +216,9 @@ export function PasswordRegisterForm({
|
|||
const registerError =
|
||||
registerState.status === AsyncStatus.Error ? registerState.error : undefined;
|
||||
|
||||
// The password troubles show under the password field; the rest is the form's.
|
||||
const registerTrouble = registerError && registerErrorTrouble(registerError.errcode);
|
||||
const formTrouble = registerTrouble?.startsWith('password-') ? undefined : registerTrouble;
|
||||
useRegisterComplete(customRegisterResp);
|
||||
|
||||
const handleSubmit: ChangeEventHandler<HTMLFormElement> = (evt) => {
|
||||
|
|
@ -298,20 +310,10 @@ export function PasswordRegisterForm({
|
|||
required
|
||||
/>
|
||||
{registerError?.errcode === RegisterError.PasswordWeak && (
|
||||
<FieldError
|
||||
message={
|
||||
registerError.data.error ??
|
||||
'Weak Password. Password rejected by server please choosing more strong Password.'
|
||||
}
|
||||
/>
|
||||
<FieldError message={registerTroubleMessage('password-weak', server)} />
|
||||
)}
|
||||
{registerError?.errcode === RegisterError.PasswordShort && (
|
||||
<FieldError
|
||||
message={
|
||||
registerError.data.error ??
|
||||
'Short Password. Password rejected by server please choosing more long Password.'
|
||||
}
|
||||
/>
|
||||
<FieldError message={registerTroubleMessage('password-short', server)} />
|
||||
)}
|
||||
</Box>
|
||||
<Box direction="Column" gap="100">
|
||||
|
|
@ -378,18 +380,7 @@ export function PasswordRegisterForm({
|
|||
</Text>
|
||||
</Box>
|
||||
)}
|
||||
{registerError?.errcode === RegisterError.RateLimited && (
|
||||
<FieldError message="Failed to register. Your register request has been rate-limited by server, Please try after some time." />
|
||||
)}
|
||||
{registerError?.errcode === RegisterError.Forbidden && (
|
||||
<FieldError message="Failed to register. The homeserver does not permit registration." />
|
||||
)}
|
||||
{registerError?.errcode === RegisterError.InvalidRequest && (
|
||||
<FieldError message="Failed to register. Invalid request." />
|
||||
)}
|
||||
{registerError?.errcode === RegisterError.Unknown && (
|
||||
<FieldError message={registerError.data.error ?? 'Failed to register. Unknown Reason.'} />
|
||||
)}
|
||||
{formTrouble && <FieldError message={registerTroubleMessage(formTrouble, server)} />}
|
||||
<span data-spacing-node />
|
||||
<TrainingNotice checked={trainingAck} onToggle={() => setTrainingAck((ack) => !ack)} />
|
||||
<Button variant="Primary" size="500" type="submit" disabled={!trainingAck}>
|
||||
|
|
|
|||
37
src/app/pages/auth/register/RegisterRoute.test.tsx
Normal file
37
src/app/pages/auth/register/RegisterRoute.test.tsx
Normal file
|
|
@ -0,0 +1,37 @@
|
|||
import React from 'react';
|
||||
import { describe, expect, it, vi } from 'vitest';
|
||||
import { renderToString } from 'react-dom/server';
|
||||
import { ClientConfigProvider } from '../../../hooks/useClientConfig';
|
||||
import type { ClientConfig } from '../../../hooks/useClientConfig';
|
||||
import { RegisterRoute } from './RegisterRoute';
|
||||
|
||||
// The in-client form needs a homeserver; the route test only needs to know whether it was asked for.
|
||||
vi.mock('./Register', () => ({ Register: () => <div>in-client register form</div> }));
|
||||
|
||||
const render = (config: ClientConfig) =>
|
||||
renderToString(
|
||||
<ClientConfigProvider value={config}>
|
||||
<RegisterRoute />
|
||||
</ClientConfigProvider>
|
||||
);
|
||||
|
||||
describe('the /register route', () => {
|
||||
it('with the switch on offers only the account service link, not the in-client form', () => {
|
||||
const html = render({ registration: { enabled: true, url: 'https://accounts.example/join' } });
|
||||
expect(html).toContain('href="https://accounts.example/join"');
|
||||
expect(html).not.toContain('in-client register form');
|
||||
});
|
||||
|
||||
it('with the switch off shows the in-client form', () => {
|
||||
expect(render({ registration: { enabled: false, url: 'https://accounts.example/join' } })).toContain(
|
||||
'in-client register form'
|
||||
);
|
||||
expect(render({})).toContain('in-client register form');
|
||||
});
|
||||
|
||||
it('with the switch on but an unusable url falls back to the form', () => {
|
||||
expect(render({ registration: { enabled: true, url: 'ftp://accounts.example/join' } })).toContain(
|
||||
'in-client register form'
|
||||
);
|
||||
});
|
||||
});
|
||||
26
src/app/pages/auth/register/RegisterRoute.tsx
Normal file
26
src/app/pages/auth/register/RegisterRoute.tsx
Normal file
|
|
@ -0,0 +1,26 @@
|
|||
import { Box, Text } from 'folds';
|
||||
import React, { useEffect } from 'react';
|
||||
import { useClientConfig } from '../../../hooks/useClientConfig';
|
||||
import { registrationEntry } from '../registrationEntry';
|
||||
import { Register } from './Register';
|
||||
|
||||
// With the Register switch on, a stranger gets one way to register: the account service's own
|
||||
// page. This route sends them there (the link stays for when the redirect does not run). With
|
||||
// the switch off, the in-client form is the way.
|
||||
export function RegisterRoute() {
|
||||
const entry = registrationEntry(useClientConfig());
|
||||
useEffect(() => {
|
||||
if (entry) window.location.replace(entry.url);
|
||||
}, [entry]);
|
||||
if (!entry) return <Register />;
|
||||
return (
|
||||
<Box direction="Column" gap="300">
|
||||
<Text size="H2" priority="400">
|
||||
Register
|
||||
</Text>
|
||||
<Text>
|
||||
Sign-up happens on the account page. <a href={entry.url}>Continue to sign up</a>
|
||||
</Text>
|
||||
</Box>
|
||||
);
|
||||
}
|
||||
39
src/app/pages/auth/register/registerErrors.test.ts
Normal file
39
src/app/pages/auth/register/registerErrors.test.ts
Normal file
|
|
@ -0,0 +1,39 @@
|
|||
import { describe, expect, it } from 'vitest';
|
||||
import { registerTroubleMessage } from '../authMessages';
|
||||
import { RegisterError, registerErrorTrouble } from './registerUtil';
|
||||
|
||||
// What the form shows for a failed registration, from the error code the server answered with.
|
||||
const shown = (errcode: string) => {
|
||||
const trouble = registerErrorTrouble(errcode);
|
||||
return trouble && registerTroubleMessage(trouble, 'chat.example');
|
||||
};
|
||||
|
||||
describe('registration failures as the form shows them', () => {
|
||||
it('say what to do next for every failure that is not a username field error', () => {
|
||||
[
|
||||
RegisterError.PasswordWeak,
|
||||
RegisterError.PasswordShort,
|
||||
RegisterError.RateLimited,
|
||||
RegisterError.Forbidden,
|
||||
RegisterError.InvalidRequest,
|
||||
RegisterError.Unknown,
|
||||
].forEach((e) => expect(shown(e)?.length).toBeGreaterThan(20));
|
||||
});
|
||||
|
||||
it('never carry the server text or a Matrix error code', () => {
|
||||
Object.values(RegisterError).forEach((e) => {
|
||||
expect(shown(e) ?? '').not.toMatch(/M_[A-Z_]+|errcode|homeserver does not/i);
|
||||
});
|
||||
});
|
||||
|
||||
it('leave the username errors to their own field messages', () => {
|
||||
[RegisterError.UserTaken, RegisterError.UserInvalid, RegisterError.UserExclusive].forEach((e) =>
|
||||
expect(shown(e)).toBeUndefined()
|
||||
);
|
||||
});
|
||||
|
||||
it('tell a closed server from a rate limit', () => {
|
||||
expect(shown(RegisterError.Forbidden)).toContain('chat.example');
|
||||
expect(shown(RegisterError.RateLimited)).not.toBe(shown(RegisterError.Forbidden));
|
||||
});
|
||||
});
|
||||
|
|
@ -17,6 +17,7 @@ import {
|
|||
import { getHomePath, getLoginPath, withSearchParam } from '../../pathUtils';
|
||||
import { getMxIdLocalPart, getMxIdServer } from '../../../utils/matrix';
|
||||
import { setFallbackSession } from '../../../state/sessions';
|
||||
import type { RegisterTrouble } from '../authMessages';
|
||||
|
||||
export enum RegisterError {
|
||||
UserTaken = 'UserTaken',
|
||||
|
|
@ -30,6 +31,27 @@ export enum RegisterError {
|
|||
Unknown = 'Unknown',
|
||||
}
|
||||
|
||||
// The form-level words for a failed registration, never the server's own text. The username
|
||||
// errors are the form's own field messages and have no trouble here.
|
||||
export const registerErrorTrouble = (errcode: string): RegisterTrouble | undefined => {
|
||||
switch (errcode) {
|
||||
case RegisterError.PasswordWeak:
|
||||
return 'password-weak';
|
||||
case RegisterError.PasswordShort:
|
||||
return 'password-short';
|
||||
case RegisterError.RateLimited:
|
||||
return 'rate-limited';
|
||||
case RegisterError.Forbidden:
|
||||
return 'closed';
|
||||
case RegisterError.InvalidRequest:
|
||||
return 'invalid';
|
||||
case RegisterError.Unknown:
|
||||
return 'unknown';
|
||||
default:
|
||||
return undefined;
|
||||
}
|
||||
};
|
||||
|
||||
export type CustomRegisterResponse = {
|
||||
baseUrl: string;
|
||||
response: RegisterResponse;
|
||||
|
|
|
|||
Loading…
Reference in a new issue