Improve error handling for authentication

closes #3660
- added wrapping in JSON API format to error500()
- added client side handling
Merge pull request #3686 from sebgie/issue#3660

Improve error handling for authentication