From f152613f830ec32a3de3d7f442816a63a4c732c5 Mon Sep 17 00:00:00 2001 From: Michael Telatynski <7t3chguy@gmail.com> Date: Fri, 31 Mar 2023 09:26:15 +0100 Subject: [PATCH] Apply `strictNullChecks` to `src/components/auth/*` (#10484 * Apply `strictNullChecks` to `src/components/auth/*` * fix * strict types --- src/Lifecycle.ts | 2 +- src/Login.ts | 6 +++--- src/components/structures/auth/Login.tsx | 5 +++-- .../structures/auth/SetupEncryptionBody.tsx | 11 ++++++----- src/components/structures/auth/SoftLogout.tsx | 12 +++++++++--- .../structures/auth/header/AuthHeaderContext.tsx | 2 +- .../structures/auth/header/AuthHeaderDisplay.tsx | 2 +- .../structures/auth/header/AuthHeaderModifier.tsx | 2 +- 8 files changed, 25 insertions(+), 17 deletions(-) diff --git a/src/Lifecycle.ts b/src/Lifecycle.ts index b1f12232a5..bd45dd1c4b 100644 --- a/src/Lifecycle.ts +++ b/src/Lifecycle.ts @@ -200,7 +200,7 @@ export function attemptTokenLogin( } const homeserver = localStorage.getItem(SSO_HOMESERVER_URL_KEY); - const identityServer = localStorage.getItem(SSO_ID_SERVER_URL_KEY); + const identityServer = localStorage.getItem(SSO_ID_SERVER_URL_KEY) ?? undefined; if (!homeserver) { logger.warn("Cannot log in with token: can't determine HS URL to use"); Modal.createDialog(ErrorDialog, { diff --git a/src/Login.ts b/src/Login.ts index 73e6366956..569363eeb0 100644 --- a/src/Login.ts +++ b/src/Login.ts @@ -164,7 +164,7 @@ export default class Login { */ export async function sendLoginRequest( hsUrl: string, - isUrl: string, + isUrl: string | undefined, loginType: string, loginParams: ILoginParams, ): Promise { @@ -177,11 +177,11 @@ export async function sendLoginRequest( const wellknown = data.well_known; if (wellknown) { - if (wellknown["m.homeserver"] && wellknown["m.homeserver"]["base_url"]) { + if (wellknown["m.homeserver"]?.["base_url"]) { hsUrl = wellknown["m.homeserver"]["base_url"]; logger.log(`Overrode homeserver setting with ${hsUrl} from login response`); } - if (wellknown["m.identity_server"] && wellknown["m.identity_server"]["base_url"]) { + if (wellknown["m.identity_server"]?.["base_url"]) { // TODO: should we prompt here? isUrl = wellknown["m.identity_server"]["base_url"]; logger.log(`Overrode IS setting with ${isUrl} from login response`); diff --git a/src/components/structures/auth/Login.tsx b/src/components/structures/auth/Login.tsx index 3be905878d..43147266a4 100644 --- a/src/components/structures/auth/Login.tsx +++ b/src/components/structures/auth/Login.tsx @@ -90,7 +90,7 @@ interface IState { // used for preserving form values when changing homeserver username: string; - phoneCountry?: string; + phoneCountry: string; phoneNumber: string; // We perform liveliness checks later, but for now suppress the errors. @@ -126,6 +126,7 @@ export default class LoginComponent extends React.PureComponent canTryLogin: true, username: props.defaultUsername ? props.defaultUsername : "", + phoneCountry: "", phoneNumber: "", serverIsAlive: true, @@ -217,7 +218,7 @@ export default class LoginComponent extends React.PureComponent let errorText: ReactNode; // Some error strings only apply for logging in - const usingEmail = username?.indexOf("@") > 0; + const usingEmail = username && username.indexOf("@") > 0; if (error.httpStatus === 400 && usingEmail) { errorText = _t("This homeserver does not support login using email address."); } else if (error.errcode === "M_RESOURCE_LIMIT_EXCEEDED") { diff --git a/src/components/structures/auth/SetupEncryptionBody.tsx b/src/components/structures/auth/SetupEncryptionBody.tsx index 9a28d20f75..2cbfbf8367 100644 --- a/src/components/structures/auth/SetupEncryptionBody.tsx +++ b/src/components/structures/auth/SetupEncryptionBody.tsx @@ -39,8 +39,8 @@ interface IProps { interface IState { phase: Phase; - verificationRequest: VerificationRequest; - backupInfo: IKeyBackupInfo; + verificationRequest: VerificationRequest | null; + backupInfo: IKeyBackupInfo | null; lostKeys: boolean; } @@ -96,7 +96,7 @@ export default class SetupEncryptionBody extends React.Component this.props.onFinished(); Modal.createDialog(VerificationRequestDialog, { verificationRequestPromise: requestPromise, - member: cli.getUser(userId), + member: cli.getUser(userId) ?? undefined, onFinished: async (): Promise => { const request = await requestPromise; request.cancel(); @@ -142,15 +142,16 @@ export default class SetupEncryptionBody extends React.Component }; public render(): React.ReactNode { + const cli = MatrixClientPeg.get(); const { phase, lostKeys } = this.state; - if (this.state.verificationRequest) { + if (this.state.verificationRequest && cli.getUser(this.state.verificationRequest.otherUserId)) { return ( ); diff --git a/src/components/structures/auth/SoftLogout.tsx b/src/components/structures/auth/SoftLogout.tsx index 3eb997c843..a7a377e1d1 100644 --- a/src/components/structures/auth/SoftLogout.tsx +++ b/src/components/structures/auth/SoftLogout.tsx @@ -114,7 +114,7 @@ export default class SoftLogout extends React.Component { private async initLogin(): Promise { const queryParams = this.props.realQueryParams; - const hasAllParams = queryParams && queryParams["loginToken"]; + const hasAllParams = queryParams?.["loginToken"]; if (hasAllParams) { this.setState({ loginView: LoginView.Loading }); this.trySsoLogin(); @@ -156,7 +156,7 @@ export default class SoftLogout extends React.Component { user: MatrixClientPeg.get().getUserId(), }, password: this.state.password, - device_id: MatrixClientPeg.get().getDeviceId(), + device_id: MatrixClientPeg.get().getDeviceId() ?? undefined, }; let credentials: IMatrixClientCreds; @@ -185,11 +185,17 @@ export default class SoftLogout extends React.Component { this.setState({ busy: true }); const hsUrl = localStorage.getItem(SSO_HOMESERVER_URL_KEY); + if (!hsUrl) { + logger.error("Homeserver URL unknown for SSO login callback"); + this.setState({ busy: false, loginView: LoginView.Unsupported }); + return; + } + const isUrl = localStorage.getItem(SSO_ID_SERVER_URL_KEY) || MatrixClientPeg.get().getIdentityServerUrl(); const loginType = "m.login.token"; const loginParams = { token: this.props.realQueryParams["loginToken"], - device_id: MatrixClientPeg.get().getDeviceId(), + device_id: MatrixClientPeg.get().getDeviceId() ?? undefined, }; let credentials: IMatrixClientCreds; diff --git a/src/components/structures/auth/header/AuthHeaderContext.tsx b/src/components/structures/auth/header/AuthHeaderContext.tsx index 347b26252d..67b74e5c8e 100644 --- a/src/components/structures/auth/header/AuthHeaderContext.tsx +++ b/src/components/structures/auth/header/AuthHeaderContext.tsx @@ -23,4 +23,4 @@ interface AuthHeaderContextType { dispatch: Dispatch>; } -export const AuthHeaderContext = createContext(undefined); +export const AuthHeaderContext = createContext(undefined); diff --git a/src/components/structures/auth/header/AuthHeaderDisplay.tsx b/src/components/structures/auth/header/AuthHeaderDisplay.tsx index 121859498d..2a6a8546c3 100644 --- a/src/components/structures/auth/header/AuthHeaderDisplay.tsx +++ b/src/components/structures/auth/header/AuthHeaderDisplay.tsx @@ -29,7 +29,7 @@ export function AuthHeaderDisplay({ title, icon, serverPicker, children }: Props if (!context) { return <>; } - const current = context.state.length ? context.state[0] : null; + const current = context.state[0] ?? null; return ( {current?.icon ?? icon} diff --git a/src/components/structures/auth/header/AuthHeaderModifier.tsx b/src/components/structures/auth/header/AuthHeaderModifier.tsx index 46644b7390..cfc1382588 100644 --- a/src/components/structures/auth/header/AuthHeaderModifier.tsx +++ b/src/components/structures/auth/header/AuthHeaderModifier.tsx @@ -27,7 +27,7 @@ interface Props { export function AuthHeaderModifier(props: Props): null { const context = useContext(AuthHeaderContext); - const dispatch = context ? context.dispatch : null; + const dispatch = context?.dispatch ?? null; useEffect(() => { if (!dispatch) { return;