diff options
author | Lewis Crichton <lewi@lewisakura.moe> | 2023-07-08 02:36:59 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-07-08 03:36:59 +0200 |
commit | e52ae624416eae9a0098909c4f1cc5981454472d (patch) | |
tree | b1de12b895deae9b9e9cd5d015f663d9cd725157 /src | |
parent | 7cd1d4c60f417f47a7badbae698f9f3ed4cfbf41 (diff) | |
download | Vencord-e52ae624416eae9a0098909c4f1cc5981454472d.tar.gz Vencord-e52ae624416eae9a0098909c4f1cc5981454472d.tar.bz2 Vencord-e52ae624416eae9a0098909c4f1cc5981454472d.zip |
feat(cloud): support multiple user accounts (#1382)
Co-authored-by: V <vendicated@riseup.net>
Diffstat (limited to 'src')
-rw-r--r-- | src/utils/cloud.tsx | 33 |
1 files changed, 28 insertions, 5 deletions
diff --git a/src/utils/cloud.tsx b/src/utils/cloud.tsx index db48aa7..0293062 100644 --- a/src/utils/cloud.tsx +++ b/src/utils/cloud.tsx @@ -28,15 +28,39 @@ import { openModal } from "./modal"; export const cloudLogger = new Logger("Cloud", "#39b7e0"); export const getCloudUrl = () => new URL(Settings.cloud.url); +const cloudUrlOrigin = () => getCloudUrl().origin; +const getUserId = () => { + const id = UserStore.getCurrentUser()?.id; + if (!id) throw new Error("User not yet logged in"); + return id; +}; + export async function getAuthorization() { const secrets = await DataStore.get<Record<string, string>>("Vencord_cloudSecret") ?? {}; - return secrets[getCloudUrl().origin]; + + const origin = cloudUrlOrigin(); + + // we need to migrate from the old format here + if (secrets[origin]) { + await DataStore.update<Record<string, string>>("Vencord_cloudSecret", secrets => { + secrets ??= {}; + // use the current user ID + secrets[`${origin}:${getUserId()}`] = secrets[origin]; + delete secrets[origin]; + return secrets; + }); + + // since this doesn't update the original object, we'll early return the existing authorization + return secrets[origin]; + } + + return secrets[`${origin}:${getUserId()}`]; } async function setAuthorization(secret: string) { await DataStore.update<Record<string, string>>("Vencord_cloudSecret", secrets => { secrets ??= {}; - secrets[getCloudUrl().origin] = secret; + secrets[`${cloudUrlOrigin()}:${getUserId()}`] = secret; return secrets; }); } @@ -44,7 +68,7 @@ async function setAuthorization(secret: string) { export async function deauthorizeCloud() { await DataStore.update<Record<string, string>>("Vencord_cloudSecret", secrets => { secrets ??= {}; - delete secrets[getCloudUrl().origin]; + delete secrets[`${cloudUrlOrigin()}:${getUserId()}`]; return secrets; }); } @@ -117,8 +141,7 @@ export async function authorizeCloud() { } export async function getCloudAuth() { - const userId = UserStore.getCurrentUser().id; const secret = await getAuthorization(); - return window.btoa(`${secret}:${userId}`); + return window.btoa(`${secret}:${getUserId()}`); } |