Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

fix(dashboard): carry the stored role over to the per-tab store

Yudhi Armyndharisf51ec18e76b6
diff --git a/dashboard/src/components/RoleProvider.test.ts b/dashboard/src/components/RoleProvider.test.ts
new file mode 100644
index 000000000..5f1fec0f7
--- /dev/null
+++ b/dashboard/src/components/RoleProvider.test.ts
@@ -0,0 +1,63 @@
+// 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.
+import '../test-helpers/register-hooks.ts';
+import { test, before, beforeEach, afterEach } from 'node:test';
+import assert from 'node:assert/strict';
+import { createElement } from 'react';
+
+let rtl: typeof import('@testing-library/react');
+let RoleProvider: (typeof import('./RoleProvider.tsx'))['RoleProvider'];
+let useRole: (typeof import('../hooks/useRole.tsx'))['useRole'];
+
+before(async () => {
+ const { installJsdomGlobals } = await import('../test-helpers/jsdom.ts');
+ await installJsdomGlobals();
+ rtl = await import('@testing-library/react');
+ ({ RoleProvider } = await import('./RoleProvider.tsx'));
+ ({ useRole } = await import('../hooks/useRole.tsx'));
+});
+
+beforeEach(() => {
+ localStorage.clear();
+ sessionStorage.clear();
+});
+
+afterEach(() => rtl.cleanup());
+
+/** The role the first render sees: a later correction would still flash the wrong UI. */
+function firstRenderedRole(): string | null {
+ const seen: (string | null)[] = [];
+ function Probe() {
+ seen.push(useRole().role);
+ return null;
+ }
+ rtl.render(createElement(RoleProvider, null, createElement(Probe)));
+ return seen[0];
+}
+
+test('a signed-in tab adopts the role an older build left in localStorage', () => {
+ sessionStorage.setItem('openwa_api_key', 'key');
+ localStorage.setItem('openwa_user_role', 'admin');
+
+ assert.equal(firstRenderedRole(), 'admin');
+ assert.equal(sessionStorage.getItem('openwa_user_role'), 'admin');
+ assert.equal(localStorage.getItem('openwa_user_role'), null);
+});
+
+test("the tab's own role wins, and the shared copy is still dropped", () => {
+ 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);
+});
+
+test('a tab without an API key does not take the shared role', () => {
+ localStorage.setItem('openwa_user_role', 'admin');
+
+ assert.equal(firstRenderedRole(), null);
+ assert.equal(sessionStorage.getItem('openwa_user_role'), null);
+ assert.equal(localStorage.getItem('openwa_user_role'), null);
+});
diff --git a/dashboard/src/components/RoleProvider.tsx b/dashboard/src/components/RoleProvider.tsx
index d409a6818..85567db44 100644
--- a/dashboard/src/components/RoleProvider.tsx
+++ b/dashboard/src/components/RoleProvider.tsx
@@ -6,6 +6,14 @@ export function RoleProvider({ children }: { children: ReactNode }) {
// Per tab, next to the API key it was validated for: a role outliving its key (or shared with
// 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.
+ 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);
+ }
const saved = sessionStorage.getItem('openwa_user_role');
return (saved as UserRole) || null;
});