diff --git a/src/utils/cloud.tsx b/src/utils/cloud.tsx index db48aa7f8..029306227 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>("Vencord_cloudSecret") ?? {}; - return secrets[getCloudUrl().origin]; + + const origin = cloudUrlOrigin(); + + // we need to migrate from the old format here + if (secrets[origin]) { + await DataStore.update>("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>("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>("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()}`); }