I opened a PR for this issue - it also solves the same issue for SSO sites
https://github.com/discourse/discourse/pull/8423