wrbapp/backend/index.js
2022-09-26 16:23:45 +02:00

86 lines
2.3 KiB
JavaScript

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');
initializeApp({
credential: cert(serviceAccount)
});
const db = getFirestore();
const app = express();
app.use(express.json());
app.use(cors({
origin: '*',
}));
app.listen(7289, () => console.log('API is online!'));
app.get('/', (req, res) => {
res.status(200).send({
status: 'success',
});
});
app.post('/checkrelatiecode', async (req, res) => {
const { relatiecode, email } = req.body;
if (!relatiecode) return res.status(400).send({ code: 'no-relatiecode'})
if (!email) return res.status(400).send({ code: 'no-email'})
try {
const docRef = db.collection('ledenlijst').doc(relatiecode);
const doc = await docRef.get();
if (!doc.exists) return res.status(400).send({ code: 'incorrect'})
const data = doc.data()
if (data.email[0] === email || data.email[1] === email) {
return res.status(200).send({ code: 'correct'})
} else {
return res.status(400).send({ code: 'incorrect'})
}
} catch (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 = [];
let persons = [];
snapshot.forEach(doc => {
relatiecodes.push(doc.id)
const data = doc.data()
persons.push({ fullName: data.fullName, relatiecode: doc.id })
});
res.status(200).send({ code: 'success', relatiecodes: relatiecodes, persons: persons })
} catch (e) {
return res.status(500).send({ code: 'error', error: e })
}
})