That is great!
Normally what would be done is that code similar to the SSO code you have linked to would be installed on the service that users initially log into. In your case that service would be Teachable. Unfortunately, I do not think it is possible to add server side code to a Teachable site.
Have a look at this post to see how you can integrate Discourse with Teachable by sending Discourse invitations to users when they register on Teachable:
You can achieve similar functionality by sending the invites through Zapier. That is outlined here: Automate sending Discourse invite emails with Zapier.