Search code examples
djangodjango-authenticationdjango-allauthdjango-signalsdjango-middleware

How to execute code after authentication in Django?


I want to execute one or more functions after a user logs into my site. How is this possible? I looked into Middleware. Djangobook says that I'll need this to run a piece of code on each and every request that Django handles. However, I just need the code run when the authentication happens successfully.

Note: I am using Django Allauth for authentication and I don't have any view of my own to log in users.


Solution

  • As per the official documentation, there is a signal allauth.account.signals.user_logged_in which gets triggered when a user logs in. This can serve your purpose.