fix: modified get request to confirm key identity

pull/3421/head
Brandon 2 years ago committed by Brandon Cohen
parent 23dca554ca
commit 9ef07c3943

@ -3548,7 +3548,7 @@ paths:
responses: responses:
'204': '204':
description: Successfully registered push subscription description: Successfully registered push subscription
/user/{key}/pushSubscription: /user/{userId}/pushSubscription/{key}:
get: get:
summary: Get web push notification settings for a user summary: Get web push notification settings for a user
description: | description: |
@ -3556,6 +3556,11 @@ paths:
tags: tags:
- users - users
parameters: parameters:
- in: path
name: userId
required: true
schema:
type: number
- in: path - in: path
name: key name: key
required: true required: true
@ -3581,6 +3586,11 @@ paths:
tags: tags:
- users - users
parameters: parameters:
- in: path
name: userId
required: true
schema:
type: number
- in: path - in: path
name: key name: key
required: true required: true

@ -138,25 +138,6 @@ router.post(
} }
); );
router.get<{ key: string }>(
'/:key/pushSubscription',
async (req, res, next) => {
try {
const userPushSubRepository = getRepository(UserPushSubscription);
const userPushSub = await userPushSubRepository.findOneOrFail({
where: {
p256dh: req.params.key,
},
});
return res.status(200).json(userPushSub);
} catch (e) {
next({ status: 404, message: 'User subscription not found.' });
}
}
);
router.post< router.post<
never, never,
unknown, unknown,
@ -199,14 +180,43 @@ router.post<
} }
}); });
router.delete<{ key: string }>( router.get<{ userId: number; key: string }>(
'/:key/pushSubscription', '/:userId/pushSubscription/:key',
async (req, res, next) => { async (req, res, next) => {
try { try {
const userPushSubRepository = getRepository(UserPushSubscription); const userPushSubRepository = getRepository(UserPushSubscription);
const userPushSub = await userPushSubRepository.findOneOrFail({ const userPushSub = await userPushSubRepository.findOneOrFail({
where: { p256dh: req.params.key }, relations: {
user: true,
},
where: {
user: { id: req.params.userId },
p256dh: req.params.key,
},
});
return res.status(200).json(userPushSub);
} catch (e) {
next({ status: 404, message: 'User subscription not found.' });
}
}
);
router.delete<{ userId: number; key: string }>(
'/:userId/pushSubscription/:key',
async (req, res, next) => {
try {
const userPushSubRepository = getRepository(UserPushSubscription);
const userPushSub = await userPushSubRepository.findOneOrFail({
relations: {
user: true,
},
where: {
user: { id: req.params.userId },
p256dh: req.params.key,
},
}); });
await userPushSubRepository.remove(userPushSub); await userPushSubRepository.remove(userPushSub);

@ -39,34 +39,11 @@ const UserDropdown = () => {
const { user, revalidate } = useUser(); const { user, revalidate } = useUser();
const logout = async () => { const logout = async () => {
if ('serviceWorker' in navigator && user?.id) {
navigator.serviceWorker.getRegistration('/sw.js').then((registration) => {
registration?.pushManager
.getSubscription()
.then(async (subscription) => {
subscription
?.unsubscribe()
.then(async () => {
const parsedSub = JSON.parse(JSON.stringify(subscription));
await axios.delete(
`/api/v1/user/${parsedSub.keys.p256dh}/pushSubscription`
);
const response = await axios.post('/api/v1/auth/logout'); const response = await axios.post('/api/v1/auth/logout');
if (response.data?.status === 'ok') { if (response.data?.status === 'ok') {
revalidate(); revalidate();
} }
})
.catch(function (error) {
// eslint-disable-next-line no-console
console.log(
'[SW] Failure unsubscribing to push manager, error:',
error
);
});
});
});
}
}; };
return ( return (

@ -90,7 +90,7 @@ const UserWebPushSettings = () => {
.then(async () => { .then(async () => {
const parsedSub = JSON.parse(JSON.stringify(subscription)); const parsedSub = JSON.parse(JSON.stringify(subscription));
await axios.delete( await axios.delete(
`/api/v1/user/${parsedSub.keys.p256dh}/pushSubscription` `/api/v1/user/${user.id}/pushSubscription/${parsedSub.keys.p256dh}`
); );
setWebPushEnabled(false); setWebPushEnabled(false);
}) })
@ -120,7 +120,7 @@ const UserWebPushSettings = () => {
const parsedKey = JSON.parse(JSON.stringify(subscription)); const parsedKey = JSON.parse(JSON.stringify(subscription));
const currentUserPushSub = const currentUserPushSub =
await axios.get<UserPushSubscription>( await axios.get<UserPushSubscription>(
`/api/v1/user/${parsedKey.keys.p256dh}/pushSubscription` `/api/v1/user/${user.id}/pushSubscription/${parsedKey.keys.p256dh}`
); );
if (currentUserPushSub.data.p256dh !== parsedKey.keys.p256dh) { if (currentUserPushSub.data.p256dh !== parsedKey.keys.p256dh) {

Loading…
Cancel
Save