Skip to content

OpenPanel is an open-source web and product analytics platform, an open-source alternative to Mixpanel with optional self-hosting.

fix(dashboard): updating a share keeps its password

Carl-Gerhard Lindesvärd78afebf5a17a
diff --git a/apps/start/src/modals/share-dashboard-modal.tsx b/apps/start/src/modals/share-dashboard-modal.tsx
index 6dd629c9e..ccf7c320a 100644
--- a/apps/start/src/modals/share-dashboard-modal.tsx
+++ b/apps/start/src/modals/share-dashboard-modal.tsx
@@ -15,6 +15,11 @@ import { Input } from '@/components/ui/input';
import { Tooltiper } from '@/components/ui/tooltip';
import { useAppParams } from '@/hooks/use-app-params';
import { handleError, useTRPC } from '@/integrations/trpc/react';
+import {
+ getSharePasswordFieldValue,
+ getSharePasswordToSubmit,
+ STORED_PASSWORD_PLACEHOLDER,
+} from '@/utils/share-password';
const validator = zShareDashboard;
@@ -48,13 +53,16 @@ export default function ShareDashboardModal({
const { register, handleSubmit, watch } = useForm<IForm>({
resolver: zodResolver(validator),
- defaultValues: {
+ // `values` rather than `defaultValues`: the share settings arrive after
+ // the first render, and the field must then show the stored password.
+ values: {
public: true,
- password: existingShare?.hasPassword ? '••••••••' : '',
+ password: getSharePasswordFieldValue(existingShare?.hasPassword),
projectId,
organizationId,
dashboardId,
},
+ resetOptions: { keepDirtyValues: true },
});
const password = watch('password');
@@ -165,9 +173,7 @@ export default function ShareDashboardModal({
onSubmit={handleSubmit((values) => {
mutation.mutate({
...values,
- // Only send password if it's not the placeholder
- password:
- values.password === '••••••••' ? null : values.password || null,
+ password: getSharePasswordToSubmit(values.password),
});
})}
>
@@ -175,7 +181,7 @@ export default function ShareDashboardModal({
{...register('password')}
placeholder="Enter your password (optional)"
size="large"
- type={password === '••••••••' ? 'text' : 'password'}
+ type={password === STORED_PASSWORD_PLACEHOLDER ? 'text' : 'password'}
/>
<ButtonContainer>
<Button onClick={() => popModal()} type="button" variant="outline">
diff --git a/apps/start/src/modals/share-overview-modal.tsx b/apps/start/src/modals/share-overview-modal.tsx
index ddecef8c0..bd20b8e92 100644
--- a/apps/start/src/modals/share-overview-modal.tsx
+++ b/apps/start/src/modals/share-overview-modal.tsx
@@ -15,6 +15,11 @@ import { Input } from '@/components/ui/input';
import { Tooltiper } from '@/components/ui/tooltip';
import { useAppParams } from '@/hooks/use-app-params';
import { handleError, useTRPC } from '@/integrations/trpc/react';
+import {
+ getSharePasswordFieldValue,
+ getSharePasswordToSubmit,
+ STORED_PASSWORD_PLACEHOLDER,
+} from '@/utils/share-password';
const validator = zShareOverview;
@@ -43,12 +48,15 @@ export default function ShareOverviewModal() {
const { register, handleSubmit, watch } = useForm<IForm>({
resolver: zodResolver(validator),
- defaultValues: {
+ // `values` rather than `defaultValues`: the share settings arrive after
+ // the first render, and the field must then show the stored password.
+ values: {
public: true,
- password: existingShare?.hasPassword ? '••••••••' : '',
+ password: getSharePasswordFieldValue(existingShare?.hasPassword),
projectId,
organizationId,
},
+ resetOptions: { keepDirtyValues: true },
});
const password = watch('password');
@@ -156,9 +164,7 @@ export default function ShareOverviewModal() {
onSubmit={handleSubmit((values) => {
mutation.mutate({
...values,
- // Only send password if it's not the placeholder
- password:
- values.password === '••••••••' ? null : values.password || null,
+ password: getSharePasswordToSubmit(values.password),
});
})}
>
@@ -166,7 +172,7 @@ export default function ShareOverviewModal() {
{...register('password')}
placeholder="Enter your password (optional)"
size="large"
- type={password === '••••••••' ? 'text' : 'password'}
+ type={password === STORED_PASSWORD_PLACEHOLDER ? 'text' : 'password'}
/>
<ButtonContainer>
<Button onClick={() => popModal()} type="button" variant="outline">
diff --git a/apps/start/src/modals/share-report-modal.tsx b/apps/start/src/modals/share-report-modal.tsx
index 0798b1433..aad7008b5 100644
--- a/apps/start/src/modals/share-report-modal.tsx
+++ b/apps/start/src/modals/share-report-modal.tsx
@@ -15,6 +15,11 @@ import { Input } from '@/components/ui/input';
import { Tooltiper } from '@/components/ui/tooltip';
import { useAppParams } from '@/hooks/use-app-params';
import { handleError, useTRPC } from '@/integrations/trpc/react';
+import {
+ getSharePasswordFieldValue,
+ getSharePasswordToSubmit,
+ STORED_PASSWORD_PLACEHOLDER,
+} from '@/utils/share-password';
const validator = zShareReport;
@@ -44,13 +49,16 @@ export default function ShareReportModal({ reportId }: { reportId: string }) {
const { register, handleSubmit, watch } = useForm<IForm>({
resolver: zodResolver(validator),
- defaultValues: {
+ // `values` rather than `defaultValues`: the share settings arrive after
+ // the first render, and the field must then show the stored password.
+ values: {
public: true,
- password: existingShare?.hasPassword ? '••••••••' : '',
+ password: getSharePasswordFieldValue(existingShare?.hasPassword),
projectId,
organizationId,
reportId,
},
+ resetOptions: { keepDirtyValues: true },
});
const password = watch('password');
@@ -157,9 +165,7 @@ export default function ShareReportModal({ reportId }: { reportId: string }) {
onSubmit={handleSubmit((values) => {
mutation.mutate({
...values,
- // Only send password if it's not the placeholder
- password:
- values.password === '••••••••' ? null : values.password || null,
+ password: getSharePasswordToSubmit(values.password),
});
})}
>
@@ -167,7 +173,7 @@ export default function ShareReportModal({ reportId }: { reportId: string }) {
{...register('password')}
placeholder="Enter your password (optional)"
size="large"
- type={password === '••••••••' ? 'text' : 'password'}
+ type={password === STORED_PASSWORD_PLACEHOLDER ? 'text' : 'password'}
/>
<ButtonContainer>
<Button onClick={() => popModal()} type="button" variant="outline">
diff --git a/apps/start/src/utils/share-password.test.ts b/apps/start/src/utils/share-password.test.ts
new file mode 100644
index 000000000..213b55b9d
--- /dev/null
+++ b/apps/start/src/utils/share-password.test.ts
@@ -0,0 +1,30 @@
+import { describe, expect, it } from 'vitest';
+import {
+ getSharePasswordFieldValue,
+ getSharePasswordToSubmit,
+ STORED_PASSWORD_PLACEHOLDER,
+} from './share-password';
+
+describe('getSharePasswordToSubmit', () => {
+ it('omits the password when the placeholder is untouched', () => {
+ expect(getSharePasswordToSubmit(STORED_PASSWORD_PLACEHOLDER)).toBe(
+ undefined
+ );
+ });
+
+ it('removes the password when the field was cleared', () => {
+ expect(getSharePasswordToSubmit('')).toBe(null);
+ });
+
+ it('sends a typed password', () => {
+ expect(getSharePasswordToSubmit('hunter2')).toBe('hunter2');
+ });
+});
+
+describe('getSharePasswordFieldValue', () => {
+ it('shows the placeholder only for a share with a password', () => {
+ expect(getSharePasswordFieldValue(true)).toBe(STORED_PASSWORD_PLACEHOLDER);
+ expect(getSharePasswordFieldValue(false)).toBe('');
+ expect(getSharePasswordFieldValue(undefined)).toBe('');
+ });
+});
diff --git a/apps/start/src/utils/share-password.ts b/apps/start/src/utils/share-password.ts
new file mode 100644
index 000000000..674aeff24
--- /dev/null
+++ b/apps/start/src/utils/share-password.ts
@@ -0,0 +1,21 @@
+/** Stands in for a stored share password, which the dashboard never receives. */
+export const STORED_PASSWORD_PLACEHOLDER = '••••••••';
+
+export function getSharePasswordFieldValue(
+ hasPassword: boolean | undefined
+): string {
+ return hasPassword ? STORED_PASSWORD_PLACEHOLDER : '';
+}
+
+/**
+ * The share mutations keep the stored password when the field is omitted and
+ * remove it on null, so the untouched placeholder must not become null.
+ */
+export function getSharePasswordToSubmit(
+ fieldValue: string | null | undefined
+): string | null | undefined {
+ if (fieldValue === STORED_PASSWORD_PLACEHOLDER) {
+ return undefined;
+ }
+ return fieldValue || null;
+}
diff --git a/packages/core/src/modules/share/share.constants.ts b/packages/core/src/modules/share/share.constants.ts
index 6a1797627..b64f1507d 100644
--- a/packages/core/src/modules/share/share.constants.ts
+++ b/packages/core/src/modules/share/share.constants.ts
@@ -1,9 +1,12 @@
import { z } from 'zod';
+/** Omitted keeps the stored password, null removes it, a string replaces it. */
+const zSharePassword = z.string().nullable().optional();
+
export const zShareOverview = z.object({
organizationId: z.string(),
projectId: z.string(),
- password: z.string().nullable(),
+ password: zSharePassword,
public: z.boolean(),
});
@@ -11,7 +14,7 @@ export const zShareDashboard = z.object({
organizationId: z.string(),
projectId: z.string(),
dashboardId: z.string(),
- password: z.string().nullable(),
+ password: zSharePassword,
public: z.boolean(),
});
@@ -19,6 +22,6 @@ export const zShareReport = z.object({
organizationId: z.string(),
projectId: z.string(),
reportId: z.string(),
- password: z.string().nullable(),
+ password: zSharePassword,
public: z.boolean(),
});
diff --git a/packages/core/src/modules/share/share.service.test.ts b/packages/core/src/modules/share/share.service.test.ts
index eb34bddbf..2dbbefbe9 100644
--- a/packages/core/src/modules/share/share.service.test.ts
+++ b/packages/core/src/modules/share/share.service.test.ts
@@ -25,6 +25,17 @@ interface FakeReportShare extends FakeShare {
reportId: string;
}
+/** Prisma skips an `undefined` field on update; a plain spread would not. */
+function applyUpdate<T extends FakeShare>(
+ existing: T,
+ update: { public: boolean; password?: string | null }
+): T {
+ const defined = Object.fromEntries(
+ Object.entries(update).filter(([, value]) => value !== undefined)
+ );
+ return { ...existing, ...defined };
+}
+
const overviewStore = new Map<string, FakeShare>();
const dashboardShareStore = new Map<string, FakeDashboardShare>();
const reportShareStore = new Map<string, FakeReportShare>();
@@ -72,10 +83,10 @@ const shareOverview = {
}: {
where: { projectId: string };
create: FakeShare;
- update: { public: boolean; password: string | null };
+ update: { public: boolean; password?: string | null };
}) => {
const existing = findOverviewByProjectId(projectId);
- const next = existing ? { ...existing, ...update } : { ...create };
+ const next = existing ? applyUpdate(existing, update) : { ...create };
overviewStore.set(next.id, next);
return next;
}
@@ -119,10 +130,10 @@ const shareDashboard = {
}: {
where: { dashboardId: string };
create: FakeDashboardShare;
- update: { public: boolean; password: string | null };
+ update: { public: boolean; password?: string | null };
}) => {
const existing = findDashboardShareByDashboardId(dashboardId);
- const next = existing ? { ...existing, ...update } : { ...create };
+ const next = existing ? applyUpdate(existing, update) : { ...create };
dashboardShareStore.set(next.id, next);
return next;
}
@@ -160,10 +171,10 @@ const shareReport = {
}: {
where: { reportId: string };
create: FakeReportShare;
- update: { public: boolean; password: string | null };
+ update: { public: boolean; password?: string | null };
}) => {
const existing = findReportShareByReportId(reportId);
- const next = existing ? { ...existing, ...update } : { ...create };
+ const next = existing ? applyUpdate(existing, update) : { ...create };
reportShareStore.set(next.id, next);
return next;
}
@@ -375,6 +386,73 @@ test('createShareOverview hashes the password and reports hasPassword', async ()
expect(noPassword).toMatchObject({ public: false, hasPassword: false });
});
+// "Update" in the share modals omits the password it never saw; treating the
+// omission as null silently removed the password from every updated share.
+test('createShareOverview keeps the stored password when the input omits it, removes it on null', async () => {
+ const base = { organizationId: 'org_1', projectId: 'proj_1' };
+ await subject.createShareOverview({
+ ...base,
+ public: true,
+ password: 'sekret123',
+ });
+
+ const kept = await subject.createShareOverview({ ...base, public: true });
+ expect(kept).toMatchObject({ hasPassword: true });
+
+ const removed = await subject.createShareOverview({
+ ...base,
+ public: true,
+ password: null,
+ });
+ expect(removed).toMatchObject({ hasPassword: false });
+});
+
+test('createShareDashboard keeps the stored password when the input omits it, removes it on null', async () => {
+ const base = {
+ organizationId: 'org_1',
+ projectId: 'proj_1',
+ dashboardId: 'dash_1',
+ };
+ await subject.createShareDashboard({
+ ...base,
+ public: true,
+ password: 'sekret123',
+ });
+
+ const kept = await subject.createShareDashboard({ ...base, public: true });
+ expect(kept).toMatchObject({ hasPassword: true });
+
+ const removed = await subject.createShareDashboard({
+ ...base,
+ public: true,
+ password: null,
+ });
+ expect(removed).toMatchObject({ hasPassword: false });
+});
+
+test('createShareReport keeps the stored password when the input omits it, removes it on null', async () => {
+ const base = {
+ organizationId: 'org_1',
+ projectId: 'proj_1',
+ reportId: 'report_1',
+ };
+ await subject.createShareReport({
+ ...base,
+ public: true,
+ password: 'sekret123',
+ });
+
+ const kept = await subject.createShareReport({ ...base, public: false });
+ expect(kept).toMatchObject({ public: false, hasPassword: true });
+
+ const removed = await subject.createShareReport({
+ ...base,
+ public: true,
+ password: null,
+ });
+ expect(removed).toMatchObject({ hasPassword: false });
+});
+
test('createShareDashboard throws NOT_FOUND when the dashboard does not exist', async () => {
await expect(
subject.createShareDashboard({
diff --git a/packages/core/src/modules/share/share.service.ts b/packages/core/src/modules/share/share.service.ts
index d2c3cc93d..482f954d6 100644
--- a/packages/core/src/modules/share/share.service.ts
+++ b/packages/core/src/modules/share/share.service.ts
@@ -92,7 +92,7 @@ export async function validateReportAccess(
},
});
- if (!(share && share.public)) {
+ if (!share?.public) {
throw new Error('Share not found or not public');
}
@@ -108,7 +108,7 @@ export async function validateReportAccess(
include: { report: true },
});
- if (!(share && share.public)) {
+ if (!share?.public) {
throw new Error('Share not found or not public');
}
@@ -214,7 +214,7 @@ export async function validateOverviewShareAccess(
where: { id: shareId },
});
- if (!(share && share.public)) {
+ if (!share?.public) {
throw new Error('Share not found or not public');
}
@@ -351,7 +351,7 @@ export async function getShareOverview(
},
});
- if (!(share && share.public)) {
+ if (!share?.public) {
throw new TRPCNotFoundError('Share not found');
}
@@ -385,11 +385,25 @@ export async function getShareOverviewSettings(
return { id: share.id, public: share.public, hasPassword: !!share.password };
}
+/**
+ * Keeps the input's three states apart: `undefined` leaves the stored password
+ * untouched (Prisma skips an undefined field on update), null or an empty
+ * string removes it, anything else replaces it.
+ */
+async function hashSharePassword(
+ password: string | null | undefined
+): Promise<string | null | undefined> {
+ if (password === undefined) {
+ return undefined;
+ }
+ return password ? await hashPassword(password) : null;
+}
+
export interface CreateShareOverviewInput {
organizationId: string;
projectId: string;
public: boolean;
- password: string | null;
+ password?: string | null;
}
export async function createShareOverview(
@@ -397,9 +411,7 @@ export async function createShareOverview(
input: CreateShareOverviewInput
) {
const db = deps.db;
- const passwordHash = input.password
- ? await hashPassword(input.password)
- : null;
+ const passwordHash = await hashSharePassword(input.password);
const share = await db.shareOverview.upsert({
where: { projectId: input.projectId },
@@ -435,7 +447,7 @@ export async function getShareDashboard(
},
});
- if (!(share && share.public)) {
+ if (!share?.public) {
throw new TRPCNotFoundError('Dashboard share not found');
}
@@ -477,7 +489,7 @@ export interface CreateShareDashboardInput {
projectId: string;
dashboardId: string;
public: boolean;
- password: string | null;
+ password?: string | null;
}
export async function createShareDashboard(
@@ -493,9 +505,7 @@ export async function createShareDashboard(
throw new TRPCNotFoundError('Dashboard not found');
}
- const passwordHash = input.password
- ? await hashPassword(input.password)
- : null;
+ const passwordHash = await hashSharePassword(input.password);
const db = deps.db;
const share = await db.shareDashboard.upsert({
@@ -525,7 +535,7 @@ export async function getShareDashboardReports(
) {
const share = await getShareDashboardById(deps, shareId);
- if (!(share && share.public)) {
+ if (!share?.public) {
throw new TRPCNotFoundError('Dashboard share not found');
}
@@ -559,7 +569,7 @@ export async function getShareReport(
},
});
- if (!(share && share.public)) {
+ if (!share?.public) {
throw new TRPCNotFoundError('Report share not found');
}
@@ -602,7 +612,7 @@ export interface CreateShareReportInput {
projectId: string;
reportId: string;
public: boolean;
- password: string | null;
+ password?: string | null;
}
export async function createShareReport(
@@ -614,9 +624,7 @@ export async function createShareReport(
throw new TRPCNotFoundError('Report not found');
}
- const passwordHash = input.password
- ? await hashPassword(input.password)
- : null;
+ const passwordHash = await hashSharePassword(input.password);
const db = deps.db;
const share = await db.shareReport.upsert({