User JS Object `groups` array not populated for several users

You’re using the session endpoint for the currently logged in user (i.e. session/current). Only staff members will get the “automatic” groups (e.g. trust level groups) from that endpoint.

For an example of that check out https://meta.discourse.org/g. It’s not the same endpoint, but the same principle applies. You’ll see that you can’t see all the trust level groups on meta, because you’re not a staff member on this forum.

You’ll need to use an authenticated request, i.e. with an API key, to get all of a user’s groups, including automatic groups. If this javascript is running on Wordpress you can use the WP Discourse utility functions to do this. It’ll make a request using the API key you’ve supplied in the admin interface.