notify and debug apple-signin api

This commit is contained in:
Dobromir Popov
2024-04-17 01:29:20 +03:00
parent d81b64b34d
commit 6e08ea21d4
4 changed files with 80 additions and 2 deletions

37
pages/api/notify.ts Normal file
View File

@ -0,0 +1,37 @@
const webPush = require('web-push')
webPush.setVapidDetails(
`mailto:${process.env.WEB_PUSH_EMAIL}`,
process.env.NEXT_PUBLIC_WEB_PUSH_PUBLIC_KEY,
process.env.WEB_PUSH_PRIVATE_KEY
)
const Notification = (req, res) => {
if (req.method == 'POST') {
const { subscription } = req.body
webPush
.sendNotification(
subscription,
JSON.stringify({ title: 'Hello Web Push', message: 'Your web push notification is here!' })
)
.then(response => {
res.writeHead(response.statusCode, response.headers).end(response.body)
})
.catch(err => {
if ('statusCode' in err) {
res.writeHead(err.statusCode, err.headers).end(err.body)
} else {
console.error(err)
res.statusCode = 500
res.end()
}
})
} else {
res.statusCode = 405
res.end()
}
}
export default Notification