2022-01-06 09:19:03 +08:00
|
|
|
/*
|
2022-05-28 04:08:03 +08:00
|
|
|
Copyright 2021-2022 New Vector Ltd
|
2022-01-06 09:19:03 +08:00
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2023-07-01 06:21:18 +08:00
|
|
|
import {
|
2022-05-28 04:08:03 +08:00
|
|
|
FC,
|
2022-01-06 09:19:03 +08:00
|
|
|
useCallback,
|
|
|
|
useEffect,
|
|
|
|
useState,
|
|
|
|
createContext,
|
|
|
|
useContext,
|
2022-11-02 23:23:05 +08:00
|
|
|
useRef,
|
2023-07-04 01:45:42 +08:00
|
|
|
useMemo,
|
2022-01-06 09:19:03 +08:00
|
|
|
} from "react";
|
|
|
|
import { useHistory } from "react-router-dom";
|
2023-06-10 01:18:30 +08:00
|
|
|
import { ClientEvent, MatrixClient } from "matrix-js-sdk/src/client";
|
2022-06-28 05:41:07 +08:00
|
|
|
import { logger } from "matrix-js-sdk/src/logger";
|
2022-10-10 21:19:10 +08:00
|
|
|
import { useTranslation } from "react-i18next";
|
2023-06-10 01:18:30 +08:00
|
|
|
import { ISyncStateData, SyncState } from "matrix-js-sdk/src/sync";
|
2022-05-28 04:08:03 +08:00
|
|
|
|
2022-02-11 09:10:36 +08:00
|
|
|
import { ErrorView } from "./FullScreenView";
|
2022-07-28 04:14:05 +08:00
|
|
|
import {
|
2022-08-13 05:58:29 +08:00
|
|
|
CryptoStoreIntegrityError,
|
2022-10-26 19:58:41 +08:00
|
|
|
fallbackICEServerAllowed,
|
2023-06-30 23:43:28 +08:00
|
|
|
initClient,
|
2022-07-28 04:14:05 +08:00
|
|
|
} from "./matrix-utils";
|
2022-09-09 14:08:17 +08:00
|
|
|
import { widget } from "./widget";
|
2023-03-01 20:47:36 +08:00
|
|
|
import {
|
|
|
|
PosthogAnalytics,
|
|
|
|
RegistrationType,
|
|
|
|
} from "./analytics/PosthogAnalytics";
|
2022-10-10 21:19:10 +08:00
|
|
|
import { translatedError } from "./TranslatedError";
|
2022-11-22 01:39:48 +08:00
|
|
|
import { useEventTarget } from "./useEvents";
|
2022-12-21 01:26:45 +08:00
|
|
|
import { Config } from "./config/Config";
|
2022-01-06 09:19:03 +08:00
|
|
|
|
2022-05-28 04:08:03 +08:00
|
|
|
declare global {
|
|
|
|
interface Window {
|
|
|
|
matrixclient: MatrixClient;
|
2023-06-30 23:43:28 +08:00
|
|
|
passwordlessUser: boolean;
|
2022-05-28 04:08:03 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-06-30 23:43:28 +08:00
|
|
|
export type ClientState = ValidClientState | ErrorState;
|
2022-11-22 01:39:48 +08:00
|
|
|
|
2023-06-30 23:43:28 +08:00
|
|
|
export type ValidClientState = {
|
|
|
|
state: "valid";
|
|
|
|
authenticated?: AuthenticatedClient;
|
2023-07-22 03:03:37 +08:00
|
|
|
// 'Disconnected' rather than 'connected' because it tracks specifically
|
|
|
|
// whether the client is supposed to be connected but is not
|
|
|
|
disconnected: boolean;
|
2023-06-30 23:43:28 +08:00
|
|
|
setClient: (params?: SetClientParams) => void;
|
2022-05-28 04:08:03 +08:00
|
|
|
};
|
|
|
|
|
2023-06-30 23:43:28 +08:00
|
|
|
export type AuthenticatedClient = {
|
2022-05-28 04:08:03 +08:00
|
|
|
client: MatrixClient;
|
2023-06-30 23:43:28 +08:00
|
|
|
isPasswordlessUser: boolean;
|
2022-05-28 04:08:03 +08:00
|
|
|
changePassword: (password: string) => Promise<void>;
|
|
|
|
logout: () => void;
|
2023-06-30 23:43:28 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
export type ErrorState = {
|
|
|
|
state: "error";
|
|
|
|
error: Error;
|
|
|
|
};
|
|
|
|
|
|
|
|
export type SetClientParams = {
|
|
|
|
client: MatrixClient;
|
|
|
|
session: Session;
|
|
|
|
};
|
|
|
|
|
|
|
|
const ClientContext = createContext<ClientState | undefined>(undefined);
|
|
|
|
|
2023-09-23 06:05:13 +08:00
|
|
|
export const useClientState = (): ClientState | undefined =>
|
|
|
|
useContext(ClientContext);
|
2023-06-30 23:43:28 +08:00
|
|
|
|
|
|
|
export function useClient(): {
|
|
|
|
client?: MatrixClient;
|
|
|
|
setClient?: (params?: SetClientParams) => void;
|
|
|
|
} {
|
|
|
|
let client;
|
|
|
|
let setClient;
|
|
|
|
|
|
|
|
const clientState = useClientState();
|
|
|
|
if (clientState?.state === "valid") {
|
|
|
|
client = clientState.authenticated?.client;
|
|
|
|
setClient = clientState.setClient;
|
|
|
|
}
|
|
|
|
|
|
|
|
return { client, setClient };
|
2022-05-28 04:08:03 +08:00
|
|
|
}
|
|
|
|
|
2023-06-30 23:43:28 +08:00
|
|
|
// Plain representation of the `ClientContext` as a helper for old components that expected an object with multiple fields.
|
|
|
|
export function useClientLegacy(): {
|
|
|
|
client?: MatrixClient;
|
|
|
|
setClient?: (params?: SetClientParams) => void;
|
|
|
|
passwordlessUser: boolean;
|
|
|
|
loading: boolean;
|
|
|
|
authenticated: boolean;
|
|
|
|
logout?: () => void;
|
|
|
|
error?: Error;
|
|
|
|
} {
|
|
|
|
const clientState = useClientState();
|
|
|
|
|
|
|
|
let client;
|
|
|
|
let setClient;
|
|
|
|
let passwordlessUser = false;
|
|
|
|
let loading = true;
|
|
|
|
let error;
|
|
|
|
let authenticated = false;
|
|
|
|
let logout;
|
|
|
|
|
|
|
|
if (clientState?.state === "valid") {
|
|
|
|
client = clientState.authenticated?.client;
|
|
|
|
setClient = clientState.setClient;
|
|
|
|
passwordlessUser = clientState.authenticated?.isPasswordlessUser ?? false;
|
|
|
|
loading = false;
|
|
|
|
authenticated = client !== undefined;
|
|
|
|
logout = clientState.authenticated?.logout;
|
|
|
|
} else if (clientState?.state === "error") {
|
|
|
|
error = clientState.error;
|
|
|
|
loading = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
client,
|
|
|
|
setClient,
|
|
|
|
passwordlessUser,
|
|
|
|
loading,
|
|
|
|
authenticated,
|
|
|
|
logout,
|
|
|
|
error,
|
|
|
|
};
|
|
|
|
}
|
2022-05-28 04:08:03 +08:00
|
|
|
|
2023-06-30 23:43:28 +08:00
|
|
|
const loadChannel =
|
|
|
|
"BroadcastChannel" in window ? new BroadcastChannel("load") : null;
|
2022-01-06 09:19:03 +08:00
|
|
|
|
2022-07-08 21:56:00 +08:00
|
|
|
interface Props {
|
|
|
|
children: JSX.Element;
|
|
|
|
}
|
|
|
|
|
|
|
|
export const ClientProvider: FC<Props> = ({ children }) => {
|
2022-01-06 09:19:03 +08:00
|
|
|
const history = useHistory();
|
|
|
|
|
2023-07-22 03:08:53 +08:00
|
|
|
// null = signed out, undefined = loading
|
2023-06-30 23:43:28 +08:00
|
|
|
const [initClientState, setInitClientState] = useState<
|
2023-07-22 03:08:53 +08:00
|
|
|
InitResult | null | undefined
|
2023-06-30 23:43:28 +08:00
|
|
|
>(undefined);
|
|
|
|
|
|
|
|
const initializing = useRef(false);
|
2022-01-06 09:19:03 +08:00
|
|
|
useEffect(() => {
|
2022-11-02 23:23:05 +08:00
|
|
|
// In case the component is mounted, unmounted, and remounted quickly (as
|
|
|
|
// React does in strict mode), we need to make sure not to doubly initialize
|
2023-06-30 23:43:28 +08:00
|
|
|
// the client.
|
2022-11-02 23:23:05 +08:00
|
|
|
if (initializing.current) return;
|
|
|
|
initializing.current = true;
|
|
|
|
|
2023-06-30 23:43:28 +08:00
|
|
|
loadClient()
|
2023-07-22 03:08:53 +08:00
|
|
|
.then(setInitClientState)
|
2023-06-30 23:43:28 +08:00
|
|
|
.catch((err) => logger.error(err))
|
2022-11-02 23:23:05 +08:00
|
|
|
.finally(() => (initializing.current = false));
|
2022-01-06 09:19:03 +08:00
|
|
|
}, []);
|
|
|
|
|
|
|
|
const changePassword = useCallback(
|
2022-05-28 04:08:03 +08:00
|
|
|
async (password: string) => {
|
2023-06-30 23:43:28 +08:00
|
|
|
const session = loadSession();
|
|
|
|
if (!initClientState?.client || !session) {
|
|
|
|
return;
|
|
|
|
}
|
2022-01-06 09:19:03 +08:00
|
|
|
|
2023-06-30 23:43:28 +08:00
|
|
|
await initClientState.client.setPassword(
|
2022-01-06 09:19:03 +08:00
|
|
|
{
|
|
|
|
type: "m.login.password",
|
|
|
|
identifier: {
|
|
|
|
type: "m.id.user",
|
2022-05-28 04:08:03 +08:00
|
|
|
user: session.user_id,
|
2022-01-06 09:19:03 +08:00
|
|
|
},
|
2022-05-28 04:08:03 +08:00
|
|
|
user: session.user_id,
|
2023-06-30 23:43:28 +08:00
|
|
|
password: session.tempPassword,
|
2022-01-06 09:19:03 +08:00
|
|
|
},
|
2023-10-11 22:42:04 +08:00
|
|
|
password,
|
2022-01-06 09:19:03 +08:00
|
|
|
);
|
|
|
|
|
2022-05-28 04:08:03 +08:00
|
|
|
saveSession({ ...session, passwordlessUser: false });
|
2022-01-06 09:19:03 +08:00
|
|
|
|
2023-06-30 23:43:28 +08:00
|
|
|
setInitClientState({
|
|
|
|
client: initClientState.client,
|
|
|
|
passwordlessUser: false,
|
2022-01-06 09:19:03 +08:00
|
|
|
});
|
|
|
|
},
|
2023-10-11 22:42:04 +08:00
|
|
|
[initClientState?.client],
|
2022-01-06 09:19:03 +08:00
|
|
|
);
|
|
|
|
|
2022-02-16 04:46:58 +08:00
|
|
|
const setClient = useCallback(
|
2023-06-30 23:43:28 +08:00
|
|
|
(clientParams?: SetClientParams) => {
|
|
|
|
const oldClient = initClientState?.client;
|
|
|
|
const newClient = clientParams?.client;
|
|
|
|
if (oldClient && oldClient !== newClient) {
|
|
|
|
oldClient.stopClient();
|
2022-02-16 04:46:58 +08:00
|
|
|
}
|
2022-01-06 09:19:03 +08:00
|
|
|
|
2023-06-30 23:43:28 +08:00
|
|
|
if (clientParams) {
|
|
|
|
saveSession(clientParams.session);
|
|
|
|
setInitClientState({
|
|
|
|
client: clientParams.client,
|
|
|
|
passwordlessUser: clientParams.session.passwordlessUser,
|
2022-02-16 04:46:58 +08:00
|
|
|
});
|
|
|
|
} else {
|
2022-05-28 04:08:03 +08:00
|
|
|
clearSession();
|
2023-08-10 00:00:13 +08:00
|
|
|
setInitClientState(null);
|
2022-02-16 04:46:58 +08:00
|
|
|
}
|
|
|
|
},
|
2023-10-11 22:42:04 +08:00
|
|
|
[initClientState?.client],
|
2022-02-16 04:46:58 +08:00
|
|
|
);
|
2022-01-06 09:19:03 +08:00
|
|
|
|
2022-09-13 22:48:04 +08:00
|
|
|
const logout = useCallback(async () => {
|
2023-06-30 23:43:28 +08:00
|
|
|
const client = initClientState?.client;
|
|
|
|
if (!client) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-10-14 09:25:15 +08:00
|
|
|
await client.logout(true);
|
2022-09-26 20:01:43 +08:00
|
|
|
await client.clearStores();
|
2022-05-28 04:08:03 +08:00
|
|
|
clearSession();
|
2023-08-10 00:00:13 +08:00
|
|
|
setInitClientState(null);
|
2022-05-28 04:08:03 +08:00
|
|
|
history.push("/");
|
2022-11-04 20:07:14 +08:00
|
|
|
PosthogAnalytics.instance.setRegistrationType(RegistrationType.Guest);
|
2023-06-30 23:43:28 +08:00
|
|
|
}, [history, initClientState?.client]);
|
2022-01-06 09:19:03 +08:00
|
|
|
|
2022-10-10 21:19:10 +08:00
|
|
|
const { t } = useTranslation();
|
|
|
|
|
2022-11-22 01:39:48 +08:00
|
|
|
// To protect against multiple sessions writing to the same storage
|
|
|
|
// simultaneously, we send a broadcast message that shuts down all other
|
|
|
|
// running instances of the app. This isn't necessary if the app is running in
|
|
|
|
// a widget though, since then it'll be mostly stateless.
|
2022-02-11 09:10:36 +08:00
|
|
|
useEffect(() => {
|
2022-11-29 05:15:47 +08:00
|
|
|
if (!widget) loadChannel?.postMessage({});
|
2022-11-22 01:39:48 +08:00
|
|
|
}, []);
|
2022-02-11 09:10:36 +08:00
|
|
|
|
2023-06-30 23:43:28 +08:00
|
|
|
const [alreadyOpenedErr, setAlreadyOpenedErr] = useState<Error | undefined>(
|
2023-10-11 22:42:04 +08:00
|
|
|
undefined,
|
2023-06-30 23:43:28 +08:00
|
|
|
);
|
2022-11-22 01:39:48 +08:00
|
|
|
useEventTarget(
|
|
|
|
loadChannel,
|
|
|
|
"message",
|
|
|
|
useCallback(() => {
|
2023-06-30 23:43:28 +08:00
|
|
|
initClientState?.client.stopClient();
|
|
|
|
setAlreadyOpenedErr(
|
2023-10-11 22:42:04 +08:00
|
|
|
translatedError("This application has been opened in another tab.", t),
|
2023-06-30 23:43:28 +08:00
|
|
|
);
|
2023-10-11 22:42:04 +08:00
|
|
|
}, [initClientState?.client, setAlreadyOpenedErr, t]),
|
2022-11-22 01:39:48 +08:00
|
|
|
);
|
2022-02-11 09:10:36 +08:00
|
|
|
|
2023-07-22 03:03:37 +08:00
|
|
|
const [isDisconnected, setIsDisconnected] = useState(false);
|
|
|
|
|
2023-07-22 03:08:53 +08:00
|
|
|
const state: ClientState | undefined = useMemo(() => {
|
2023-06-30 23:43:28 +08:00
|
|
|
if (alreadyOpenedErr) {
|
2023-07-04 01:45:42 +08:00
|
|
|
return { state: "error", error: alreadyOpenedErr };
|
2023-06-30 23:43:28 +08:00
|
|
|
}
|
|
|
|
|
2023-07-22 03:08:53 +08:00
|
|
|
if (initClientState === undefined) return undefined;
|
|
|
|
|
|
|
|
const authenticated =
|
|
|
|
initClientState === null
|
|
|
|
? undefined
|
|
|
|
: {
|
|
|
|
client: initClientState.client,
|
|
|
|
isPasswordlessUser: initClientState.passwordlessUser,
|
|
|
|
changePassword,
|
|
|
|
logout,
|
|
|
|
};
|
2023-06-30 23:43:28 +08:00
|
|
|
|
2023-07-22 03:03:37 +08:00
|
|
|
return {
|
|
|
|
state: "valid",
|
|
|
|
authenticated,
|
2022-01-06 09:19:03 +08:00
|
|
|
setClient,
|
2023-07-22 03:03:37 +08:00
|
|
|
disconnected: isDisconnected,
|
|
|
|
};
|
|
|
|
}, [
|
|
|
|
alreadyOpenedErr,
|
|
|
|
changePassword,
|
|
|
|
initClientState,
|
|
|
|
logout,
|
|
|
|
setClient,
|
|
|
|
isDisconnected,
|
|
|
|
]);
|
|
|
|
|
|
|
|
const onSync = useCallback(
|
|
|
|
(state: SyncState, _old: SyncState | null, data?: ISyncStateData) => {
|
|
|
|
setIsDisconnected(clientIsDisconnected(state, data));
|
|
|
|
},
|
2023-10-11 22:42:04 +08:00
|
|
|
[],
|
2022-01-06 09:19:03 +08:00
|
|
|
);
|
|
|
|
|
2022-02-16 04:46:58 +08:00
|
|
|
useEffect(() => {
|
2023-06-30 23:43:28 +08:00
|
|
|
if (!initClientState) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
window.matrixclient = initClientState.client;
|
|
|
|
window.passwordlessUser = initClientState.passwordlessUser;
|
2023-04-05 20:06:55 +08:00
|
|
|
|
2023-04-05 22:00:14 +08:00
|
|
|
if (PosthogAnalytics.hasInstance())
|
|
|
|
PosthogAnalytics.instance.onLoginStatusChanged();
|
2022-02-16 04:46:58 +08:00
|
|
|
|
2023-07-22 03:03:37 +08:00
|
|
|
if (initClientState.client) {
|
|
|
|
initClientState.client.on(ClientEvent.Sync, onSync);
|
|
|
|
}
|
|
|
|
|
|
|
|
return () => {
|
|
|
|
if (initClientState.client) {
|
|
|
|
initClientState.client.removeListener(ClientEvent.Sync, onSync);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}, [initClientState, onSync]);
|
2022-02-16 04:46:58 +08:00
|
|
|
|
2023-06-30 23:43:28 +08:00
|
|
|
if (alreadyOpenedErr) {
|
|
|
|
return <ErrorView error={alreadyOpenedErr} />;
|
2022-02-11 09:10:36 +08:00
|
|
|
}
|
|
|
|
|
2022-01-06 09:19:03 +08:00
|
|
|
return (
|
2023-06-30 23:43:28 +08:00
|
|
|
<ClientContext.Provider value={state}>{children}</ClientContext.Provider>
|
2022-01-06 09:19:03 +08:00
|
|
|
);
|
2022-05-28 04:08:03 +08:00
|
|
|
};
|
2022-01-06 09:19:03 +08:00
|
|
|
|
2023-06-30 23:43:28 +08:00
|
|
|
type InitResult = {
|
|
|
|
client: MatrixClient;
|
|
|
|
passwordlessUser: boolean;
|
|
|
|
};
|
|
|
|
|
2023-07-22 03:08:53 +08:00
|
|
|
async function loadClient(): Promise<InitResult | null> {
|
2023-06-30 23:43:28 +08:00
|
|
|
if (widget) {
|
|
|
|
// We're inside a widget, so let's engage *matryoshka mode*
|
|
|
|
logger.log("Using a matryoshka client");
|
|
|
|
const client = await widget.client;
|
|
|
|
return {
|
|
|
|
client,
|
|
|
|
passwordlessUser: false,
|
|
|
|
};
|
|
|
|
} else {
|
|
|
|
// We're running as a standalone application
|
|
|
|
try {
|
|
|
|
const session = loadSession();
|
|
|
|
if (!session) {
|
2023-07-22 03:08:53 +08:00
|
|
|
logger.log("No session stored; continuing without a client");
|
|
|
|
return null;
|
2023-06-30 23:43:28 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
logger.log("Using a standalone client");
|
|
|
|
|
|
|
|
/* eslint-disable camelcase */
|
|
|
|
const { user_id, device_id, access_token, passwordlessUser } = session;
|
|
|
|
const initClientParams = {
|
|
|
|
baseUrl: Config.defaultHomeserverUrl()!,
|
|
|
|
accessToken: access_token,
|
|
|
|
userId: user_id,
|
|
|
|
deviceId: device_id,
|
|
|
|
fallbackICEServerAllowed: fallbackICEServerAllowed,
|
2023-07-13 00:57:54 +08:00
|
|
|
livekitServiceURL: Config.get().livekit!.livekit_service_url,
|
2023-06-30 23:43:28 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
try {
|
|
|
|
const client = await initClient(initClientParams, true);
|
|
|
|
return {
|
|
|
|
client,
|
|
|
|
passwordlessUser,
|
|
|
|
};
|
|
|
|
} catch (err) {
|
|
|
|
if (err instanceof CryptoStoreIntegrityError) {
|
|
|
|
// We can't use this session anymore, so let's log it out
|
|
|
|
try {
|
|
|
|
const client = await initClient(initClientParams, false); // Don't need the crypto store just to log out)
|
|
|
|
await client.logout(true);
|
|
|
|
} catch (err) {
|
|
|
|
logger.warn(
|
|
|
|
"The previous session was lost, and we couldn't log it out, " +
|
|
|
|
err +
|
2023-10-11 22:42:04 +08:00
|
|
|
"either",
|
2023-06-30 23:43:28 +08:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
throw err;
|
|
|
|
}
|
|
|
|
/* eslint-enable camelcase */
|
|
|
|
} catch (err) {
|
|
|
|
clearSession();
|
|
|
|
throw err;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export interface Session {
|
|
|
|
user_id: string;
|
|
|
|
device_id: string;
|
|
|
|
access_token: string;
|
|
|
|
passwordlessUser: boolean;
|
|
|
|
tempPassword?: string;
|
|
|
|
}
|
|
|
|
|
2023-09-23 06:05:13 +08:00
|
|
|
const clearSession = (): void => localStorage.removeItem("matrix-auth-store");
|
|
|
|
const saveSession = (s: Session): void =>
|
2023-06-30 23:43:28 +08:00
|
|
|
localStorage.setItem("matrix-auth-store", JSON.stringify(s));
|
|
|
|
const loadSession = (): Session | undefined => {
|
|
|
|
const data = localStorage.getItem("matrix-auth-store");
|
|
|
|
if (!data) {
|
|
|
|
return undefined;
|
|
|
|
}
|
|
|
|
|
|
|
|
return JSON.parse(data);
|
|
|
|
};
|
2023-07-22 03:03:37 +08:00
|
|
|
|
|
|
|
const clientIsDisconnected = (
|
|
|
|
syncState: SyncState,
|
2023-10-11 22:42:04 +08:00
|
|
|
syncData?: ISyncStateData,
|
2023-09-23 06:05:13 +08:00
|
|
|
): boolean =>
|
|
|
|
syncState === "ERROR" && syncData?.error?.name === "ConnectionError";
|