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
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() {
$.ajax({
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);
}
});
});