-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathFire.js
More file actions
77 lines (68 loc) · 2.02 KB
/
Copy pathFire.js
File metadata and controls
77 lines (68 loc) · 2.02 KB
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
import { getAuth, onAuthStateChanged } from "firebase/auth";
import firebase from 'firebase/compat/app';
import 'firebase/compat/auth';
require('firebase/compat/auth');
import 'firebase/compat/firestore';
import 'firebase/compat/database';
require('@firebase/database');
class Fire {
constructor() {
this.init()
this.checkAuth()
}
init = () => {
if(!firebase.apps.length){
firebase.initializeApp({
apiKey: "AIzaSyDrQM977O7YN-Ee4xZWJN0fIdUvVQYqYl0",
authDomain: "giftedchatapp-4ac6c.firebaseapp.com",
databaseURL: "https://giftedchatapp-4ac6c-default-rtdb.asia-southeast1.firebasedatabase.app",
projectId: "giftedchatapp-4ac6c",
storageBucket: "giftedchatapp-4ac6c.appspot.com",
messagingSenderId: "161535509950",
appId: "1:161535509950:web:2391cd3e0aa8481a9da5ef",
measurementId: "G-DJV9RF8VSH"
});
}
};
checkAuth = () => {
firebase.auth().onAuthStateChanged(user => {
if (!user) {
firebase.auth().signInAnonymously();
}
});
};
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();