Skip to content

Commit 4e4247a

Browse files
author
Ace Nassri
authored
Node 6: add Firebase snippets (GoogleCloudPlatform#697)
* Firebase snippets: initial commit * Remove analytics sample
1 parent 0c9f58b commit 4e4247a

1 file changed

Lines changed: 63 additions & 0 deletions

File tree

functions/firebase/index.js

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
// [START functions_firebase_rtdb]
2+
/**
3+
* Triggered by a change to a Firebase RTDB reference.
4+
*
5+
* @param {!Object} event The Cloud Functions event.
6+
* @param {!Function} The callback function.
7+
*/
8+
exports.helloRTDB = (event, callback) => {
9+
const triggerResource = event.resource;
10+
11+
console.log(`Function triggered by change to: ${triggerResource}`);
12+
console.log(`Admin?: ${!!event.auth.admin}`);
13+
console.log(`Delta:`);
14+
console.log(JSON.stringify(event.delta, null, 2));
15+
16+
// Don't forget to call the callback.
17+
callback();
18+
};
19+
// [END functions_firebase_rtdb]
20+
21+
// [START functions_firebase_firestore]
22+
/**
23+
* Triggered by a change to a Firestore document.
24+
*
25+
* @param {!Object} event The Cloud Functions event.
26+
* @param {!Function} The callback function.
27+
*/
28+
exports.helloFirestore = (event, callback) => {
29+
const triggerResource = event.resource;
30+
31+
// We're just going to log the resource string and the
32+
// full event to prove that it worked.
33+
console.log(`Function triggered by change to: ${triggerResource}`);
34+
console.log(JSON.stringify(event));
35+
36+
// Don't forget to call the callback.
37+
callback();
38+
};
39+
// [END functions_firebase_firestore]
40+
41+
// [START functions_firebase_auth]
42+
/**
43+
* Triggered by a change to a Firebase Auth user object.
44+
*
45+
* @param {!Object} event The Cloud Functions event.
46+
* @param {!Function} The callback function.
47+
*/
48+
exports.helloAuth = (event, callback) => {
49+
try {
50+
const data = event.data;
51+
console.log(`Function triggered by change to user: ${data.uid}`);
52+
console.log(`Created at: ${data.metadata.createdAt}`);
53+
54+
if (event.data.email) {
55+
console.log(`Email: ${data.email}`);
56+
}
57+
} catch (err) {
58+
console.error(err);
59+
}
60+
// Don't forget to call the callback.
61+
callback();
62+
};
63+
// [END functions_firebase_auth]

0 commit comments

Comments
 (0)