This sample shows how to authenticate access to a JSON API to only allow access to data for a specific Firebase user.
Only users who pass a valid Firebase ID token as a Bearer token in the
Authorization
header of the HTTP request are authorized to use the API.
This sample comes with a web-based API explorer UI whose code is in the public directory. It lets you sign in to Firebase with your Google account, and create messages whose sentiments are detected by the Cloud Natural Language API.
- Create a Firebase Project using the Firebase Console.
- Enable the Google Provider in the Auth section.
- Enable Billing on your project (to connect to the Natural Language API) by switching to the Blaze plan.
- Clone or download this repo and open the
authenticated-json-api
directory. - You must have the Firebase CLI installed. If you don't have it install it with
npm install -g firebase-tools
and then configure it withfirebase login
. - Configure the CLI locally by using
firebase use --add
and select your project in the list. - Install dependencies locally by running:
cd functions; npm install; cd -
- Enable the Google Cloud Natural Language API
This sample comes with a web-based UI for testing the function. To test locally do:
- Set up admin credentials so that the emulator can talk to the Natural Language API
- Start serving your project locally using
firebase serve --only hosting,functions
- Open the app in a browser at
https://localhost:5000
. - Sign in to the web app in the browser using Google Sign-In
- Create messages and explore them using the List and Detail sections.
- Sign out. You should no longer be able to access the API.
To deploy and test on prod do:
- Deploy your project using
firebase deploy
- Open the app using
firebase open hosting:site
, this will open a browser. - Sign in to the web app in the browser using Google Sign-In
- Create messages and explore them using the List and Detail sections.
- Sign out. You should no longer be able to access the API.
We'd love that you contribute to the project. Before doing so please read our Contributor guide.
© Google, 2017. Licensed under an Apache-2 license.