-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFire.js
120 lines (102 loc) · 2.61 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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
import firebase from "firebase/compat/app";
import "firebase/compat/auth";
import "firebase/compat/firestore";
var firebaseConfig = {
apiKey: "AIzaSyDq89xqcvWh-tcr115gt7O35XHia_MZjS0",
authDomain: "socialapplication-10a5c.firebaseapp.com",
projectId: "socialapplication-10a5c",
storageBucket: "socialapplication-10a5c.appspot.com",
messagingSenderId: "901578680890",
appId: "1:901578680890:web:452fae6a7b3c0622c1e303",
};
// Initialize Firebase
class Fire {
constructor() {
firebase.initializeApp(firebaseConfig);
}
addPost = async ({ text, localUri }) => {
const remoteUri = await this.uploadPhotoAsync(
localUri,
`photos/${this.uid}/${Date.now()}`
);
return new Promise((res, rej) => {
console.log("sending: ", {
text,
uid: this.uid,
timestamp: this.timestamp,
image: remoteUri,
});
this.firestore
.collection("posts")
.add({
text,
uid: this.uid,
timestamp: this.timestamp,
image: remoteUri,
})
.then((ref) => {
res(ref);
})
.catch((error) => {
rej(error);
});
});
};
uploadPhotoAsync = async (uri, filename) => {
return new Promise(async (res, rej) => {
const response = await fetch(uri);
const file = await response.blob();
const path = `photos/${this.uid}/${Date.now()}.jpg`;
let upload = firebase.storage().ref(path).put(file);
upload.on(
"state_changed",
(snapshot) => {},
(err) => {
rej(err);
},
async () => {
const url = await upload.snapshot.ref.getDownloadURL();
res(url);
}
);
});
};
createUser = async (user) => {
let remoteUri = null;
try {
await firebase
.auth()
.createUserWithEmailAndPassword(user.email, user.password);
let db = this.firestore.collection("users").doc(this.uid);
db.set({
name: user.name,
email: user.email,
avatar: null,
});
if (user.avatar) {
remoteUri = await this.uploadPhotoAsync(
user.avatar,
`avatars/${this.uid}`
);
db.set({ avatar: remoteUri }, { merge: true });
}
} catch (error) {
console.error(error);
alert("Error: ", error);
}
};
signOut = () => {
firebase.auth().signOut();
};
get firestore() {
return firebase.firestore();
}
get uid() {
return (firebase.auth().currentUser || {}).uid;
}
get timestamp() {
return Date.now();
}
}
Fire.shared = new Fire();
export default Fire;