The discussion revolves around accessing a user’s external authentication ID via the Javascript API. gormus is able to retrieve the ID using the Admin API but not with the Javascript API. simon explains that a hidden site setting, include_associated_account_ids, needs to be enabled for the associated_account_ids to be returned from the CurrentUserSerializer. However, this setting cannot be enabled by users on hosted instances. gormus confirms this limitation, and simon suggests that users on hosted instances should contact Discourse’s support team to enable the setting.
When I consume API directly, I can access user’s external authentication ID without any problem:
GET /admin/users/{id}.json
However, when I try using the Javascript API, I cannot get the same data for the current user. In fact, the currentUser doesn’t return any data for external IDs:
import { apiInitializer } from "discourse/lib/api";
export default apiInitializer("1.5.0", api => {
const currentUser = api.getCurrentUser();
if (currentUser) {
const userOAuthID = currentUser?.external_ids?.oauth2_basic;
console.log("userOAuthID:", userOAuthID); // null
}
});
How could I access currentUser’s external ID via javascript?
The Context
I’m trying to push current user’s external ID to window.dataLayer.
It looks like you are trying to get the user’s user_associated_account.provider_uid. From looking at the code, there is a hidden site setting that you need to enable to cause the associated_account_ids to be returned from the CurrentUserSerializer: