2022-01-06 01:19:03 +00:00
|
|
|
/*
|
2022-05-27 20:08:03 +00:00
|
|
|
Copyright 2021-2022 New Vector Ltd
|
2022-01-06 01:19:03 +00: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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
import React, {
|
2022-05-27 20:08:03 +00:00
|
|
|
FC,
|
2022-01-06 01:19:03 +00:00
|
|
|
useCallback,
|
|
|
|
useEffect,
|
|
|
|
useState,
|
|
|
|
createContext,
|
|
|
|
useMemo,
|
|
|
|
useContext,
|
|
|
|
} from "react";
|
|
|
|
import { useHistory } from "react-router-dom";
|
2022-05-27 20:08:03 +00:00
|
|
|
import { MatrixClient, ClientEvent } from "matrix-js-sdk/src/client";
|
|
|
|
import { MatrixEvent } from "matrix-js-sdk/src/models/event";
|
|
|
|
|
2022-02-11 01:10:36 +00:00
|
|
|
import { ErrorView } from "./FullScreenView";
|
2022-01-06 21:33:13 +00:00
|
|
|
import { initClient, defaultHomeserver } from "./matrix-utils";
|
2022-01-06 01:19:03 +00:00
|
|
|
|
2022-05-27 20:08:03 +00:00
|
|
|
declare global {
|
|
|
|
interface Window {
|
|
|
|
matrixclient: MatrixClient;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export interface Session {
|
|
|
|
user_id: string;
|
|
|
|
device_id: string;
|
|
|
|
access_token: string;
|
|
|
|
passwordlessUser: boolean;
|
|
|
|
tempPassword?: string;
|
|
|
|
}
|
|
|
|
|
|
|
|
const loadSession = (): Session => {
|
|
|
|
const data = localStorage.getItem("matrix-auth-store");
|
|
|
|
if (data) return JSON.parse(data);
|
|
|
|
return null;
|
|
|
|
};
|
|
|
|
const saveSession = (session: Session) =>
|
|
|
|
localStorage.setItem("matrix-auth-store", JSON.stringify(session));
|
|
|
|
const clearSession = () => localStorage.removeItem("matrix-auth-store");
|
|
|
|
|
|
|
|
interface ClientState {
|
|
|
|
loading: boolean;
|
|
|
|
isAuthenticated: boolean;
|
|
|
|
isPasswordlessUser: boolean;
|
|
|
|
client: MatrixClient;
|
|
|
|
userName: string;
|
|
|
|
changePassword: (password: string) => Promise<void>;
|
|
|
|
logout: () => void;
|
|
|
|
setClient: (client: MatrixClient, session: Session) => void;
|
|
|
|
}
|
|
|
|
|
|
|
|
const ClientContext = createContext<ClientState>(null);
|
|
|
|
|
|
|
|
type ClientProviderState = Omit<
|
|
|
|
ClientState,
|
|
|
|
"changePassword" | "logout" | "setClient"
|
|
|
|
> & { error?: Error };
|
2022-01-06 01:19:03 +00:00
|
|
|
|
2022-05-27 20:08:03 +00:00
|
|
|
export const ClientProvider: FC = ({ children }) => {
|
2022-01-06 01:19:03 +00:00
|
|
|
const history = useHistory();
|
|
|
|
const [
|
2022-02-11 01:10:36 +00:00
|
|
|
{ loading, isAuthenticated, isPasswordlessUser, client, userName, error },
|
2022-01-06 01:19:03 +00:00
|
|
|
setState,
|
2022-05-27 20:08:03 +00:00
|
|
|
] = useState<ClientProviderState>({
|
2022-01-06 01:19:03 +00:00
|
|
|
loading: true,
|
|
|
|
isAuthenticated: false,
|
|
|
|
isPasswordlessUser: false,
|
|
|
|
client: undefined,
|
|
|
|
userName: null,
|
2022-02-11 01:10:36 +00:00
|
|
|
error: undefined,
|
2022-01-06 01:19:03 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
useEffect(() => {
|
2022-05-27 20:08:03 +00:00
|
|
|
const restore = async (): Promise<
|
|
|
|
Pick<ClientProviderState, "client" | "isPasswordlessUser">
|
|
|
|
> => {
|
2022-01-06 01:19:03 +00:00
|
|
|
try {
|
2022-05-27 20:08:03 +00:00
|
|
|
const session = loadSession();
|
2022-01-06 01:19:03 +00:00
|
|
|
|
2022-05-27 20:08:03 +00:00
|
|
|
if (session) {
|
|
|
|
/* eslint-disable camelcase */
|
|
|
|
const { user_id, device_id, access_token, passwordlessUser } =
|
|
|
|
session;
|
2022-01-06 01:19:03 +00:00
|
|
|
|
|
|
|
const client = await initClient({
|
|
|
|
baseUrl: defaultHomeserver,
|
|
|
|
accessToken: access_token,
|
|
|
|
userId: user_id,
|
|
|
|
deviceId: device_id,
|
|
|
|
});
|
2022-05-27 20:08:03 +00:00
|
|
|
/* eslint-enable camelcase */
|
2022-01-06 01:19:03 +00:00
|
|
|
|
2022-05-27 20:08:03 +00:00
|
|
|
return { client, isPasswordlessUser: passwordlessUser };
|
2022-01-06 01:19:03 +00:00
|
|
|
}
|
|
|
|
|
2022-05-27 20:08:03 +00:00
|
|
|
return { client: undefined, isPasswordlessUser: false };
|
2022-01-06 01:19:03 +00:00
|
|
|
} catch (err) {
|
2022-01-06 21:33:13 +00:00
|
|
|
console.error(err);
|
2022-05-27 20:08:03 +00:00
|
|
|
clearSession();
|
2022-01-06 01:19:03 +00:00
|
|
|
throw err;
|
|
|
|
}
|
2022-05-27 20:08:03 +00:00
|
|
|
};
|
2022-01-06 01:19:03 +00:00
|
|
|
|
|
|
|
restore()
|
2022-05-27 20:08:03 +00:00
|
|
|
.then(({ client, isPasswordlessUser }) => {
|
2022-01-06 01:19:03 +00:00
|
|
|
setState({
|
|
|
|
client,
|
|
|
|
loading: false,
|
2022-05-27 20:08:03 +00:00
|
|
|
isAuthenticated: Boolean(client),
|
|
|
|
isPasswordlessUser,
|
2022-01-06 01:19:03 +00:00
|
|
|
userName: client?.getUserIdLocalpart(),
|
|
|
|
});
|
|
|
|
})
|
|
|
|
.catch(() => {
|
|
|
|
setState({
|
|
|
|
client: undefined,
|
|
|
|
loading: false,
|
|
|
|
isAuthenticated: false,
|
|
|
|
isPasswordlessUser: false,
|
|
|
|
userName: null,
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}, []);
|
|
|
|
|
|
|
|
const changePassword = useCallback(
|
2022-05-27 20:08:03 +00:00
|
|
|
async (password: string) => {
|
|
|
|
const { tempPassword, ...session } = loadSession();
|
2022-01-06 01:19:03 +00:00
|
|
|
|
|
|
|
await client.setPassword(
|
|
|
|
{
|
|
|
|
type: "m.login.password",
|
|
|
|
identifier: {
|
|
|
|
type: "m.id.user",
|
2022-05-27 20:08:03 +00:00
|
|
|
user: session.user_id,
|
2022-01-06 01:19:03 +00:00
|
|
|
},
|
2022-05-27 20:08:03 +00:00
|
|
|
user: session.user_id,
|
2022-01-06 01:19:03 +00:00
|
|
|
password: tempPassword,
|
|
|
|
},
|
|
|
|
password
|
|
|
|
);
|
|
|
|
|
2022-05-27 20:08:03 +00:00
|
|
|
saveSession({ ...session, passwordlessUser: false });
|
2022-01-06 01:19:03 +00:00
|
|
|
|
|
|
|
setState({
|
|
|
|
client,
|
|
|
|
loading: false,
|
|
|
|
isAuthenticated: true,
|
|
|
|
isPasswordlessUser: false,
|
|
|
|
userName: client.getUserIdLocalpart(),
|
|
|
|
});
|
|
|
|
},
|
|
|
|
[client]
|
|
|
|
);
|
|
|
|
|
2022-02-15 20:46:58 +00:00
|
|
|
const setClient = useCallback(
|
2022-05-27 20:08:03 +00:00
|
|
|
(newClient: MatrixClient, session: Session) => {
|
2022-02-15 20:46:58 +00:00
|
|
|
if (client && client !== newClient) {
|
|
|
|
client.stopClient();
|
|
|
|
}
|
2022-01-06 01:19:03 +00:00
|
|
|
|
2022-02-15 20:46:58 +00:00
|
|
|
if (newClient) {
|
2022-05-27 20:08:03 +00:00
|
|
|
saveSession(session);
|
2022-01-06 01:19:03 +00:00
|
|
|
|
2022-02-15 20:46:58 +00:00
|
|
|
setState({
|
|
|
|
client: newClient,
|
|
|
|
loading: false,
|
|
|
|
isAuthenticated: true,
|
2022-05-27 20:08:03 +00:00
|
|
|
isPasswordlessUser: session.passwordlessUser,
|
2022-02-15 20:46:58 +00:00
|
|
|
userName: newClient.getUserIdLocalpart(),
|
|
|
|
});
|
|
|
|
} else {
|
2022-05-27 20:08:03 +00:00
|
|
|
clearSession();
|
2022-02-15 20:46:58 +00:00
|
|
|
|
|
|
|
setState({
|
|
|
|
client: undefined,
|
|
|
|
loading: false,
|
|
|
|
isAuthenticated: false,
|
|
|
|
isPasswordlessUser: false,
|
|
|
|
userName: null,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
},
|
|
|
|
[client]
|
|
|
|
);
|
2022-01-06 01:19:03 +00:00
|
|
|
|
|
|
|
const logout = useCallback(() => {
|
2022-05-27 20:08:03 +00:00
|
|
|
clearSession();
|
|
|
|
history.push("/");
|
2022-01-06 01:19:03 +00:00
|
|
|
}, [history]);
|
|
|
|
|
2022-02-11 01:10:36 +00:00
|
|
|
useEffect(() => {
|
2022-02-19 00:23:37 +00:00
|
|
|
if (client) {
|
2022-02-11 01:10:36 +00:00
|
|
|
const loadTime = Date.now();
|
|
|
|
|
2022-05-27 20:08:03 +00:00
|
|
|
const onToDeviceEvent = (event: MatrixEvent) => {
|
|
|
|
if (event.getType() !== "org.matrix.call_duplicate_session") return;
|
2022-02-19 00:23:37 +00:00
|
|
|
|
|
|
|
const content = event.getContent();
|
|
|
|
|
2022-05-27 20:08:03 +00:00
|
|
|
if (content.session_id === client.getSessionId()) return;
|
2022-02-19 00:23:37 +00:00
|
|
|
|
|
|
|
if (content.timestamp > loadTime) {
|
2022-05-27 20:08:03 +00:00
|
|
|
client?.stopClient();
|
2022-02-11 01:10:36 +00:00
|
|
|
|
|
|
|
setState((prev) => ({
|
|
|
|
...prev,
|
|
|
|
error: new Error(
|
|
|
|
"This application has been opened in another tab."
|
|
|
|
),
|
|
|
|
}));
|
|
|
|
}
|
2022-02-19 00:23:37 +00:00
|
|
|
};
|
|
|
|
|
2022-05-27 20:08:03 +00:00
|
|
|
client.on(ClientEvent.ToDeviceEvent, onToDeviceEvent);
|
2022-02-11 01:10:36 +00:00
|
|
|
|
2022-02-19 00:23:37 +00:00
|
|
|
client.sendToDevice("org.matrix.call_duplicate_session", {
|
|
|
|
[client.getUserId()]: {
|
|
|
|
"*": { session_id: client.getSessionId(), timestamp: loadTime },
|
|
|
|
},
|
|
|
|
});
|
2022-02-11 01:10:36 +00:00
|
|
|
|
|
|
|
return () => {
|
2022-05-27 20:08:03 +00:00
|
|
|
client?.removeListener(ClientEvent.ToDeviceEvent, onToDeviceEvent);
|
2022-02-11 01:10:36 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
}, [client]);
|
|
|
|
|
2022-05-27 20:08:03 +00:00
|
|
|
const context = useMemo<ClientState>(
|
2022-01-06 01:19:03 +00:00
|
|
|
() => ({
|
|
|
|
loading,
|
|
|
|
isAuthenticated,
|
|
|
|
isPasswordlessUser,
|
|
|
|
client,
|
|
|
|
changePassword,
|
|
|
|
logout,
|
|
|
|
userName,
|
|
|
|
setClient,
|
|
|
|
}),
|
|
|
|
[
|
|
|
|
loading,
|
|
|
|
isAuthenticated,
|
|
|
|
isPasswordlessUser,
|
|
|
|
client,
|
|
|
|
changePassword,
|
|
|
|
logout,
|
|
|
|
userName,
|
|
|
|
setClient,
|
|
|
|
]
|
|
|
|
);
|
|
|
|
|
2022-02-15 20:46:58 +00:00
|
|
|
useEffect(() => {
|
|
|
|
window.matrixclient = client;
|
|
|
|
}, [client]);
|
|
|
|
|
2022-02-11 01:10:36 +00:00
|
|
|
if (error) {
|
|
|
|
return <ErrorView error={error} />;
|
|
|
|
}
|
|
|
|
|
2022-01-06 01:19:03 +00:00
|
|
|
return (
|
|
|
|
<ClientContext.Provider value={context}>{children}</ClientContext.Provider>
|
|
|
|
);
|
2022-05-27 20:08:03 +00:00
|
|
|
};
|
2022-01-06 01:19:03 +00:00
|
|
|
|
2022-05-27 20:08:03 +00:00
|
|
|
export const useClient = () => useContext(ClientContext);
|