Create a user-specific dashboard Questions

Hi community,

I am new to Mongo Charts and recently built a demo dashboard , but having questions regarding the user setting.

I am currently using JWT authentication method to embed the dashboard in my nodejs application.
However, I want to create a user “filter” , which means each user can only see their own data.

I tried to refer to the documentation about the Injected User Filter , by using email to define each user,however, I am having errors 17. I wonder what is the correct setting / any demo code to use the user filter.

Please advise.

Hi @Super_Chain -

It sounds like you’re on the right track. As per the docs, error 17 means “injected filter failed to apply”. Can you share your filter function code? It should interrogate the context.token object and return a JavaScript object representing a valid MQL filter.

For example, if the user’s email address is in the sub field of the JWT token and you want to filter based on the ownerId field in your collection, the function would look like this:

function getFilter(context) {
  return { ownerId: context.token.sub };
}

Tom

2 Likes

Hey Tom !!!

Thanks for your reply !!!

Just fixed it ! Thanks for explaining the “sub” field, I wasnt know it was referring to the JWT token. I decoded the JWT token by putting the token in jwt.io and found that our payload is not using “sub” , instead using “username” , so after changing that it works perfectly !!!

Cheers!

1 Like

Hi Tom,

I am using having an new error when I deploy the application :slight_smile…

Without changing the node.js code, I found that the dashboard is no longer showing to every users, like this : (suppose when the user login, they can see the numbers for their own data)

I checked the network tab and seems every requests are fine with all 200

So, I also check to use postman to request the URL with the authorisation as the Bearer Token and there is 404 error - Cannot access member ‘text’ of undefined

Here is the setting for Inject Filter Per User :

I use username instead of sub, as based on the jwt decoding the Bearer token :

PAYLOAD:DATA
{
“username”: “test@cat.com”,
“iat”: 1656477038,
“exp”: 1656480638
}

Trying to change the setup on MongoDB , but no luck so far …

Could you please shed light on the direction , what aspect should I look at for debug ?

Cheers !

Hi - so it looks like the charts aren’t showing any error, but they don’t contain any data. The most common cause for this is because all data is being filtered out by your filter function.

From the information provided it’s hard to tell exactly what’s going wrong, but I’d guess that either the token or the data isn’t what you expect. One good step to debug would be to change your function so it uses a constant value, e.g:

return { ownerId: "test@cat.com" };

This will result in the same filter for all users which clearly isn’t what want, but depending on whether it renders a chart or not you may get some clues as to the nature of the problem. I take it that ownerId is the field you want to match in your data?

Tom

1 Like

Oh Tom ! Lifesaver !

I thought it was a default setting of using “ownerId”. Turns out this is exactly how we query the data normally. Just like {"email":"test@cat.com}

I got the correct return, based on your advice , here is the filter should be looks like

// Return a filter based on token attributes, e.g:
return { email: context.token.username };

2 Likes

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.