Revert "Revert "Improve login handling using hooks""

This reverts commit 8981ed62103baf3969a644799c577f6404a2325e.