vir us
vir us

Reputation: 10717

Firebase callable + express?

With functions.https.onRequest(app); it was possible to use express right away.

I'm wondering if it's possible to use functions.https.onCall(...) together with express in the same way?

onCall(...) seem to have a different signature but maybe there is still a way to keep using express while working with onCall(...) functions?

Upvotes: 9

Views: 2146

Answers (3)

Kalani Bright
Kalani Bright

Reputation: 129

Not WITH express but they can call the same functions... they BOTH can exist as root...and express like whatever callable function exist at root (exports)...

So they can coexists...they also can call common functionality...such as a service layer...

Upvotes: 0

fabio moretti
fabio moretti

Reputation: 47

What does work however is using onRequest and calling that... then you can use express like normal and have the simplicity of firebase callable on the client side...

then you can do your authorization like normal. For example with the following middleware:

export const createFirebaseAuth = () => (req: express.Request, res: express.Response, next: express.NextFunction) => {
    console.log('Time: ', Date.now());
    const token = req.header('Authorization');
    if (!token) {
      res.status(400);
      res.send('Authtoken must be sent with a request');
      return;
    }
    admin
      .auth()
      .verifyIdToken(token.replace('Bearer ', ''))
      .then((v) => {
        req.user = v;
        next();
      })
      .catch((error) => {
        res.status(401);
        res.send(error.message);
        res.end();
      });
  }

Upvotes: 0

Doug Stevenson
Doug Stevenson

Reputation: 317362

No, it's not possible. Callable functions force your endpoint to use a certain path, a certain type of input (JSON via POST) and a certain type of output (also JSON). Express wouldn't really help you out, given the constraints of how callables work. You can read about all the callable protocol details in the documentation. You can see that callables abstract away all of the details of the request and response, which you would normally work with when using Express.

Upvotes: 14

Related Questions