Making external API call after logging in

(Adam Capriola) #1

This would be for SSO synchronization purposes: I’d like to call an external API to send back the external_id and actual Discourse username of the user after successfully logging in to Discourse through SSO, which would in essence be telling the source of the login that “Hey, they made it!” I could then do stuff with those two variables to make sure the make sure the user is synchronized correctly between the two platforms.

I am a complete novice when it comes to Ruby and coding anything for Discourse, but any pointers on how to do this would be much appreciated.

(Adam Capriola) #2

I figured out a workaround for this:

  1. Enable the sso overrides username setting.
  2. Set username change period to 0.
  3. If using WordPress, queue up wp_schedule_single_event with a short delay (I’m trying 15 seconds) right before redirecting the user back to Discourse. Other softwares might have similar functionality.

Steps 1 and 2 help ensure the username is the same both on Discourse and the other part of your site. (This isn’t foolproof unless you also restrict usernames between 3 and 15 letters, numbers, and underscores on the other part as well.)

Step 3 gives the user a chance to be registered into the Discourse database (if they’ve never logged in before) and then you can run any code you want to sync groups and whatnot. The delay in execution is important for fresh users.