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

I am using the Discourse JS User Object to check upon the current user’s groups they are “in”.

For some users, the groups array in the User’s JS Object returns a proper array of objects, each describing a group this user is part of. Example:

groups: Array (5)
0 {
  id: 1, 
  name: "admins", 
  has_messages: false
1 {
  id: 46, 
  name: "custom-group", 
  has_messages: false
2 {
  id: 3, 
  name: "staff", 
  has_messages: false
3 {
  id: 10, 
  name: "trust_level_0", 
  has_messages: false
4 {
  id: 11, 
  name: "trust_level_1", 
  has_messages: false

However, for most users, the groups array is simply empty (0)
I cannot figure out why this would be the case, since those users are properly member of groups.
I also checked if it is related to the “primary group”, however, the users that have a groups array are also not in any primary group, so this is not the issue.

I am using a WP SSO Connection to handle the user logins, so they do log in on a WP Instance, and the Discourse instance basically is a “slave” of the WP, meaning Discourse pulls all user data from WP, and the login is handled in WP
I made sure the “faulty” user has properly logged in at least once in WP and went at least once to Discourse so it is logged in, and I can confirm the user gets added to the right groups - it is just not visible in the groups array in the User JS Object

Can anyone help me spot why this happens, and how I can fix it ?
I “desperately” need the group of which the user is member of in that array :slight_smile:

BTW if it is important, this is how I fetch the current user JS Object - works great so far unless the groups issue affecting some users (most, actually).

$(document).ready(function() {
        type: 'GET',
        cache: false,
        url: 'https://discourse.domain.tld/session/current.json',
        dataType: 'json',
        xhrFields: {
            'withCredentials': true
        success: function(result){
            var current_user_groups = result.current_user.groups;
            console.log(current_user_groups);// This returns empty for Many users.
            console.log(result.current_user);// All data in here is properly populated for ALL users, unless the GROUPS array.
        error: function(result){
            console.log('Error: '+result);

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 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.


Really? I don’t think it should be so complicated?

This is from await $.ajax("/u/Lhc_fl.json")
Obviously I’m not a staff member of Meta, but I can see my “automatic” groups


@Lhc_fl, so what you did is get the current user’s username (I suppose with the same AJAX as I use?), then another AJAX to that user JSON, and from there the group?

I like as well @angus’ approach - my code though is on the discourse side… I could just use the same API key and authenticate with that?
I had presumed, that since I use withCredentials… the code runs only when a user is authenticated, and that the data would always be the same.

I am not sure why the data is not the same, is this a security concern to communicate what group a user is in? It’s not that we communicate the actual group data, or other users in that group, as far I see.

In any case, venturing into the suggested approaches now…


If you are developing components for discourse… I think the easiest way is to use discourse’s api. If you are developing a client, I think the user name should have been known long ago?

For the former, here is an example;

require("discourse/lib/plugin-api").withPluginApi("1.0.0", (api)=>{console.log(api.currentUser())});

You should be able to access the current user object and that will show you what groups the user knows that they are a member of. It’s already loaded for every user, so you’ll access the plugin api like any other theme that looks at the current user. There are examples of your go through Theme Developer Quick Reference Guide and look around.

This topic that is a recommended topic for this one has an example: currentUser.groups doesn't match group_users table

1 Like

Thanks all

While on this particular instance the groups still remains empty (even with the api of the user instead of the current session), I was able to achieve this by checking on the group ID in the group_users which as well stores the same data, just not with name

I did use my original approach to get the data as this is “just” a snippet added in Discourse as a head script, no theme or plugin.
I couldn’t get the actual api to work, but the AJAX approach works just as fine for what I needed it.

Thanks again all for the inputs, specially @Lhc_fl whose approach led me to the right solution for my case.


This topic was automatically closed after 11 days. New replies are no longer allowed.