Apply strictNullChecks to src/components/views/auth/* (#10299

* Apply `strictNullChecks` to src/components/views/auth/*

* Iterate PR
This commit is contained in:
Michael Telatynski 2023-03-07 10:45:55 +00:00 committed by GitHub
parent c79eff2292
commit 32aa18ff2e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
16 changed files with 127 additions and 122 deletions

View file

@ -78,9 +78,9 @@ interface IProps {
}
interface IState {
// true if we're waiting for the user to complete
busy: boolean;
errorText?: ReactNode;
// true if we're waiting for the user to complete
// We remember the values entered by the user because
// the registration form will be unmounted during the
// course of registration, but if there's an error we
@ -88,7 +88,7 @@ interface IState {
// values the user entered still in it. We can keep
// them in this component's state since this component
// persist for the duration of the registration process.
formVals: Record<string, string>;
formVals: Record<string, string | undefined>;
// user-interactive auth
// If we've been given a session ID, we're resuming
// straight back into UI auth
@ -96,9 +96,11 @@ interface IState {
// If set, we've registered but are not going to log
// the user in to their new account automatically.
completedNoSignin: boolean;
flows: {
stages: string[];
}[];
flows:
| {
stages: string[];
}[]
| null;
// We perform liveliness checks later, but for now suppress the errors.
// We also track the server dead errors independently of the regular errors so
// that we can render it differently, and override any other error the user may
@ -158,7 +160,7 @@ export default class Registration extends React.Component<IProps, IState> {
window.removeEventListener("beforeunload", this.unloadCallback);
}
private unloadCallback = (event: BeforeUnloadEvent): string => {
private unloadCallback = (event: BeforeUnloadEvent): string | undefined => {
if (this.state.doingUIAuth) {
event.preventDefault();
event.returnValue = "";
@ -215,7 +217,7 @@ export default class Registration extends React.Component<IProps, IState> {
this.loginLogic.setHomeserverUrl(hsUrl);
this.loginLogic.setIdentityServerUrl(isUrl);
let ssoFlow: ISSOFlow;
let ssoFlow: ISSOFlow | undefined;
try {
const loginFlows = await this.loginLogic.getFlows();
if (serverConfig !== this.latestServerConfig) return; // discard, serverConfig changed from under us
@ -289,6 +291,7 @@ export default class Registration extends React.Component<IProps, IState> {
sendAttempt: number,
sessionId: string,
): Promise<IRequestTokenResponse> => {
if (!this.state.matrixClient) throw new Error("Matrix client has not yet been loaded");
return this.state.matrixClient.requestRegisterEmailToken(
emailAddress,
clientSecret,
@ -303,6 +306,8 @@ export default class Registration extends React.Component<IProps, IState> {
};
private onUIAuthFinished: InteractiveAuthCallback = async (success, response): Promise<void> => {
if (!this.state.matrixClient) throw new Error("Matrix client has not yet been loaded");
debuglog("Registration: ui authentication finished: ", { success, response });
if (!success) {
let errorText: ReactNode = (response as Error).message || (response as Error).toString();
@ -327,10 +332,8 @@ export default class Registration extends React.Component<IProps, IState> {
</div>
);
} else if ((response as IAuthData).required_stages?.includes(AuthType.Msisdn)) {
let msisdnAvailable = false;
for (const flow of (response as IAuthData).available_flows) {
msisdnAvailable = msisdnAvailable || flow.stages.includes(AuthType.Msisdn);
}
const flows = (response as IAuthData).available_flows ?? [];
const msisdnAvailable = flows.some((flow) => flow.stages.includes(AuthType.Msisdn));
if (!msisdnAvailable) {
errorText = _t("This server does not support authentication with a phone number.");
}
@ -348,12 +351,16 @@ export default class Registration extends React.Component<IProps, IState> {
return;
}
MatrixClientPeg.setJustRegisteredUserId((response as IAuthData).user_id);
const userId = (response as IAuthData).user_id;
const accessToken = (response as IAuthData).access_token;
if (!userId || !accessToken) throw new Error("Registration failed");
MatrixClientPeg.setJustRegisteredUserId(userId);
const newState: Partial<IState> = {
doingUIAuth: false,
registeredUsername: (response as IAuthData).user_id,
differentLoggedInUserId: null,
differentLoggedInUserId: undefined,
completedNoSignin: false,
// we're still busy until we get unmounted: don't show the registration form again
busy: true,
@ -393,13 +400,13 @@ export default class Registration extends React.Component<IProps, IState> {
// the email, not the client that started the registration flow
await this.props.onLoggedIn(
{
userId: (response as IAuthData).user_id,
userId,
deviceId: (response as IAuthData).device_id,
homeserverUrl: this.state.matrixClient.getHomeserverUrl(),
identityServerUrl: this.state.matrixClient.getIdentityServerUrl(),
accessToken: (response as IAuthData).access_token,
accessToken,
},
this.state.formVals.password,
this.state.formVals.password!,
);
this.setupPushers();
@ -457,6 +464,8 @@ export default class Registration extends React.Component<IProps, IState> {
};
private makeRegisterRequest = (auth: IAuthData | null): Promise<IAuthData> => {
if (!this.state.matrixClient) throw new Error("Matrix client has not yet been loaded");
const registerParams: IRegisterRequestParams = {
username: this.state.formVals.username,
password: this.state.formVals.password,
@ -494,7 +503,7 @@ export default class Registration extends React.Component<IProps, IState> {
return sessionLoaded;
};
private renderRegisterComponent(): JSX.Element {
private renderRegisterComponent(): ReactNode {
if (this.state.matrixClient && this.state.doingUIAuth) {
return (
<InteractiveAuth
@ -517,8 +526,8 @@ export default class Registration extends React.Component<IProps, IState> {
<Spinner />
</div>
);
} else if (this.state.flows.length) {
let ssoSection;
} else if (this.state.matrixClient && this.state.flows.length) {
let ssoSection: JSX.Element | undefined;
if (this.state.ssoFlow) {
let continueWithSection;
const providers = this.state.ssoFlow.identity_providers || [];
@ -571,6 +580,8 @@ export default class Registration extends React.Component<IProps, IState> {
</React.Fragment>
);
}
return null;
}
public render(): React.ReactNode {