Warm tip: This article is reproduced from serverfault.com, please click

can firebase callable functions be used to build an API?

发布于 2020-11-30 05:35:07

I know that the difference between firebase's cloud callable functions and http functions have been clarified here. However, I still find myself confused in terms of the specific use case of building an API for CRUD operations to the firestore.

I know how to do this with http functions and express, but http functions don't pass a context with user tokens like how callable functions does.

Can/how do you create a router similar to how one would with express with http functions (see below) that allow you to create an API that can handle GET, POST, PUT, and DELETE requests for public and private routes with middleware all within one callable function? Or would you need to create a new callable function for each "route"?

// Define Routes
app.use('/api/users', require('./routes/users'));
app.use('/api/auth', require('./routes/auth'));
app.use('/api/contacts', require('./routes/contacts'));

Can you also hook up the callable function to be called from firebase hosting on particular routes like http functions can (see below)?

"hosting": {
    "public": "client/build",
    "rewrites": [
      {
        "source": "/api{,/**}",
        "function": "api"
      },
      {
        "source": "**",
        "destination": "/index.html"
      }
    ]
  },

Or would it just be better to stick with passing an express app through http functions and authenticating by just passing the user token in the request headers?

Questioner
josh
Viewed
0
Rafael Lemos 2020-12-02 00:33:29

After checking this Community Answer I saw that this is not possible, as you can see:

HTTP requests to callable functions don't really come "from" a URL. They come from anywhere on the internet. It could be a web site, Android or iOS app, or someone who simply knows the protocol to call the function.

So unless you workaround that by sending the URL in the data of the callable function, it will not work. And even if you do, it just would go against the principle of callable functions, so I would recommend that you use Http Functions for that purpose.