Keep device language when it has been previosuly set, after a successful delegated authentication flow that clears localStorage (#11902)

* Do not remove the language when clearing local storage

Signed-off-by: Milton Moura <miltonmoura@gmail.com>

* Revised comment on getting the defined language from settings

Signed-off-by: Milton Moura <miltonmoura@gmail.com>

* Explicitly checking for language set at the device level

Signed-off-by: Milton Moura <miltonmoura@gmail.com>

* Add test that checks if device language setting is kept after a successfull delegated authentication flow

Signed-off-by: Milton Moura <miltonmoura@gmail.com>

* Adds test for unhappy path and adjusts to use the SettingsStore instead of going directly to localStorage

Signed-off-by: Milton Moura <miltonmoura@gmail.com>

* Removing unnecessary variable after test refactor

Signed-off-by: Milton Moura <miltonmoura@gmail.com>

---------

Signed-off-by: Milton Moura <miltonmoura@gmail.com>
This commit is contained in:
Milton Moura 2023-11-24 14:51:28 -01:00 committed by GitHub
parent ba3395e833
commit 76b7aa2d33
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 291 additions and 253 deletions

View file

@ -40,6 +40,7 @@ import PlatformPeg from "./PlatformPeg";
import { sendLoginRequest } from "./Login";
import * as StorageManager from "./utils/StorageManager";
import SettingsStore from "./settings/SettingsStore";
import { SettingLevel } from "./settings/SettingLevel";
import ToastStore from "./stores/ToastStore";
import { IntegrationManagers } from "./integrations/IntegrationManagers";
import { Mjolnir } from "./mjolnir/Mjolnir";
@ -1105,6 +1106,9 @@ export async function onLoggedOut(): Promise<void> {
*/
async function clearStorage(opts?: { deleteEverything?: boolean }): Promise<void> {
if (window.localStorage) {
// get the currently defined device language, if set, so we can restore it later
const language = SettingsStore.getValueAt(SettingLevel.DEVICE, "language", null, true, true);
// try to save any 3pid invites from being obliterated and registration time
const pendingInvites = ThreepidInviteStore.instance.getWireInvites();
const registrationTime = window.localStorage.getItem("mx_registration_time");
@ -1118,8 +1122,12 @@ async function clearStorage(opts?: { deleteEverything?: boolean }): Promise<void
logger.error("idbDelete failed for account:mx_access_token", e);
}
// now restore those invites and registration time
// now restore those invites, registration time and previously set device language
if (!opts?.deleteEverything) {
if (language) {
await SettingsStore.setValue("language", null, SettingLevel.DEVICE, language);
}
pendingInvites.forEach(({ roomId, ...invite }) => {
ThreepidInviteStore.instance.storeInvite(roomId, invite);
});