Index: Dev/branches/rest-dojo-ui/server/api.php
===================================================================
--- Dev/branches/rest-dojo-ui/server/api.php	(revision 263)
+++ Dev/branches/rest-dojo-ui/server/api.php	(revision 274)
@@ -46,12 +46,8 @@
 
         $user = null;
-        $data = null;
         if (!empty($request->data)) {
-            $data = json_decode($request->data, true);
-        }
-        if (!empty($data)) {
-            $user = Auth::login($data['username'], sha1($data['password']));
+            $user = Auth::login($request->data['email'], $request->data['password']);
             if (!$user) {
-                throw new ResponseException("Incorrect username and password", Response::UNAUTHORIZED);
+                throw new ResponseException("Incorrect email and password", Response::UNAUTHORIZED);
             }
             set_session_cookie($response, $user);
@@ -59,5 +55,26 @@
             $user = restore_session($response);
         }
-        $response->body = json_encode($user);
+        $response->body = $user;
+        return $response;
+    }
+
+}
+
+/**
+ *  @uri /register
+ */
+class RegisterResource extends Resource {
+
+    function post($request) {
+        $response = new Response($request);
+
+        $user = null;
+        if (!empty($request->data)) {
+            $user = Auth::register($request->data['email'], $request->data['password']);
+            set_session_cookie($response, $user);
+        } else {
+            throw new ResponseException("No email and password provided.",Response::BADREQUEST);
+        }
+        $response->body = $user;
         return $response;
     }
@@ -83,7 +100,6 @@
         $type = $this->getType($request);
         $objects = $type::get(array());
-        //$objects = array_map(function($val) { return array('uid' => $val->uid); }, $objects);
-
-        $response->body = json_encode($objects);
+
+        $response->body = $objects;
         return $response;
     }
@@ -94,11 +110,10 @@
 
         $type = $this->getType($request);
-        $data = json_decode($request->data);
         $onlyAdd = $request->ifNoneMatch('*');
         $onlyUpdate = $request->ifMatch('*');
 
         $object = FALSE;
-        if (isset($data->uid)) {
-            $objects = $type::get(array('uid' => $data->uid));
+        if (isset($request->data->uid)) {
+            $objects = $type::get(array('uid' => $request->data->uid));
             if (!empty($objects)) {
                 $object = $objects[0];
@@ -116,10 +131,12 @@
             $response->code = Response::OK;
         }
-        foreach ($data as $key => $val) {
+        foreach ($request->data as $key => $val) {
             $object->$key = $val;
         }
-        $object->save();
-
-        $response->body = json_encode($object);
+        if (!$object->save()) {
+            throw new ResponseException("Save failed", Response::INTERNALSERVERERROR);
+        }
+
+        $response->body = $object;
         return $response;
     }
@@ -150,5 +167,5 @@
         }
 
-        $response->body = json_encode($objects[0]);
+        $response->body = $objects[0];
         return $response;
     }
@@ -159,5 +176,4 @@
 
         $info = $this->getTypeAndUid($request);
-        $data = json_decode($request->data);
         $onlyAdd = $request->ifNoneMatch('*');
         $onlyUpdate = $request->ifMatch('*');
@@ -179,15 +195,17 @@
             $response->code = Response::OK;
         }
-        foreach ($data as $key => $val) {
+        foreach ($request->data as $key => $val) {
             $object->$key = $val;
         }
-        $object->save();
-
-        $response->body = json_encode($object);
+        if (!$object->save()) {
+            throw new ResponseException("Save failed", Response::INTERNALSERVERERROR);
+        }
+
+        $response->body = $object;
         return $response;
     }
 
     function delete($request) {
-        restore_session();
+        restore_session(new Response($request));
         throw new ReponseException("Delete not implemented", Response::METHODNOTALLOWED);
     }
@@ -202,5 +220,5 @@
     $request->baseUri = $baseUri;
 }
-
+$request->data = Marshaller::unmarshall(json_decode($request->data));
 try {
     $resource = $request->loadResource();
@@ -208,11 +226,13 @@
 } catch (ResponseException $e) {
     $response = $e->response($request);
-    $response->body = json_encode(array('errorMsg' => $response->body));
+    $response->body = array('errorMsg' => $response->body);
 } catch (Exception $e) {
     $response = new Response($request);
     $response->code = Response::INTERNALSERVERERROR;
-    $response->body = json_encode(array('errorMsg' => "Unhandled exception: " . $e));
+    $response->body = array('errorMsg' => "Unhandled exception: " . $e);
 }
 add_default_headers($response);
+$response->body = json_encode(Marshaller::marshall($response->body));
 $response->output();
+
 ?>
