What's the best way to catch the login event in plugin on client side?

My Discourse site is set up as the SSO provider for my WordPress site. Since login / logout on Discourse doesn’t automatically sync to WordPress, I’m writing a Discourse plugin to do that. I found a way to catch the logout event from here: https://github.com/discourse/discourse/blob/b404a4b97c41953cfd35c51b38068338b918831d/app/models/user.rb#L909, but the log_in method below doesn’t publish to message bus, is there a way to catch login event at all?

Another question, during /logout event, I can send an AJAX request to logout in WP, but the AJAX request gets canceled because logout causes a page refresh before the request is completed, is there a way to tell discourse to wait for a Promise/Deferred to complete before proceed?

Thanks!

Looks like I can emit events inside of my plugin, using DiscourseEvent.trigger, so the event problem is solved, still trying to look for a way to hold the login/logout page refresh so that the requests I made can be completed.

Any input is appreciated.