From 3e340d5906c31aa0697ac74b7aac3e163e927da1 Mon Sep 17 00:00:00 2001 From: maheichyk Date: Tue, 7 Mar 2023 18:24:44 +0300 Subject: [PATCH] Fixes user authentication when registering via the module API (#10257) * Fixes authentication when user is registered via module API * Remove "on_logging_in" action --------- Signed-off-by: Mikhail Aheichyk Co-authored-by: Mikhail Aheichyk --- src/Lifecycle.ts | 11 +---------- src/modules/ProxiedModuleApi.ts | 18 ++++++++++++++++++ test/modules/ProxiedModuleApi-test.ts | 26 ++++++++++++++++++++++++++ 3 files changed, 45 insertions(+), 10 deletions(-) diff --git a/src/Lifecycle.ts b/src/Lifecycle.ts index 590b7eb180..5122b0a632 100644 --- a/src/Lifecycle.ts +++ b/src/Lifecycle.ts @@ -568,7 +568,7 @@ export async function hydrateSession(credentials: IMatrixClientCreds): Promise; + private overrideLoginResolve?: () => void; + + public constructor() { + dispatcher.register(this.onAction); + } + + private onAction = (payload: ActionPayload): void => { + if (payload.action === Action.OnLoggedIn) { + this.overrideLoginResolve?.(); + } + }; + /** * All custom translations used by the associated module. */ @@ -155,6 +168,11 @@ export class ProxiedModuleApi implements ModuleApi { }, true, ); // require to be sync to match inherited interface behaviour + + // wait for login to complete + await new Promise((resolve) => { + this.overrideLoginResolve = resolve; + }); } /** diff --git a/test/modules/ProxiedModuleApi-test.ts b/test/modules/ProxiedModuleApi-test.ts index 4a29e37453..881e98c9df 100644 --- a/test/modules/ProxiedModuleApi-test.ts +++ b/test/modules/ProxiedModuleApi-test.ts @@ -15,12 +15,15 @@ limitations under the License. */ import { TranslationStringsObject } from "@matrix-org/react-sdk-module-api/lib/types/translations"; +import { AccountAuthInfo } from "@matrix-org/react-sdk-module-api/lib/types/AccountAuthInfo"; import { ProxiedModuleApi } from "../../src/modules/ProxiedModuleApi"; import { stubClient } from "../test-utils"; import { setLanguage } from "../../src/languageHandler"; import { ModuleRunner } from "../../src/modules/ModuleRunner"; import { registerMockModule } from "./MockModule"; +import defaultDispatcher from "../../src/dispatcher/dispatcher"; +import { Action } from "../../src/dispatcher/actions"; describe("ProxiedApiModule", () => { afterEach(() => { @@ -44,6 +47,29 @@ describe("ProxiedApiModule", () => { expect(api.translations).toBe(translations); }); + it("should overwriteAccountAuth", async () => { + const dispatchSpy = jest.spyOn(defaultDispatcher, "dispatch"); + + const api = new ProxiedModuleApi(); + const accountInfo = {} as unknown as AccountAuthInfo; + const promise = api.overwriteAccountAuth(accountInfo); + + expect(dispatchSpy).toHaveBeenCalledWith( + expect.objectContaining({ + action: Action.OverwriteLogin, + credentials: { + ...accountInfo, + guest: false, + }, + }), + expect.anything(), + ); + + defaultDispatcher.fire(Action.OnLoggedIn); + + await expect(promise).resolves.toBeUndefined(); + }); + describe("integration", () => { it("should translate strings using translation system", async () => { // Test setup