MongoDB.live, free & fully virtual. June 9th - 10th. Register Now

Setting function’s custom execution user does work

Hi, In the docs it is written that it shall be possible to use specific user id or a run a script to determine user id before calling a function: https://docs.mongodb.com/stitch/functions/define-a-function/

It does not seem to work - at least for triggers and for “test” runs.

Example:

  1. Create a function that always authenticate using a specific use id or script. Inside the function log the “context.user”.
  2. Try with a simple test in the console. The context executes as “system”. Failed.
  3. Create a scheduled trigger that calls this function (for example every minute). Don’t forget to deploy.
  4. Check the logs. In the log files it’s always executes as “system”. Failed

Am I doing something wrong ?

Hi Dimitar –

Triggers aren’t called by a specific user, and therefore will always run as “System”. As for the console, it will default to using the System User. However, you can “Change User” in the UI and run as any user within your Stitch Authentication. Have you tried this?

Hi Drew,

thanks for you reply. Yes I tried and it works, executing the function as a different user using “Change User”.

What confuses me is the following.

According to the docs and Stitch UI, I shall be able to choose how to authenticate a function. For example, this is what the docs state for User Id - “This type of authentication configures a function to always run as a specific application user”.
Why can I set this if it is not used? Maybe more info in the docs will be good.

To clarify why I need this to work:
My use case is that I would like periodically to send emails to my users. For that I need their email addresses. The email address of a user is stored in the internal stitch database to which I don’t have direct access (except with Stitch API). I was hoping that I can use the “Script” authentication method when I run my “sendEmail” function. The script should get the parameters of the “sendEmail” function and return the user id to which I want to send the email to. Does this make any sense ? :slight_smile:

Thanks again!

Appreciate the feedback, we’ll look into clarifying this in documentation. Since Triggers don’t use Authentication – they are just backend functions that respond to set events – they don’t have an associated user or use authentication.

Specifically, for the case that you mention, you could use the Stitch Admin API or keep information about the Stitch user in a separate collection.

1 Like