Fixed the original issue you reported via:
https://github.com/discourse/discourse/commit/91519fdfe7f8665999eac290e872879703fcaca8
The second SSO login issue you reported is unrelated to the original issue, and I can repro it on my dev instance. I have a fix for it, but need to test it thoroughly.