2

I'm using Django and python-social-auth and when a user signs up I want to call the API of another service to add the user to it (think something like Mailchimp). What's the correct way of doing it?

My app has a backend which is API only written in Python and Django, and a frontend written in JavaScript. For the purpose of signing up/in though, there's a link to the backend (api.example.com/social-auth/...) so it ends up being a bit of a hybrid.

After logging in or out, the backend redirects the user back to the frontend, by having this in settings.py:

LOGIN_REDIRECT_URL = env("FRONTEND_URL")
LOGOUT_REDIRECT_URL = env("FRONTEND_URL")

If there's any of my code that's relevant to this question, please let me know and I'll add it. I'm not sure what parts are and I didn't want to do a big dump of all my code. Also, I don't think my code around this issue is particularly good, so I'm happy to change it.

Pablo Fernandez
  • 279,434
  • 135
  • 377
  • 622
  • I haven't used python-social-auth, but if it uses the Django user system, maybe you could do this at the Model level (extending the User and overriding the save() method), so every time a user is created it automatically creates it in the other app. – Marc Compte May 29 '21 at 15:53
  • @MarcCompte: it does create user models. Is that the correct Django way of doing this? In the Rails world is frown upon to have the model trigger API calls for example. – Pablo Fernandez May 29 '21 at 17:32
  • It is not usual, I agree. Personally, I think it depends on your flow design. If every time a user gets created at the database level you need to run an API call, then from a conceptual point of view, this belongs to the model. If you want it to happen only when the user registers him/herself, then this belongs to the ModelForm and not the Model. – Marc Compte May 29 '21 at 18:56
  • This would mean both apps are tight together, you will not be able to run the first one without the other. If that is already your design, then I still think it could be a valid way. If not, then do that on the form. – Marc Compte May 29 '21 at 18:58

0 Answers0