Move to new event/action based login.

Also clean up DB code, and change error handling for user registration.
Pretty heavy edits to almost every file, so I'll be amazed if I haven't
introduced some bugs.