Move as much login as possible out of views.py.