|
| 1 | +# Firebase SDK for Cloud Functions Quickstart - Firestore with auth context |
| 2 | + |
| 3 | +This quickstart demonstrates using the **Firebase SDK for Cloud Functions** with |
| 4 | +**Firestore** with authentication context. |
| 5 | + |
| 6 | +## Introduction |
| 7 | + |
| 8 | +This sample app adds auth metadata to all documents written to a collection. |
| 9 | + |
| 10 | +## Set up the sample |
| 11 | + |
| 12 | +Before you can test the functions locally or deploy to a Firebase project, |
| 13 | +you'll need to run `npm install` in the `functions` directory. |
| 14 | + |
| 15 | +## Run locally with the Firebase Emulator suite |
| 16 | + |
| 17 | +The |
| 18 | +[Firebase Local Emulator Suite](https://firebase.google.com/docs/emulator-suite) |
| 19 | +allows you to build and test apps on your local machine instead of deploying to |
| 20 | +a Firebase project. |
| 21 | + |
| 22 | +1. Create a Firebase project in the |
| 23 | + [Firebase Console](https://console.firebase.google.com) |
| 24 | + > _Wondering why this step is needed?_ Even though the emulator will run this |
| 25 | + > sample on your local machine, it needs to interact with a Firebase project |
| 26 | + > to retrieve some configuration values. |
| 27 | +1. [Set up or update the Firebase CLI](https://firebase.google.com/docs/cli#setup_update_cli) |
| 28 | +1. Run `firebase emulators:start` |
| 29 | +1. Open the Emulator Suite UI |
| 30 | + 1. Look in the output of the `firebase emulators:start` command for the URL |
| 31 | + of the Emulator Suite UI. It defaults to |
| 32 | + [localhost:4000](http://localhost:4000), but may be hosted on a different |
| 33 | + port on your machine. |
| 34 | + 1. Enter that URL in your browser to open the UI. |
| 35 | +1. Trigger the functions |
| 36 | + 1. Look in the output of the `firebase emulators:start` command for the URL |
| 37 | + of the http function "verifyComment". It will look similar to: |
| 38 | + `http://localhost:5001/MY_PROJECT/us-central1/verifyComment` |
| 39 | + 1. `MY_PROJECT` will be replaced with your project ID |
| 40 | + 1. The port may be different on your local machine |
| 41 | + 1. Create a new document in the `comments` collection in Firestore in the emulator UI. |
| 42 | +1. View the effects of the functions in the Emulator Suite UI |
| 43 | + |
| 44 | + 1. In the "Logs" tab, you should see new logs indicating that the functions |
| 45 | + "verifyComment" and "makeuppercase" ran: |
| 46 | + |
| 47 | + > `functions: Beginning execution of "verifyComment"` |
| 48 | +
|
| 49 | + 1. In the "Firestore" tab, you should see the document containing your original |
| 50 | + message updated to include auth context. |
| 51 | + |
| 52 | +## Deploy and test on a live Firebase project |
| 53 | + |
| 54 | +To deploy and test the sample: |
| 55 | + |
| 56 | +1. Create a Firebase project on the |
| 57 | + [Firebase Console](https://console.firebase.google.com) |
| 58 | +1. Deploy your project's code using `firebase deploy` |
| 59 | +1. Create a new document in the `comments` collection in Firestore in the Firebase console. |
| 60 | + |
| 61 | +You should see the document containing your original message updated to include auth context. |
| 62 | + |
| 63 | +## Contributing |
| 64 | + |
| 65 | +We'd love that you contribute to the project. Before doing so please read our |
| 66 | +[Contributor guide](../../CONTRIBUTING.md). |
| 67 | + |
| 68 | +## License |
| 69 | + |
| 70 | +© Google, 2023. Licensed under an [Apache-2](../../LICENSE) license. |
0 commit comments