added userdata

This commit is contained in:
Xeovalyte 2022-09-22 16:39:11 +02:00
parent da0619b016
commit d5e324978c
4 changed files with 116 additions and 10 deletions

View File

@ -2,6 +2,7 @@ const express = require('express');
const cors = require('cors');
const { initializeApp, applicationDefault, cert } = require('firebase-admin/app');
const { getFirestore, Timestamp, FieldValue } = require('firebase-admin/firestore');
const { verifyIdToken } = require('firebase-admin/auth')
const serviceAccount = require('./firebase.json');
@ -48,6 +49,35 @@ app.post('/checkrelatiecode', async (req, res) => {
} catch (e) {
return res.status(500).send({ error: e })
return res.status(500).send({ code: 'error', error: e })
}
})
app.post('/getrelatiecodes', async (req, res) => {
const { email } = req.body;
if (!email) return res.status(400).send({ code: 'no-email'})
try {
const ledenlijstRef = db.collection('ledenlijst')
const snapshot = await ledenlijstRef.where("email", "array-contains", email).get()
if (snapshot.empty) {
res.status(400).send({ code: 'no-relatiecodes'})
return;
}
let relatiecodes = [];
snapshot.forEach(doc => {
relatiecodes.push(doc.id)
});
res.status(200).send({ code: 'success', relatiecodes: relatiecodes })
} catch (e) {
return res.status(500).send({ code: 'error', error: e })
}
})

View File

@ -17,7 +17,7 @@
</template>
<script setup>
import { doc, getFirestore, serverTimestamp, writeBatch, collection, getDocs } from "firebase/firestore";
import { doc, getFirestore, serverTimestamp, writeBatch, collection, getDoc } from "firebase/firestore";
import { getAuth, onAuthStateChanged, signInWithEmailAndPassword } from "firebase/auth";
const db = getFirestore()
@ -27,25 +27,52 @@ const isAuthenticated = ref(false)
const user = ref('frikandel')
const auth = ref(null)
const userLoaded = ref(false)
const userData = ref(null)
const userPersons = ref([])
onMounted(() => {
auth.value = getAuth()
onAuthStateChanged(auth.value, (usr) => {
onAuthStateChanged(auth.value, async (usr) => {
if (usr) {
isAuthenticated.value = true
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)
}
isAuthenticated.value = true
} else {
isAuthenticated.value = false
user.value = null
userData.value = null
userPersons.value = []
}
userLoaded.value = true
})
})
const getPersons = async (persons) => {
userPersons.value = [];
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())
}
}
}
const ledenlijst = ref([])
provide('firebase', { db, ledenlijst, isAuthenticated, user, auth })
provide('firebase', { db, ledenlijst, isAuthenticated, user, userData, userPersons, auth })
</script>

View File

@ -43,8 +43,10 @@
<script setup>
import { useToast } from 'vue-toastification'
import { createUserWithEmailAndPassword, signInWithEmailAndPassword } from "firebase/auth";
import { doc, setDoc } from "firebase/firestore";
const { auth } = inject('firebase')
const { auth, db } = inject('firebase')
const toast = useToast()
@ -76,7 +78,7 @@ const submitLoginForm = () => {
Authorization: 'Basic WGVvdmFseXRlOmtNKjhuRXMzNTchalJlXm1KYnZrRSFOIw==',
'content-type': 'application/json'
},
body: JSON.stringify({ email: form.value.email, relatiecode: form.value.password })
body: JSON.stringify({ email: form.value.email, relatiecode: form.value.password.toUpperCase() })
}).then(response => response.json())
.then(response => {
disableButtons.value = false
@ -87,8 +89,12 @@ const submitLoginForm = () => {
disableButtons.value = false
console.log(err)
toast.error('Error while checking relatiecode')
toast.error('Error met het controleren van relatiecode')
});
} else if (error.code === 'auth/wrong-password') {
toast.error('Verkeerde wachtwoord')
} else {
toast.error('Error met inloggen')
}
disableButtons.value = false
@ -103,13 +109,44 @@ const submitCreateForm = () => {
disableButtons.value = true
createUserWithEmailAndPassword(auth.value, form.value.email, form.value.newPassword)
.then((userCredential) => {
fetch('http://test.xeovalyte.com:7289/getrelatiecodes', {
method: 'POST',
headers: {
Authorization: 'Basic WGVvdmFseXRlOmtNKjhuRXMzNTchalJlXm1KYnZrRSFOIw==',
'content-type': 'application/json'
},
body: JSON.stringify({ email: form.value.email })
}).then(response => response.json())
.then(response => {
disableButtons.value = false
if (response.code === 'error') return toast.error('Error tijdens maken van account')
else if (response.code === 'success') {
setDoc(doc(db, "users", userCredential.user.uid), {
email: form.value.email,
relatiecodes: [form.value.password.toUpperCase()],
allRelatiecodes: response.relatiecodes,
});
if (response.relatiecodes.length > 1) {
return navigateTo('/settings/config/managerelatiecodes')
}
}
})
.catch(err => {
disableButtons.value = false
console.log(err)
toast.error('Error met het controleren van relatiecode')
});
})
.catch((error) => {
const errorCode = error.code;
const errorMessage = error.message;
console.log(error)
toast.error('Error while creating account')
toast.error('Error tijdens het maken van account')
});
}

View File

@ -0,0 +1,12 @@
<template>
<div>
Manage relatiecodes
</div>
</template>
<script setup>
definePageMeta({
title: 'Beheer Personen',
key: 'back'
})
</script>