Fix (rust crypto): Adjust login procedures to account for rust crypto behaviour. (#2603)
* Fix for missing client store (caused by: #2587) * Fix interactive login with authenticated guest user. Fix clearing storage before logging in a new account.
This commit is contained in:
@@ -379,19 +379,15 @@ async function loadClient(): Promise<InitResult | null> {
|
|||||||
} catch (err) {
|
} catch (err) {
|
||||||
if (err instanceof MatrixError && err.errcode === "M_UNKNOWN_TOKEN") {
|
if (err instanceof MatrixError && err.errcode === "M_UNKNOWN_TOKEN") {
|
||||||
// We can't use this session anymore, so let's log it out
|
// We can't use this session anymore, so let's log it out
|
||||||
try {
|
logger.log(
|
||||||
const client = await initClient(initClientParams, false); // Don't need the crypto store just to log out)
|
"The session from local store is invalid; continuing without a client",
|
||||||
await client.logout(true);
|
|
||||||
} catch (err) {
|
|
||||||
logger.warn(
|
|
||||||
"The previous session was unable to login, and we couldn't log it out: " +
|
|
||||||
err,
|
|
||||||
);
|
);
|
||||||
}
|
clearSession();
|
||||||
|
// returning null = "no client` pls register" (undefined = "loading" which is the current value when reaching this line)
|
||||||
|
return null;
|
||||||
}
|
}
|
||||||
throw err;
|
throw err;
|
||||||
}
|
}
|
||||||
/* eslint-enable camelcase */
|
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
clearSession();
|
clearSession();
|
||||||
throw err;
|
throw err;
|
||||||
|
|||||||
@@ -32,8 +32,8 @@ export const LoginPage: FC = () => {
|
|||||||
const { t } = useTranslation();
|
const { t } = useTranslation();
|
||||||
usePageTitle(t("login_title"));
|
usePageTitle(t("login_title"));
|
||||||
|
|
||||||
const { setClient } = useClient();
|
const { client, setClient } = useClient();
|
||||||
const login = useInteractiveLogin();
|
const login = useInteractiveLogin(client);
|
||||||
const homeserver = Config.defaultHomeserverUrl(); // TODO: Make this configurable
|
const homeserver = Config.defaultHomeserverUrl(); // TODO: Make this configurable
|
||||||
const usernameRef = useRef<HTMLInputElement>(null);
|
const usernameRef = useRef<HTMLInputElement>(null);
|
||||||
const passwordRef = useRef<HTMLInputElement>(null);
|
const passwordRef = useRef<HTMLInputElement>(null);
|
||||||
|
|||||||
@@ -24,8 +24,15 @@ import {
|
|||||||
|
|
||||||
import { initClient } from "../utils/matrix";
|
import { initClient } from "../utils/matrix";
|
||||||
import { Session } from "../ClientContext";
|
import { Session } from "../ClientContext";
|
||||||
|
/**
|
||||||
export function useInteractiveLogin(): (
|
* This provides the login method to login using user credentials.
|
||||||
|
* @param oldClient If there is an already authenticated client it should be passed to this hook
|
||||||
|
* this allows the interactive login to sign out the client before logging in.
|
||||||
|
* @returns A async method that can be called/awaited to log in with the provided credentials.
|
||||||
|
*/
|
||||||
|
export function useInteractiveLogin(
|
||||||
|
oldClient?: MatrixClient,
|
||||||
|
): (
|
||||||
homeserver: string,
|
homeserver: string,
|
||||||
username: string,
|
username: string,
|
||||||
password: string,
|
password: string,
|
||||||
@@ -36,7 +43,8 @@ export function useInteractiveLogin(): (
|
|||||||
username: string,
|
username: string,
|
||||||
password: string,
|
password: string,
|
||||||
) => Promise<[MatrixClient, Session]>
|
) => Promise<[MatrixClient, Session]>
|
||||||
>(async (homeserver: string, username: string, password: string) => {
|
>(
|
||||||
|
async (homeserver: string, username: string, password: string) => {
|
||||||
const authClient = createClient({ baseUrl: homeserver });
|
const authClient = createClient({ baseUrl: homeserver });
|
||||||
|
|
||||||
const interactiveAuth = new InteractiveAuth({
|
const interactiveAuth = new InteractiveAuth({
|
||||||
@@ -67,6 +75,8 @@ export function useInteractiveLogin(): (
|
|||||||
passwordlessUser: false,
|
passwordlessUser: false,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// To not confuse the rust crypto sessions we need to logout the old client before initializing the new one.
|
||||||
|
await oldClient?.logout(true);
|
||||||
const client = await initClient(
|
const client = await initClient(
|
||||||
{
|
{
|
||||||
baseUrl: homeserver,
|
baseUrl: homeserver,
|
||||||
@@ -78,5 +88,7 @@ export function useInteractiveLogin(): (
|
|||||||
);
|
);
|
||||||
/* eslint-enable camelcase */
|
/* eslint-enable camelcase */
|
||||||
return [client, session];
|
return [client, session];
|
||||||
}, []);
|
},
|
||||||
|
[oldClient],
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -71,10 +71,12 @@ async function waitForSync(client: MatrixClient): Promise<void> {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Initialises and returns a new standalone Matrix Client.
|
* Initialises and returns a new standalone Matrix Client.
|
||||||
* If false is passed for the 'restore' parameter, corresponding crypto
|
* This can only be called safely if no other client is running
|
||||||
* data is cleared before the client initialization.
|
* otherwise rust crypto will throw since it is not ready to initialize a new session.
|
||||||
|
* If another client is running make sure `.logout()` is called before executing this function.
|
||||||
* @param clientOptions Object of options passed through to the client
|
* @param clientOptions Object of options passed through to the client
|
||||||
* @param restore Whether the session is being restored from storage
|
* @param restore If the rust crypto should be reset before the cient initialization or
|
||||||
|
* if the initialization should try to restore the crypto state from the indexDB.
|
||||||
* @returns The MatrixClient instance
|
* @returns The MatrixClient instance
|
||||||
*/
|
*/
|
||||||
export async function initClient(
|
export async function initClient(
|
||||||
@@ -130,20 +132,17 @@ export async function initClient(
|
|||||||
fallbackICEServerAllowed: fallbackICEServerAllowed,
|
fallbackICEServerAllowed: fallbackICEServerAllowed,
|
||||||
});
|
});
|
||||||
|
|
||||||
// In case of registering a new matrix account caused by broken store state. This is particularly needed for:
|
// In case of logging in a new matrix account but there is still crypto local store. This is needed for:
|
||||||
// - We lost the auth tokens and cannot restore the client resulting in registering a new user.
|
// - We lost the auth tokens and cannot restore the client resulting in registering a new user.
|
||||||
// - Need to make sure any possible leftover crypto store gets cleared.
|
// - We start the sign in flow but are registered with a guest user. (It should additionally log out the guest before)
|
||||||
// - A new account is created because of missing LocalStorage: "matrix-auth-store", but the crypto IndexDB is still available.
|
// - A new account is created because of missing LocalStorage: "matrix-auth-store", but the crypto IndexDB is still available.
|
||||||
// This would result in conflicting crypto store userId vs matrixClient userId. Caused by EC 0.6.1
|
|
||||||
if (!restore) {
|
if (!restore) {
|
||||||
client.clearStores();
|
await client.clearStores();
|
||||||
}
|
}
|
||||||
|
|
||||||
// Start client store.
|
// Start client store.
|
||||||
// Note: The `client.store` is used to store things like sync results. It's independent of
|
// Note: The `client.store` is used to store things like sync results. It's independent of
|
||||||
// the cryptostore, and uses a separate indexeddb database.
|
// the cryptostore, and uses a separate indexeddb database.
|
||||||
|
|
||||||
// start the client store (totally independent to the crypto store)
|
|
||||||
try {
|
try {
|
||||||
await client.store.startup();
|
await client.store.startup();
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
@@ -156,7 +155,14 @@ export async function initClient(
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Also creates and starts any crypto related stores.
|
// Also creates and starts any crypto related stores.
|
||||||
|
try {
|
||||||
await client.initRustCrypto();
|
await client.initRustCrypto();
|
||||||
|
} catch (err) {
|
||||||
|
logger.warn(
|
||||||
|
err,
|
||||||
|
"Make sure to clear client stores before initializing the rust crypto.",
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
client.setGlobalErrorOnUnknownDevices(false);
|
client.setGlobalErrorOnUnknownDevices(false);
|
||||||
// Once startClient is called, syncs are run asynchronously.
|
// Once startClient is called, syncs are run asynchronously.
|
||||||
|
|||||||
Reference in New Issue
Block a user