diff --git a/backend/index.js b/backend/index.js index 09ae64f..459df74 100644 --- a/backend/index.js +++ b/backend/index.js @@ -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 }) } diff --git a/frontend/pages/news/newmessage.vue b/frontend/pages/news/newmessage.vue index afd7d3d..e3f309e 100644 --- a/frontend/pages/news/newmessage.vue +++ b/frontend/pages/news/newmessage.vue @@ -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,23 +40,43 @@ const sendNews = async () => { try { - await addDoc(collection(db, "news"), { - title: form.value.title, - description: form.value.description, - date: serverTimestamp() - }); + const idToken = await auth.value.currentUser.getIdToken(true) + console.log(idToken) - if (news.value) { - news.value.push({ - title: form.value.title, - description: form.value.description, - date: Date.now() + 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, + date: serverTimestamp() + }); + + if (news.value) { + news.value.push({ + title: form.value.title, + description: form.value.description, + date: Date.now() + }) + } + + toast.success('Bericht is verstuurd') + + navigateTo('/news') }) - } + .catch(err => { + console.log(err) - toast.success('Bericht is verstuurd') - - navigateTo('/news') + toast.error('Error tijdens het berict sturen') + }); } catch (e) { console.log(e)