Logout user on 401 to correctly handle session expiration.

Fixes #973.