Merge pull request #518 from robintown/logout-lost-sessions

Log out lost sessions
This commit is contained in:
Robin 2022-08-13 09:38:40 -04:00 committed by GitHub
commit 8f95da4b07
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -33,6 +33,7 @@ import {
initClient,
initMatroskaClient,
defaultHomeserver,
CryptoStoreIntegrityError,
} from "./matrix-utils";
declare global {
@ -115,14 +116,17 @@ export const ClientProvider: FC<Props> = ({ children }) => {
// We're running as a standalone application
try {
const session = loadSession();
if (!session) return { client: undefined, isPasswordlessUser: false };
logger.log("Using a standalone client");
if (session) {
/* eslint-disable camelcase */
const { user_id, device_id, access_token, passwordlessUser } =
session;
logger.log("Using a standalone client");
const client = await initClient(
try {
return {
client: await initClient(
{
baseUrl: defaultHomeserver,
accessToken: access_token,
@ -130,13 +134,33 @@ export const ClientProvider: FC<Props> = ({ children }) => {
deviceId: device_id,
},
true
),
isPasswordlessUser: 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(
{
baseUrl: defaultHomeserver,
accessToken: access_token,
userId: user_id,
deviceId: device_id,
},
false // Don't need the crypto store just to log out
);
await client.logout(undefined, true);
} catch (err_) {
logger.warn(
"The previous session was lost, and we couldn't log it out, " +
"either"
);
/* eslint-enable camelcase */
return { client, isPasswordlessUser: passwordlessUser };
}
return { client: undefined, isPasswordlessUser: false };
}
throw err;
}
/* eslint-enable camelcase */
} catch (err) {
clearSession();
throw err;