The full JavaScript code is available in this forum post.

I've modified the Delphi framework code (in the current trunk version) to match the authentication JavaScript code.
In fact, the server response is now a true JSON result object. I've modified the TSQLRestURI.SetUser code to let pure Delphi client code work as expected.

In the same discussion, another user involved in developing AJAX applications with mORMot, named RangerX, made a nice proposal: add a generic JSON error message mechanisms in the framework.
I've added this to the project to-do list.