|
| 1 | +# Copyright 2018 Google LLC |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the 'License'); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an 'AS IS' BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +# [START functions_firebase_rtdb] |
| 16 | +# [START functions_firebase_firestore] |
| 17 | +# [START functions_firebase_auth] |
| 18 | +import json |
| 19 | +# [END functions_firebase_rtdb] |
| 20 | +# [END functions_firebase_firestore] |
| 21 | +# [END functions_firebase_auth] |
| 22 | + |
| 23 | + |
| 24 | +# [START functions_firebase_rtdb] |
| 25 | +def hello_rtdb(data, context): |
| 26 | + """ Triggered by a change to a Firebase RTDB reference. |
| 27 | + Args: |
| 28 | + data (dict): The event payload. |
| 29 | + context (google.cloud.functions.Context): Metadata for the event. |
| 30 | + """ |
| 31 | + trigger_resource = context.resource |
| 32 | + |
| 33 | + print('Function triggered by change to: %s' % trigger_resource) |
| 34 | + print('Admin?: %s' % data.get("admin", False)) |
| 35 | + print('Delta:') |
| 36 | + print(json.dumps(data["delta"])) |
| 37 | +# [END functions_firebase_rtdb] |
| 38 | + |
| 39 | + |
| 40 | +# [START functions_firebase_firestore] |
| 41 | +def hello_firestore(data, context): |
| 42 | + """ Triggered by a change to a Firestore document. |
| 43 | + Args: |
| 44 | + data (dict): The event payload. |
| 45 | + context (google.cloud.functions.Context): Metadata for the event. |
| 46 | + """ |
| 47 | + trigger_resource = context.resource |
| 48 | + |
| 49 | + print('Function triggered by change to: %s' % trigger_resource) |
| 50 | + |
| 51 | + print('\nOld value:') |
| 52 | + print(json.dumps(data["oldValue"])) |
| 53 | + |
| 54 | + print('\nNew value:') |
| 55 | + print(json.dumps(data["value"])) |
| 56 | +# [END functions_firebase_firestore] |
| 57 | + |
| 58 | + |
| 59 | +# [START functions_firebase_auth] |
| 60 | +def hello_auth(data, context): |
| 61 | + """ Triggered by creation or deletion of a Firebase Auth user object. |
| 62 | + Args: |
| 63 | + data (dict): The event payload. |
| 64 | + context (google.cloud.functions.Context): Metadata for the event. |
| 65 | + """ |
| 66 | + print('Function triggered by creation/deletion of user: %s' % data["uid"]) |
| 67 | + print('Created at: %s' % data["metadata"]["createdAt"]) |
| 68 | + |
| 69 | + if 'email' in data: |
| 70 | + print('Email: %s' % data["email"]) |
| 71 | +# [END functions_firebase_auth] |
0 commit comments