Custom JWT Authentication trigger not inserting and cannot debug

Overview

I am currently trying to debug an authentication trigger that runs on user creation. It is supposed to fire a function which inserts a new record into a collection where we store additional data about the user.

Here is an example of the configuration for the trigger:

And the function settings:

And the function:

Problems

  1. I see in my logs that the trigger runs when I login with a new user, however, no insert occurred when I check my collection.

  2. In the log entry for the trigger I do not see any of my console logs. In fact the logs array of the log entry is not present at all.

I can’t tell if my function is even getting fired without being able to log anything. Any help would be greatly appreciated.

1 Like

What is the object user in user.id at line 10, is it defined? Maybe you need to get it from the authEvent object const user = authEvent.user;

Also, I believe you should declare your func async at line 1 exports = async function(authEvent) {.

Thanks for your response Jerome!

Arg sorry, this is a simplified version and I must have removed a statement by accident.

I do have a const { user } = authEvent; right after the logs. So it should be defined, however, should that have an affect on whether those console.logs are reflected in the log entry?

I have tried declaring it async and still no luck.

You have const user = authEvent; instead of const user = authEvent.user; ? Maybe that’s a problem?
I don’t use Custom JWT Authentication myself but Custom Function Authentication.
However I have the exact same trigger setting and function setting as you.

Did you try to remove ALL the code except just a console.log(“hello world”) and see if you get logs or not? If this works, try to debug it until you find the culprit line.

Also when declaring the main function async, you have to use “await” with mongo async function like
const result = await collection.insertOne(newUser);

Another thing is that I don’t understand why you put “insertedId” between brackets? I don’t think collection.insertOne(newUser) returns a document but only a string variable (the id). Maybe try without the brackets.

Thanks again Jerome.

I have const { user } = authEvent;. This is JavaScript de-structuring, you can read more here Destructuring assignment - JavaScript | MDN.

Likewise, collection.insertOne returns
a document with two properties a boolean acknowledged and insertedId with the _id value of the inserted document as per the docs https://docs.mongodb.com/manual/reference/method/db.collection.insertOne/, which I de-structure via const { insertedId } = await collection.insertOne(newUser);

Yes, I have also tried just console logging and the logs don’t show. Additionally, I am using await and still nothing.

One weird thing I’ve realized is, I’ve tried using a trigger on Login event, which tries to do the same thing as the one explained in my original post. This trigger runs successfully (I see logs, data gets inserted) on the 2nd and above logins for a user. So the first time the user logs in, the logs don’t show, and the custom data about the user does not get inserted. However, if I sign out and sign back in with the same user, it runs as expected.

I think I’ll just have to implement without triggers at this point.