Metsuryu while probably feasible, it's very complicated to do with Flarum.
The easiest solution in my opinion is to do it via PHP, just listen for the user login event and do stuff there. Nothing else special to do.
If you want to do something in javascript when the user logs in, you need to hook up to something via javascript. The problem is that Flarum does a full page refresh after login. So either you need to perform your action after login but before the refresh, or after refresh.
To do it before refresh you could extend/replace the submit
handler of the login/signup modal so you can do your own things before calling location.reload()
. The downside of that is that you're holding back the process until you're done and the user has to wait (and might manually refresh if it's taking too long). Then also at this point Flarum is still in guest mode, so you'd have to hit the API to get anything you need (username or other things), adding more process time.
If you run your javascript after the refresh, you have access to anything that is injected into the page (via the api events, including the user details and your own data if you injected it via a listener). The downside is that you need some logic to decide if your javascript is run, as when the page loads there's no way of knowing if the user just logged in. You could use a custom flag you set when the login event fires or base it on whether a property exists on the user.
Either way I think you will end up writing more PHP if you try going with a javascript solution for your actual problem.
And as luceos pointed out using javascript will probably expose secrets so unless you have one API key sandboxed to each user and with only some very limited capability you will just give every user the keys to do anything with your app.
Also doing it purely with PHP you will have better error handling and can actually prevent login or do something if an error occurs. If using javascript only, your code might end up in cases where it errors or do not run and you need additional ways of deciding if it needs to run again.
I'm not sure which extension I can recommend you look at. I'm not even sure the login event is much used. One extension that hooks into login is my Chatwee extension https://github.com/clarkwinkelmann/flarum-ext-chatwee but I actually had to use a middleware instead of the login event because you can't add cookies when hooking into the login event...
Hope this helps ?