-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
33 lines (25 loc) · 881 Bytes
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
const configs = require('./configs');
const creds = require('./credentials');
const firebase = require("firebase");
// Required for side-effects
require("firebase/firestore");
firebase.initializeApp(configs);
const db = firebase.firestore();
let onDataChange = (data) => console.log('[stub] Data change:', data);
const { email, password } = creds;
firebase.auth().signInWithEmailAndPassword(email, password)
.then((user) => {
console.log('Authenticated successfully.');
listen('test', onDataChange);
})
module.exports.onDataChange = (callback) => onDataChange = callback;
function listen(collection, onDataChange) {
db.collection(collection)
.onSnapshot(function(snapshot) {
console.log('Setting listeners');
snapshot.docChanges().forEach(function(change) {
const { doc, type } = change;
onDataChange({ type, id: doc.id, data: doc.data() });
});
});
}