Improve error handling for authentication

closes #3660
- added wrapping in JSON API format to error500()
- added client side handling