This commit is contained in:
parent
c423b0da44
commit
eb08f5cb46
@ -3,7 +3,7 @@ const cors = require('cors');
|
||||
const { initializeApp, applicationDefault, cert } = require('firebase-admin/app');
|
||||
const { getFirestore, Timestamp, FieldValue } = require('firebase-admin/firestore');
|
||||
const { getMessaging } = require('firebase-admin/messaging')
|
||||
const { verifyIdToken } = require('firebase-admin/auth')
|
||||
const { getAuth } = require('firebase-admin/auth')
|
||||
|
||||
const serviceAccount = require('./firebase.json');
|
||||
|
||||
@ -102,10 +102,69 @@ app.post('/subscribetotopic', async (req, res) => {
|
||||
.catch((error) => {
|
||||
console.log('Error subscribing to topic:', error);
|
||||
|
||||
return res.status(500).send({ code: 'error', error: e })
|
||||
return res.status(500).send({ code: 'error', error: error })
|
||||
});
|
||||
|
||||
|
||||
} catch (e) {
|
||||
return res.status(500).send({ code: 'error', error: e })
|
||||
}
|
||||
})
|
||||
|
||||
app.post('/sendmessage', async (req, res) => {
|
||||
const { title, body, token } = req.body;
|
||||
|
||||
if (!title) return res.status(400).send({ code: 'no-topic'})
|
||||
if (!body) return res.status(400).send({ code: 'no-registrationToken'})
|
||||
if (!token) return res.status(400).send({ code: 'no-token'})
|
||||
|
||||
try {
|
||||
getAuth()
|
||||
.verifyIdToken(token)
|
||||
.then(async (decodedToken) => {
|
||||
const uid = decodedToken.uid;
|
||||
|
||||
const docRef = db.collection('users').doc(uid);
|
||||
const doc = await docRef.get();
|
||||
|
||||
if (!doc.exists) return res.status(400).send({ code: 'not-found'})
|
||||
|
||||
const data = doc.data()
|
||||
|
||||
if (!data.sendNews) return res.status(400).send({ code: 'no-permissions'})
|
||||
|
||||
const message = {
|
||||
notification: {
|
||||
title: title,
|
||||
body: body
|
||||
},
|
||||
topic: 'all',
|
||||
};
|
||||
|
||||
|
||||
getMessaging().send(message)
|
||||
.then((response) => {
|
||||
// Response is a message ID string.
|
||||
console.log('Successfully sent message:', response);
|
||||
|
||||
res.status(200).send({ code: 'success', response: response })
|
||||
})
|
||||
.catch((error) => {
|
||||
console.log('Error sending message:', error);
|
||||
|
||||
return res.status(500).send({ code: 'error', error: error })
|
||||
});
|
||||
|
||||
|
||||
})
|
||||
.catch((error) => {
|
||||
console.log(error)
|
||||
|
||||
return res.status(500).send({ code: 'error', error: error })
|
||||
});
|
||||
|
||||
|
||||
|
||||
} catch (e) {
|
||||
return res.status(500).send({ code: 'error', error: e })
|
||||
}
|
||||
|
@ -24,7 +24,7 @@ definePageMeta({
|
||||
key: 'back'
|
||||
})
|
||||
|
||||
const { news, userData, db } = inject('firebase')
|
||||
const { news, userData, db, auth } = inject('firebase')
|
||||
const router = useRouter()
|
||||
const toast = useToast()
|
||||
|
||||
@ -40,6 +40,20 @@ const sendNews = async () => {
|
||||
|
||||
|
||||
try {
|
||||
const idToken = await auth.value.currentUser.getIdToken(true)
|
||||
console.log(idToken)
|
||||
|
||||
await fetch('https://api.xeovalyte.com/sendmessage', {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
Authorization: 'Basic WGVvdmFseXRlOmtNKjhuRXMzNTchalJlXm1KYnZrRSFOIw==',
|
||||
'content-type': 'application/json'
|
||||
},
|
||||
body: JSON.stringify({ title: form.value.title, body: form.value.description, token: idToken })
|
||||
}).then(response => response.json())
|
||||
.then(async response => {
|
||||
console.log(response)
|
||||
|
||||
await addDoc(collection(db, "news"), {
|
||||
title: form.value.title,
|
||||
description: form.value.description,
|
||||
@ -57,6 +71,12 @@ const sendNews = async () => {
|
||||
toast.success('Bericht is verstuurd')
|
||||
|
||||
navigateTo('/news')
|
||||
})
|
||||
.catch(err => {
|
||||
console.log(err)
|
||||
|
||||
toast.error('Error tijdens het berict sturen')
|
||||
});
|
||||
} catch (e) {
|
||||
console.log(e)
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user