How to protect firebase Cloud Function HTTP endpoint to allow only Firebase authenticated users?

spaceMonkey picture spaceMonkey · Mar 12, 2017 · Viewed 66.2k times · Source

With the new firebase cloud function I've decided to move some of my HTTP endpoint to firebase. Everything works great... But i have the following issue. I have two endpoints build by HTTP Triggers (Cloud Functions)

  1. An API endpoint to create users and returns the custom Token generated by Firebase Admin SDK.
  2. An API endpoint to fetch certain user details.

While the first endpoint is fine, but for my second end point i would want to protect it for authenticated users only. meaning someone who has the token i generated earlier.

How do i go about solving this?

I know we can get the Header parameters in the cloud function using

request.get('x-myheader')

but is there a way to protect the endpoint just like protecting the real time data base?

Answer

Doug Stevenson picture Doug Stevenson · Mar 12, 2017

There is an official code sample for what you're trying to do. What it illustrates is how to set up your HTTPS function to require an Authorization header with the token that the client received during authentication. The function uses the firebase-admin library to verify the token.

Also, you can use "callable functions" to make a lot of this boilerplate easier, if your app is able to use Firebase client libraries.