Multi tenant implementation

@matthewtalma I must apologize! I clearly misunderstood you about the missing tenant header. I’ve just encountered the same problem you described :slight_smile:

I’ve just implemented a work around. It might not be the best solutions, but it seems to be doing the trick for now.

In case you’re interested, in my examples above, I had my validateTenant method as a requirement ahead of all my method calls (including the beforeFind, etc calls for the cloud functions).

Long story short, I’ve created a preFlight utility that does the tenant validation and straight after it also sets the headers again, seeing as they get lost after this point for internal calls.

Again, maybe not the most elegant, but it’s working.

Here’s my preFlight method

export const preFlight = async request => {
  await validateTenant(request)
  if (request.headers['x-tenant']) {
    Parse.CoreManager.set('REQUEST_HEADERS', {
      'x-tenant': request.headers['x-tenant']
    })
  }
}

And it works in the the standard beforeFind hooks:

Parse.Cloud.beforeFind('Page', async req => {
  await preFlight(req)
})

and also in my own code - this (the internal Site query) happens to be where I encountered the same issue as you:

export const searchModel = async (request: CloudRequest): Promise<CloudResponse> => {
  await utils.preFlight(request)

  if (request?.params?.filter?.site) {
    const site = await new Parse.Query('Site').equalTo('objectId', request.params.filter.site).first()
    request.params.filter.site = site
  }

  return {
    data: {
      ...(await utils.searchModel(request.params))
    }
  }
}

I hope this might be of some help to you (or anyone else that might encounter similar issues).

Sorry again for misunderstanding you :smiley:

Cheers