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: