-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathFire.js
81 lines (67 loc) · 1.71 KB
/
Fire.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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
import firebase from '@react-native-firebase/app';
import '@react-native-firebase/firestore';
const app = firebase.app();// edited by Harish
export const db = app.firestore(); // edited by Harish
class Fire {
constructor() {
this.init();
// this.checkAuth();
}
init = () => {
if (!firebase.apps.length) {
// this is anonymous authentication. Add OAuth
firebase.initializeApp({
apiKey: "AIzaSyDtPLoNbRrgrLGQ4SXCcnqB3Rfd6KYl1a8",
authDomain: "chatmodule-app.firebaseapp.com",
databaseURL: "https://chatmodule-app.firebaseio.com",
projectId: "chatmodule-app",
storageBucket: "chatmodule-app.appspot.com",
messagingSenderId: "54472305059",
appId: "1:54472305059:web:f1d2c27cf6a86df74ad216",
measurementId: "G-WSGE135M2R",
});
}
};
// checkAuth = () => {
// firebase.auth().onAuthStateChanged((user) => {
// if (!user) {
// // firebase.auth().signInAnonymously();
// firebase.auth().signInWithEmailAndPassword(user.email, user.password);
// }
// });
// };
send = (messages) => {
messages.forEach((item) => {
const message = {
text: item.text,
timestamp: firebase.database.ServerValue.TIMESTAMP,
user: item.user,
};
this.db.push(message);
});
};
parse = (message) => {
const { user, text, timestamp } = message.val();
const { key: _id } = message;
const createdAt = new Date(timestamp);
return {
_id,
createdAt,
text,
user,
};
};
get = (callback) => {
this.db.on("child_added", (snapshot) => callback(this.parse(snapshot)));
};
off() {
this.db.off();
}
get db() {
return firebase.database().ref("messages");
}
get uid() {
return (firebase.auth().currentUser || {}).uid;
}
}
export default new Fire();