diff --git a/frontend/app.vue b/frontend/app.vue index 6537447..457fb93 100644 --- a/frontend/app.vue +++ b/frontend/app.vue @@ -48,61 +48,61 @@ const users = ref([]) const messaging = ref(null) onMounted(() => { - auth.value = getAuth() + auth.value = getAuth() - if (process.client) { - if ('serviceWorker' in navigator && window.isSecureContext) { - Device.getInfo().then(info => { - if (info.platform === 'web') registerSW() - }); - + if (process.client) { + if ('serviceWorker' in navigator && window.isSecureContext) { + Device.getInfo().then(info => { + if (info.platform === 'web') registerSW() + }); + + } + } + + onAuthStateChanged(auth.value, async (usr) => { + if (usr) { + user.value = usr + + let docRef = doc(db, "users", user.value.uid); + let docSnap = await getDoc(docRef); + + if (docSnap.exists()) { + const data = docSnap.data() + userData.value = data + getPersons(userData.value.relatiecodes) + } else { + setTimeout(() => window.location.reload(true), 1000) } + + if (!userData.value.sendNews && route.path === '/news/newmessage') navigateTo('/') + if (!userData.value.admin && route.path.startsWith('/settings/admin')) navigateTo('/') + + isAuthenticated.value = true + + logDeviceInfo() + + } else { + isAuthenticated.value = false + user.value = null + userData.value = null + userPersons.value = [] } - onAuthStateChanged(auth.value, async (usr) => { - if (usr) { - user.value = usr - - let docRef = doc(db, "users", user.value.uid); - let docSnap = await getDoc(docRef); - - if (docSnap.exists()) { - const data = docSnap.data() - userData.value = data - getPersons(userData.value.relatiecodes) - } else { - setTimeout(() => window.location.reload(true), 1000) - } - - if (!userData.value.sendNews && route.path === '/news/newmessage') navigateTo('/') - if (!userData.value.admin && route.path.startsWith('/settings/admin')) navigateTo('/') - - isAuthenticated.value = true - - logDeviceInfo() - - } else { - isAuthenticated.value = false - user.value = null - userData.value = null - userPersons.value = [] - } - - userLoaded.value = true - }) + userLoaded.value = true + }) }) const getPersons = async (persons) => { - userPersons.value = []; + userPersons.value = []; - for (let i = 0; i < persons.length; i++) { - const docRef = doc(db, "ledenlijst", persons[i]); - const docSnap = await getDoc(docRef); + for (let i = 0; i < persons.length; i++) { + const docRef = doc(db, "ledenlijst", persons[i]); + const docSnap = await getDoc(docRef); - if (docSnap.exists()) { - userPersons.value.push(docSnap.data()) - } + if (docSnap.exists()) { + userPersons.value.push(docSnap.data()) } + } } const setupNotifications = () => { @@ -173,7 +173,7 @@ const registerFCM = () => { if (currentToken) { console.log(currentToken) - await useFetch('/api/subscribetotopic', { + const { error} = await useFetch('/api/subscribetotopic', { method: 'post', body: { topic: 'all', registrationToken: currentToken } }) @@ -183,6 +183,7 @@ const registerFCM = () => { return toast.error('Error tijdens het registreren van push notifications') } + registrationToken.value = currentToken console.log('Subscribed to topic!') } else { // Show permission request UI @@ -220,7 +221,7 @@ const logDeviceInfo = async () => { const ledenlijst = ref([]) -provide('firebase', { db, ledenlijst, isAuthenticated, user, userData, userPersons, auth, users, userAllPersons, getPersons, calEvents, news, registrationToken, contestTimes, competitors }) +provide('firebase', { db, ledenlijst, isAuthenticated, user, userData, userPersons, auth, users, userAllPersons, getPersons, calEvents, news, registrationToken, contestTimes, competitors, registrationToken })