8

In express you can add middleware such as app.use(cors()) which adds it to all of the endpoints, however I can't find something similar in firebase examples. Here is the example (see below) of how to apply it in every function. However I want to apply the middleware (cors or other) globally, as I have many functions.

import * as cors from 'cors';
const corsHandler = cors({origin: true});

export const exampleFunction= functions.https.onRequest(async (request, response) => {
       corsHandler(request, response, () => { return handler(req, res) });
});

What is the equivalent of app.use() in firebase? Is adding and express server the only option?

Kevin Danikowski
  • 4,620
  • 6
  • 41
  • 75
  • Here is an official firebase functions sample that shows using express to handle the middleware. Export the express app and the function is public. https://github.com/firebase/functions-samples/blob/main/authorized-https-endpoint/functions/index.js – IcyIcicle May 31 '22 at 22:37
  • @IcyIcicle sorry, that link is if you use an express service, not if you export the function directly. If it were to do the express service, there wouldn't be a need for this question – Kevin Danikowski Jun 05 '22 at 14:43

2 Answers2

12

Use currying to create a handler, you have to repeat it across all the functions, but it's easier than writing the middleware each time:

const applyMiddleware = handler => (req, res) => {
  return cors(req, res, () => {
    return handler(req, res)
  })
}
exports.handler = functions.https.onRequest(applyMiddleware(yourHandler))

Edit, an example of a more complex middleware:

const applyMiddleware =
  (handler, { authenticatedRoute = false } = {}) =>
  (req, res) => {
    if (authenticatedRoute) {
      const isAuthorized = isAuthenticated(req)
      if (!isAuthorized) {
        return res.status(401).send('Unauthorized')
      }
    }
    return cors(req, res, () => {
      return handler(req, res)
    })
  }
exports.handler = functions.https.onRequest(
   applyMiddleware(yourHandler, { authenticatedRoute: true })
)
Kevin Danikowski
  • 4,620
  • 6
  • 41
  • 75
  • I got this error `ReferenceError: handler is not defined` and I fixed with https://gist.github.com/jrichardsz/691e6194fd0499649158bcbfd9075428#file-firebase-functions-add-cors-md – JRichardsz Nov 07 '22 at 20:41
  • The handler is your own handler which is why it's left out actually – Kevin Danikowski Nov 09 '22 at 16:58
  • In the question you have ES6 with the import cors statement and export const exampleFunction. Please add these to the answer too (the answer uses exports.handler so is inconsistent with the question). Thanks! Then do we need to do anything with {origin: true} using the answer? – Ian Engelbrecht Jan 11 '23 at 14:37
  • Following from above, see https://stackoverflow.com/a/50992145/3210158 – Ian Engelbrecht Jan 11 '23 at 14:48
  • I updated the `handler` to `yourHandler` to add clarity. this question isn't about cors functionality, just having a middleware wrapper, so I wrote it cleanly for that. The code in the question isn't that great – Kevin Danikowski Jan 11 '23 at 23:18
0
import cors from 'cors'

const corsHandler = cors({origin: true});
const applyCORS = handler => (req, res) => {
  return corsHandler(req, res, _ => {
    return handler(req, res)
  })
}

export const firebasefunc = functions.https.onRequest(applyCORS(myhandler))