Is there a way we can access the external_id from SSO record of another user?

AI-generated summary

The discussion revolves around accessing the external_id from an SSO record of another user, not the current logged-in user. junbetterway explains that they can access the external_id for the current user using api.getCurrentUser().external_id, but need a solution for accessing it when viewing another user’s profile. pfaffman suggests creating a plugin that adds the external_id to the user serializer, while simon refines this idea, noting that external_id is a property of the single_sign_on_record, not the user. junbetterway ultimately resolves the issue by using custom user fields that are synced from their application during login.

We have no issue accessing the external_id from SSO record of the current logged-in user using below approach:

api.getCurrentUser().external_id

However, we have some buttons/links when the current logged-in user is viewing another one’s profile summary (e.g., Person X). This external_id is the primary identifier used in our website which we will be using for some redirects and actions from our website. Using the admin user API works but obviously not for non-admin user accounts.

Is this possible and how?

I think you want a plugin that adds the external_id to the user serializer.

Something like:

  add_to_serializer(:user, :external_id) do
     object.external_id
  end

I think it would be something like:

# in the plugin's after_initialize block
after_initialize do

# Edit: probably also check to make sure that `SiteSetting.enable_discourse_connect` returns `true`
  add_to_serializer(:user, :external_id) {object&.single_sign_on_record&.external_id}
end

The external_id is a property of the single_sign_on_record not the user. I remember this because I had something to do with:

Thanks everyone for the inputs, we ended up doing this via custom user fields which gets synced from our application during login.