This commit is contained in:
parent
2b6afffa73
commit
587ca2701d
2
frontend/.gitignore
vendored
2
frontend/.gitignore
vendored
@ -6,3 +6,5 @@ node_modules
|
||||
.output
|
||||
.env
|
||||
dist
|
||||
|
||||
service-account.json
|
||||
|
@ -122,26 +122,21 @@ const setupNotifications = () => {
|
||||
|
||||
// On success, we should be able to receive notifications
|
||||
PushNotifications.addListener('registration',
|
||||
(token) => {
|
||||
async (token) => {
|
||||
// alert('Push registration success, token: ' + token.value);
|
||||
registrationToken.value = token
|
||||
|
||||
|
||||
fetch('https://api.xeovalyte.com/subscribetotopic', {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
Authorization: 'Basic WGVvdmFseXRlOmtNKjhuRXMzNTchalJlXm1KYnZrRSFOIw==',
|
||||
'content-type': 'application/json'
|
||||
},
|
||||
body: JSON.stringify({ topic: 'all', registrationToken: token.value })
|
||||
}).then(response => response.json())
|
||||
.then(response => {
|
||||
console.log(response)
|
||||
const { error } = await useFetch('/api/subscribetotopic', {
|
||||
method: 'post',
|
||||
body: { topic: 'all', registrationToken: token.value }
|
||||
})
|
||||
.catch(err => {
|
||||
console.log(err)
|
||||
toast.error('Error tijdens het registreren van push notificaties')
|
||||
});
|
||||
|
||||
if (error.value) {
|
||||
console.log(error.value)
|
||||
return toast.error('Error tijdens het krijgen van relateicodes')
|
||||
}
|
||||
|
||||
console.log('Subscribed to topic!')
|
||||
}
|
||||
);
|
||||
|
||||
@ -174,23 +169,21 @@ const setupNotifications = () => {
|
||||
}
|
||||
|
||||
const registerFCM = () => {
|
||||
getToken(messaging.value, { vapidKey: 'BI7l3nyGV6wJcFh7wrwmQ42W7RSXl46bmhXZJmDd4P-0K_JFP0ClTqjO-rr5H5DXBbmVR4kXwxFpUlo_d6cUy4Q' }).then((currentToken) => {
|
||||
getToken(messaging.value, { vapidKey: 'BI7l3nyGV6wJcFh7wrwmQ42W7RSXl46bmhXZJmDd4P-0K_JFP0ClTqjO-rr5H5DXBbmVR4kXwxFpUlo_d6cUy4Q' }).then(async (currentToken) => {
|
||||
if (currentToken) {
|
||||
console.log(currentToken)
|
||||
fetch('https://api.xeovalyte.com/subscribetotopic', {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
Authorization: 'Basic WGVvdmFseXRlOmtNKjhuRXMzNTchalJlXm1KYnZrRSFOIw==',
|
||||
'content-type': 'application/json'
|
||||
},
|
||||
body: JSON.stringify({ topic: 'all', registrationToken: currentToken })
|
||||
}).then(response => response.json())
|
||||
.then(response => {
|
||||
console.log(response)
|
||||
|
||||
await useFetch('/api/subscribetotopic', {
|
||||
method: 'post',
|
||||
body: { topic: 'all', registrationToken: currentToken }
|
||||
})
|
||||
.catch(err => {
|
||||
console.log(err)
|
||||
});
|
||||
|
||||
if (error.value) {
|
||||
console.log(error.value)
|
||||
return toast.error('Error tijdens het registreren van push notifications')
|
||||
}
|
||||
|
||||
console.log('Subscribed to topic!')
|
||||
} else {
|
||||
// Show permission request UI
|
||||
console.log('No registration token available. Request permission to generate one.');
|
||||
|
@ -24,5 +24,10 @@ export default defineNuxtConfig({
|
||||
{ rel: 'icon', href: '/favicon.ico', type: 'image/x-icon' }
|
||||
]
|
||||
}
|
||||
},
|
||||
runtimeConfig: {
|
||||
privateKeyId: '',
|
||||
privateKey: '',
|
||||
clientId: ''
|
||||
}
|
||||
})
|
||||
|
2350
frontend/package-lock.json
generated
2350
frontend/package-lock.json
generated
File diff suppressed because it is too large
Load Diff
@ -26,6 +26,7 @@
|
||||
"@vueuse/firebase": "^9.2.0",
|
||||
"@vueuse/shared": "^9.4.0",
|
||||
"firebase": "^9.14.0",
|
||||
"firebase-admin": "^11.4.1",
|
||||
"vue-toastification": "^2.0.0-rc.5"
|
||||
}
|
||||
}
|
||||
|
@ -24,7 +24,7 @@ definePageMeta({
|
||||
key: 'back'
|
||||
})
|
||||
|
||||
const { news, userData, db, auth } = inject('firebase')
|
||||
const { news, db, auth } = inject('firebase')
|
||||
const router = useRouter()
|
||||
const toast = useToast()
|
||||
|
||||
@ -42,16 +42,15 @@ const sendNews = async () => {
|
||||
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)
|
||||
const { error } = await useFetch('/api/sendmessage', {
|
||||
method: 'post',
|
||||
body: { title: form.value.title, body: form.value.description, token: idToken }
|
||||
})
|
||||
|
||||
if (error.value) {
|
||||
console.log(error.value)
|
||||
return toast.error('Error tijdens het versturen van het bericht')
|
||||
}
|
||||
|
||||
await addDoc(collection(db, "news"), {
|
||||
title: form.value.title,
|
||||
@ -69,17 +68,10 @@ 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)
|
||||
|
||||
toast.error('Error tijdens het berict sturen')
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
@ -30,7 +30,7 @@ definePageMeta({
|
||||
key: 'back'
|
||||
})
|
||||
|
||||
const { user, userAllPersons, userPersons, db, getPersons } = inject('firebase')
|
||||
const { user, userAllPersons, userPersons, db, getPersons, auth } = inject('firebase')
|
||||
const toast = useToast()
|
||||
|
||||
const buttonsDisabled = ref(false)
|
||||
@ -76,18 +76,23 @@ const updateCheckbox = (person) => {
|
||||
person.checked = !person.checked
|
||||
}
|
||||
|
||||
const getAllPersons = () => {
|
||||
const getAllPersons = async () => {
|
||||
if (userPersons.value.length === 0) return setTimeout(() => getAllPersons(), 50)
|
||||
fetch('https://api.xeovalyte.com/getrelatiecodes', {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
Authorization: 'Basic WGVvdmFseXRlOmtNKjhuRXMzNTchalJlXm1KYnZrRSFOIw==',
|
||||
'content-type': 'application/json'
|
||||
},
|
||||
body: JSON.stringify({ email: user.value.email })
|
||||
}).then(response => response.json())
|
||||
.then(response => {
|
||||
response.persons.forEach(person => {
|
||||
|
||||
const idToken = await auth.value.currentUser.getIdToken(true)
|
||||
|
||||
|
||||
const { data: response, error } = await useFetch('/api/getrelatiecodes', {
|
||||
method: 'post',
|
||||
body: { email: user.value.email, token: idToken }
|
||||
})
|
||||
|
||||
if (error.value) {
|
||||
console.log(error.value)
|
||||
return toast.error('Error tijdens het krijgen van relateicodes')
|
||||
}
|
||||
|
||||
response.value.persons.forEach(person => {
|
||||
if (userPersons.value.map(a => a.relatiecode).includes(person.relatiecode)) {
|
||||
person.checked = true
|
||||
} else {
|
||||
@ -95,12 +100,6 @@ const getAllPersons = () => {
|
||||
}
|
||||
})
|
||||
|
||||
userAllPersons.value = response.persons
|
||||
})
|
||||
.catch(err => {
|
||||
console.log(err)
|
||||
|
||||
toast.error('Error tijdens het ophalen van gegevens')
|
||||
});
|
||||
userAllPersons.value = response.value.persons
|
||||
}
|
||||
</script>
|
26
frontend/server/api/checkrelatiecode.post.js
Normal file
26
frontend/server/api/checkrelatiecode.post.js
Normal file
@ -0,0 +1,26 @@
|
||||
import { db } from '../utils/firebase'
|
||||
|
||||
export default defineEventHandler(async event => {
|
||||
const { relatiecode, email } = await readBody(event);
|
||||
|
||||
if (!relatiecode) throw createError({ statusCode: 400, statusMessage: 'no-relatiecode'})
|
||||
if (!email) throw createError({ statusCode: 400, statusMessage: 'no-email'})
|
||||
|
||||
try {
|
||||
const docRef = db.collection('ledenlijst').doc(relatiecode);
|
||||
const doc = await docRef.get();
|
||||
|
||||
if (!doc.exists) throw createError({ statusCode: 400, statusMessage: 'incorrect'})
|
||||
|
||||
const data = doc.data()
|
||||
|
||||
if (data.email[0] === email || data.email[1] === email) {
|
||||
return { code: 'correct' }
|
||||
} else {
|
||||
throw createError({ statusCode: 400, statusMessage: 'incorrect'})
|
||||
}
|
||||
|
||||
} catch (e) {
|
||||
throw createError({ statusCode: 500, statusMessage: e.message })
|
||||
}
|
||||
})
|
40
frontend/server/api/getrelatiecodes.post.js
Normal file
40
frontend/server/api/getrelatiecodes.post.js
Normal file
@ -0,0 +1,40 @@
|
||||
import { db } from '../utils/firebase'
|
||||
import { getAuth } from 'firebase-admin/auth'
|
||||
|
||||
export default defineEventHandler(async event => {
|
||||
const { email, token } = await readBody(event);
|
||||
|
||||
if (!email) throw createError({ statusCode: 400, statusMessage: 'no-email'})
|
||||
if (!token) throw createError({ statusCode: 400, statusMessage: 'no-token'})
|
||||
|
||||
try {
|
||||
await getAuth().verifyIdToken(token)
|
||||
} catch (e) {
|
||||
console.log(e);
|
||||
throw createError({ statusCode: 500, statusMessage: 'error-verify-id'})
|
||||
}
|
||||
|
||||
try {
|
||||
const ledenlijstRef = db.collection('ledenlijst')
|
||||
|
||||
const snapshot = await ledenlijstRef.where("email", "array-contains", email).get()
|
||||
|
||||
if (snapshot.empty) {
|
||||
throw createError({ statusCode: 400, statusMessage: 'no-relatiecode'})
|
||||
}
|
||||
|
||||
let relatiecodes = [];
|
||||
let persons = [];
|
||||
|
||||
snapshot.forEach(doc => {
|
||||
relatiecodes.push(doc.id)
|
||||
const data = doc.data()
|
||||
persons.push({ fullName: data.fullName, relatiecode: doc.id })
|
||||
});
|
||||
|
||||
return { code: 'success', relatiecodes: relatiecodes, persons: persons }
|
||||
|
||||
} catch (e) {
|
||||
throw createError({ statusCode: 500, statusMessage: e.message })
|
||||
}
|
||||
})
|
63
frontend/server/api/sendmessage.post.js
Normal file
63
frontend/server/api/sendmessage.post.js
Normal file
@ -0,0 +1,63 @@
|
||||
import { db } from '../utils/firebase'
|
||||
import { getAuth } from 'firebase-admin/auth'
|
||||
import { getMessaging } from 'firebase-admin/messaging'
|
||||
|
||||
export default defineEventHandler(async event => {
|
||||
const { token, body, title } = await readBody(event);
|
||||
|
||||
if (!token) throw createError({ statusCode: 400, statusMessage: 'no-token'})
|
||||
if (!body) throw createError({ statusCode: 400, statusMessage: 'no-body'})
|
||||
if (!title) throw createError({ statusCode: 400, statusMessage: 'no-title'})
|
||||
|
||||
let decodedToken = null;
|
||||
|
||||
try {
|
||||
decodedToken = await getAuth().verifyIdToken(token)
|
||||
} catch (e) {
|
||||
console.log(e)
|
||||
throw createError({ statusCode: 500, statusMessage: 'error-verify-id'})
|
||||
}
|
||||
|
||||
if (!decodedToken) throw createError({ statusCode: 500, statusMessage: 'error-verify-id-test'})
|
||||
|
||||
try {
|
||||
const uid = decodedToken.uid;
|
||||
|
||||
const docRef = db.collection('users').doc(uid);
|
||||
const doc = await docRef.get();
|
||||
|
||||
if (!doc.exists) throw createError({ statusCode: 500, statusMessage: 'doc-not-found'})
|
||||
|
||||
const data = doc.data()
|
||||
|
||||
if (!data.sendNews) throw createError({ statusCode: 500, statusMessage: 'no-permissions'})
|
||||
|
||||
const message = {
|
||||
notification: {
|
||||
title: title,
|
||||
body: body,
|
||||
},
|
||||
webpush: {
|
||||
notification: {
|
||||
icon: '/ios/256.png'
|
||||
}
|
||||
},
|
||||
topic: 'test',
|
||||
apns: {
|
||||
payload: {
|
||||
aps: {
|
||||
sound: 'default'
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
const response = await getMessaging().send(message)
|
||||
|
||||
console.log('Successfully sent message:', response);
|
||||
|
||||
return { code: 'success', response: response }
|
||||
|
||||
} catch (e) {
|
||||
throw createError({ statusCode: 500, statusMessage: e.message })
|
||||
}
|
||||
})
|
19
frontend/server/api/subscribetotopic.post.js
Normal file
19
frontend/server/api/subscribetotopic.post.js
Normal file
@ -0,0 +1,19 @@
|
||||
import { getMessaging } from 'firebase-admin/messaging'
|
||||
|
||||
export default defineEventHandler(async event => {
|
||||
const { topic, registrationToken } = await readBody(event);
|
||||
|
||||
|
||||
if (!topic) throw createError({ statusCode: 400, statusMessage: 'no-topic'})
|
||||
if (!registrationToken) throw createError({ statusCode: 400, statusMessage: 'no-registrationtoken'})
|
||||
|
||||
try {
|
||||
await getMessaging().subscribeToTopic([registrationToken], topic)
|
||||
|
||||
return { code: 'success'}
|
||||
} catch (e) {
|
||||
console.log(e)
|
||||
|
||||
throw createError({ statusCode: 500, statusMessage: e.message })
|
||||
}
|
||||
})
|
25
frontend/server/utils/firebase.js
Normal file
25
frontend/server/utils/firebase.js
Normal file
@ -0,0 +1,25 @@
|
||||
import { createRequire } from 'module';
|
||||
const require = createRequire(import.meta.url);
|
||||
const admin = require("firebase-admin");
|
||||
|
||||
import { getFirestore } from 'firebase-admin/firestore';
|
||||
|
||||
const config = useRuntimeConfig()
|
||||
|
||||
export const app = admin.initializeApp({
|
||||
credential: admin.credential.cert({
|
||||
"project_id": "wrbapp",
|
||||
"private_key_id": config.privateKeyId,
|
||||
"private_key": config.privateKey,
|
||||
"client_email": "firebase-adminsdk-iqgyy@wrbapp.iam.gserviceaccount.com",
|
||||
"client_id": config.clientId,
|
||||
"auth_uri": "https://accounts.google.com/o/oauth2/auth",
|
||||
"token_uri": "https://oauth2.googleapis.com/token",
|
||||
"auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
|
||||
"client_x509_cert_url": "https://www.googleapis.com/robot/v1/metadata/x509/firebase-adminsdk-iqgyy%40wrbapp.iam.gserviceaccount.com"
|
||||
})
|
||||
})
|
||||
|
||||
export const firestore = getFirestore()
|
||||
|
||||
export const db = getFirestore();
|
Loading…
Reference in New Issue
Block a user