Add middleware to handle authentication

- Changed how login/logout works slightly.  Mostly code cleanup.