-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathfirebase.js
More file actions
88 lines (82 loc) · 2.66 KB
/
firebase.js
File metadata and controls
88 lines (82 loc) · 2.66 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
78
79
80
81
82
83
84
85
86
87
88
var firebaseConfig = {
apiKey: 'AIzaSyCUVezS4HduQ628WeiTxhTgJK7Y5VLdBuM',
authDomain: 'scrap-n-stick.firebaseapp.com',
projectId: 'scrap-n-stick',
storageBucket: 'scrap-n-stick.appspot.com',
messagingSenderId: '994124947691',
appId: '1:994124947691:web:277ef9322e1339b45a5eae',
measurementId: 'G-7SZRMD0C03',
};
// firestore initialization
firebase.initializeApp(firebaseConfig);
const firestore = firebase.firestore();
const notesRef = firestore.collection('notes');
// auth initilization
const provider = new firebase.auth.GoogleAuthProvider();
provider.setCustomParameters({
prompt: 'select_account',
});
export const setDataFirebase = async (note) => {
const newDoc = notesRef.doc();
await newDoc.set({ ...note, id: newDoc.id });
note.id = newDoc.id;
};
export const updateDataFirebase = async (note) => {
const newDoc = notesRef.doc(note.id);
await newDoc.set({ ...note });
//note.id = newDoc.id;
};
export const deleteDataFirebase = async (id) => {
await notesRef.doc(id).delete();
};
const glogin = async () => {
const result = await firebase.auth().signInWithPopup(provider);
console.log(result);
};
export const getCurrentUser = () => {
return firebase.auth().currentUser;
};
export const getDataFirebase = async () => {
const currentUserId = getCurrentUser().uid;
const querySnapshot = await notesRef.where('uid', '==', currentUserId).get();
let data = [];
querySnapshot.forEach((doc) => {
data.push(doc.data());
});
return data;
};
async function logout() {
await firebase.auth().signOut();
}
const clearNotes = async () => {
const currentUserId = getCurrentUser().uid;
const query = notesRef.where('uid', '==', currentUserId);
const querySnapshot = await query.get();
querySnapshot.forEach(async function (doc) {
await doc.ref.delete();
});
};
chrome.runtime.onMessage.addListener((msg, sender, resp) => {
if (msg.command === 'fetch') {
getDataFirebase().then((data) => resp(data));
} else if (msg.command === 'set') {
setDataFirebase(msg.data).then(() => resp());
}else if (msg.command === 'update') {
updateDataFirebase(msg.data).then(() => resp());
} else if (msg.command === 'delete') {
console.log('Delete hora hai');
deleteDataFirebase(msg.id).then(() => resp());
} else if (msg.command == 'signin') {
console.log('firebase tk pahuncha');
glogin().then(() => resp());
} else if (msg.command === 'getCurrentUser') {
const user = getCurrentUser();
resp(user);
} else if (msg.command == 'logout') {
console.log('logout firebase tk pahuncha');
logout().then(() => resp());
} else if (msg.command === 'clear') {
clearNotes().then(() => resp());
}
return true;
});