How to return 403 when user is not set in cloud function request?

Hello there!

I would like to write a cloud function that throws an error and a 403 http response code when the request.user is not defined for example.

For now I’m jus throwing an Error but the HTTP code is 400 and I do not really like this.

How can I achieve this?

Thank you!

2 Likes

I believe something like this should work:

const error = new Error();
    error.status = 403;
    error.message = 'unauthorized';
    throw error;

Thanks @davimacedo but response status is still 400 on Postman :frowning:

The closest I got to what you want is this:

throw new Parse.Error(403, 'unauthorized');

But it gives http 200 and an object with code “403”. In Parse JS docs I can’t see any way to do that as well, which is pretty disappointing.

I think it should be possible to let the developer determine the http response code to return in a Cloud Function.

If you feel like open an issue in the Parse Server repo and possibly look into how this can be achieved in code, I’ll be happy to support.