Skip to content

Commit 5a529b1

Browse files
committed
fix(notifications): drop stale credentials when switching ntfy auth type
1 parent 2c14982 commit 5a529b1

2 files changed

Lines changed: 25 additions & 4 deletions

File tree

client/src/Pages/Notifications/create/index.tsx

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@ import { zodResolver } from "@hookform/resolvers/zod";
1313
import { useGet, usePost, usePatch } from "@/Hooks/UseApi";
1414
import { useNotificationForm } from "@/Hooks/useNotificationForm";
1515
import type { NotificationFormData } from "@/Validation/notifications";
16-
import type { Notification } from "@/Types/Notification";
16+
import { type Notification, NotificationChannels, AuthTypes } from "@/Types/Notification";
1717
import { useTranslation } from "react-i18next";
18-
import { NotificationChannels, AuthTypes } from "@/Types/Notification";
18+
import { normalizeNtfy } from "@/Utils/NotificationUtils";
1919

2020
const NotificationsCreatePage = () => {
2121
const { t } = useTranslation();
@@ -78,9 +78,10 @@ const NotificationsCreatePage = () => {
7878
}, [watchedType, t]);
7979

8080
const onSubmit = async (data: NotificationFormData) => {
81+
const payload = normalizeNtfy(data);
8182
const result = isEditMode
82-
? await patch(`/notifications/${notificationId}`, data)
83-
: await post("/notifications", data);
83+
? await patch(`/notifications/${notificationId}`, payload)
84+
: await post("/notifications", payload);
8485
if (result) {
8586
navigate("/notifications");
8687
}
@@ -352,6 +353,7 @@ const NotificationsCreatePage = () => {
352353
helperText={fieldState.error?.message ?? ""}
353354
/>
354355
)}
356+
shouldUnregister={true}
355357
/>
356358
<Controller
357359
name="password"
@@ -370,6 +372,7 @@ const NotificationsCreatePage = () => {
370372
helperText={fieldState.error?.message ?? ""}
371373
/>
372374
)}
375+
shouldUnregister={true}
373376
/>
374377
</>
375378
)}
@@ -391,6 +394,7 @@ const NotificationsCreatePage = () => {
391394
helperText={fieldState.error?.message ?? ""}
392395
/>
393396
)}
397+
shouldUnregister={true}
394398
/>
395399
)}
396400
</Stack>
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import type { NotificationFormData } from "@/Validation/notifications";
2+
3+
export const normalizeNtfy = (data: NotificationFormData): NotificationFormData => {
4+
if (data.type !== "ntfy") return data;
5+
const authType = data.authType ?? "none";
6+
const base = { ...data, authType };
7+
switch (authType) {
8+
case "none":
9+
return { ...base, username: "", password: "", accessToken: "" };
10+
case "basic":
11+
return { ...base, accessToken: "" };
12+
case "bearer":
13+
return { ...base, username: "", password: "" };
14+
default:
15+
return base;
16+
}
17+
};

0 commit comments

Comments
 (0)