-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfireCinnection.js
81 lines (67 loc) · 1.91 KB
/
fireCinnection.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 app from 'firebase/app';
import 'firebase/database';
import 'firebase/auth';
import 'firebase/storage';
let firebaseConfig = {
apiKey: process.env.NEXT_PUBLIC_APIKEY,
authDomain: process.env.NEXT_PUBLIC_AUTHDOMAIN,
databaseURL: process.env.NEXT_PUBLIC_DATABASE_URL,
projectId: process.env.NEXT_PUBLIC_PROJECTID,
storageBucket: process.env.NEXT_PUBLIC_STORAGE_BUCKET,
messagingSenderId: process.env.NEXT_PUBLIC_MESSAGING_SENDER_ID,
appId: process.env.NEXT_PUBLIC_APP_ID,
measurementId: process.env.NEXT_PUBLIC_MEASUREMENTID
};
class Firebase {
constructor() {
if (!app.apps.length) {
app.initializeApp(firebaseConfig);
} else {
app.app(); // if already initialized, use that one
}
this.app = app.database();
this.storage = app.storage();
}
login(email, password) {
return app.auth().signInWithEmailAndPassword(email, password)
}
logout = async () => {
await app.auth().signOut()
.catch((error) => {
console.log(error)
});
localStorage.clear();
}
isInitialized() {
return new Promise(resolve => {
app.auth().onAuthStateChanged(resolve);
})
}
getCurrent() {
return app.auth().currentUser && app.auth().currentUser.email
}
getCurrentUid() {
return app.auth().currentUser && app.auth().currentUser.uid
}
async getUserName(callback) {
if (!app.auth().currentUser) {
return null;
}
const uid = app.auth().currentUser.uid;
await app.database().ref('usuarios').child(uid)
.once('value').then(callback);
}
async getPost(id, callback) {
await app.database().ref('posts').child(id)
.once('value').then(callback).catch(error => {
return null
});
}
async deletePost(id) {
await app.database().ref('posts').child(id).remove();
}
async editPost(id, data) {
await app.database().ref('posts').child(id).set(data);
}
}
export default new Firebase();