Free, Open Source, Self-Hosted WhatsApp API Gateway
TypeScript apibotgatewayself-hostedwhatsappwhatsapp-apiwhatsapp-automationwhatsapp-botwhatsapp-gatewaywhatsapp-gateway-apiwhatsapp-webwhatsapp-web-api
fix(dashboard): keep the shared role for every tab signed in pre-upgrade
Yudhi Armyndharise21b10cc3d2f
diff --git a/dashboard/src/components/RoleProvider.test.ts b/dashboard/src/components/RoleProvider.test.tsindex 5f1fec0f7..74877e51a 100644--- a/dashboard/src/components/RoleProvider.test.ts+++ b/dashboard/src/components/RoleProvider.test.ts@@ -1,6 +1,7 @@// The role is kept per tab in sessionStorage, next to the API key. Older builds kept it in-// localStorage, so the first load after an upgrade must adopt that copy once, or a signed-in tab-// starts with no role until /auth/validate answers.+// localStorage, so the first load after an upgrade must adopt that copy, or a signed-in tab starts+// with no role until /auth/validate answers. Every tab signed in before the upgrade needs it, so+// the first tab to load must not consume it.import '../test-helpers/register-hooks.ts';import { test, before, beforeEach, afterEach } from 'node:test';import assert from 'node:assert/strict';@@ -42,16 +43,16 @@ test('a signed-in tab adopts the role an older build left in localStorage', () =assert.equal(firstRenderedRole(), 'admin');assert.equal(sessionStorage.getItem('openwa_user_role'), 'admin');- assert.equal(localStorage.getItem('openwa_user_role'), null);+ assert.equal(localStorage.getItem('openwa_user_role'), 'admin', 'the other signed-in tabs lost the shared copy');});-test("the tab's own role wins, and the shared copy is still dropped", () => {+test("the tab's own role wins over the shared copy", () => {sessionStorage.setItem('openwa_api_key', 'key');sessionStorage.setItem('openwa_user_role', 'viewer');localStorage.setItem('openwa_user_role', 'admin');assert.equal(firstRenderedRole(), 'viewer');- assert.equal(localStorage.getItem('openwa_user_role'), null);+ assert.equal(sessionStorage.getItem('openwa_user_role'), 'viewer');});test('a tab without an API key does not take the shared role', () => {@@ -59,5 +60,17 @@ test('a tab without an API key does not take the shared role', () => {assert.equal(firstRenderedRole(), null);assert.equal(sessionStorage.getItem('openwa_user_role'), null);- assert.equal(localStorage.getItem('openwa_user_role'), null);+ // A key-less tab (the sign-in page) must not consume the copy the signed-in tabs still need.+ assert.equal(localStorage.getItem('openwa_user_role'), 'admin');+});++test('every signed-in tab adopts the shared role, not only the first to reload', () => {+ localStorage.setItem('openwa_user_role', 'admin');+ // Two tabs share localStorage; each has its own sessionStorage holding only its key.+ for (const tab of ['first', 'second']) {+ sessionStorage.clear();+ sessionStorage.setItem('openwa_api_key', `${tab}-key`);+ assert.equal(firstRenderedRole(), 'admin', `the ${tab} tab lost its role`);+ rtl.cleanup();+ }});diff --git a/dashboard/src/components/RoleProvider.tsx b/dashboard/src/components/RoleProvider.tsxindex 85567db44..d42771229 100644--- a/dashboard/src/components/RoleProvider.tsx+++ b/dashboard/src/components/RoleProvider.tsx@@ -7,10 +7,10 @@ export function RoleProvider({ children }: { children: ReactNode }) {// another tab signed in with a different key) would gate the UI for the wrong actor.const [role, setRoleState] = useState<UserRole | null>(() => {// Older builds kept the role in localStorage, shared by every tab. A tab that already holds its- // key adopts that copy once, so the first reload after an upgrade does not drop the role until- // /auth/validate answers; the shared copy goes either way.+ // key but no role of its own adopts that copy, so its first reload after an upgrade does not drop+ // the role until /auth/validate answers. The copy is left in place for the other tabs signed in+ // before the upgrade; every sign-in since stores a role with its key, so no newer tab adopts it.const legacy = localStorage.getItem('openwa_user_role');- localStorage.removeItem('openwa_user_role');if (legacy && !sessionStorage.getItem('openwa_user_role') && sessionStorage.getItem('openwa_api_key')) {sessionStorage.setItem('openwa_user_role', legacy);}