Skip to content

Commit 52e8a92

Browse files
bndct-devopsCopilot
andcommitted
fix: harden push subscription diagnostics
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent 0c76a3e commit 52e8a92

2 files changed

Lines changed: 22 additions & 4 deletions

File tree

backend/main.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -217,8 +217,17 @@ async def push_subscribe(request: Request):
217217
endpoint = body.get("endpoint")
218218
p256dh = body.get("p256dh")
219219
auth = body.get("auth")
220-
if not all([profile_id, endpoint, p256dh, auth]):
221-
return JSONResponse({"detail": "Missing fields"}, status_code=422)
220+
missing = [
221+
name for name, value in {
222+
"profileId": profile_id,
223+
"endpoint": endpoint,
224+
"p256dh": p256dh,
225+
"auth": auth,
226+
}.items()
227+
if not value
228+
]
229+
if missing:
230+
return JSONResponse({"detail": "Missing fields", "missing": missing}, status_code=422)
222231
with Session(engine) as session:
223232
existing = session.exec(
224233
select(PushSubscription).where(PushSubscription.profile_id == profile_id)
@@ -1254,4 +1263,3 @@ def _workout_out(w: Workout, sets: list) -> schemas.WorkoutOut:
12541263

12551264
if __name__ == '__main__':
12561265
uvicorn.run("main:app", host="0.0.0.0", port=8000, reload=True)
1257-

frontend/src/api.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -312,14 +312,24 @@ export async function subscribePush(profileId) {
312312
const json = sub.toJSON()
313313
const p256dhKey = sub.getKey?.('p256dh')
314314
const authKey = sub.getKey?.('auth')
315+
const endpoint = sub.endpoint || json.endpoint
315316
const p256dh = p256dhKey ? bytesToBase64Url(new Uint8Array(p256dhKey)) : json.keys?.p256dh
316317
const auth = authKey ? bytesToBase64Url(new Uint8Array(authKey)) : json.keys?.auth
318+
if (!profileId || !endpoint || !p256dh || !auth) {
319+
console.warn('[push] subscription payload missing fields', {
320+
hasProfileId: Boolean(profileId),
321+
hasEndpoint: Boolean(endpoint),
322+
hasP256dh: Boolean(p256dh),
323+
hasAuth: Boolean(auth),
324+
})
325+
return false
326+
}
317327
const res = await authFetch(base + '/api/push/subscribe', {
318328
method: 'POST',
319329
headers: { 'Content-Type': 'application/json' },
320330
body: JSON.stringify({
321331
profileId,
322-
endpoint: json.endpoint,
332+
endpoint,
323333
p256dh,
324334
auth,
325335
}),

0 commit comments

Comments
 (0)