Permalink
Browse files

Fixed PHP api

  • Loading branch information...
1 parent f782594 commit 0ab5cd597ccab48cebb0193681b381f2b75f57a0 @jpka committed Dec 8, 2012
View
BIN db/todo.0
Binary file not shown.
View
Binary file not shown.
@@ -15,7 +15,7 @@ define ["app", "modules/baseView", "modules/tasks"], (app, View, Tasks) ->
login: (registerFirst) ->
$.ajax
- type: "POST"
+ cache: false
url: "#{app.url}/" + if registerFirst then "register" else "login"
dataType: "json"
data: @$el.serialize()
@@ -16,7 +16,7 @@ define ["app", "modules/login", "modules/tasks"], (app, Login, Tasks) ->
start: ->
$.ajax
- type: "POST"
+ cache: false
url: "#{app.url}/login"
data: ""
dataType: "json"
View
@@ -15,10 +15,7 @@ class UnauthorizedException extends Exception {}
$r->exceptionRoute("UnauthorizedException", function (UnauthorizedException $e) {
header(" ", true, 401);
$message = $e->getMessage();
- if (!empty($message))
- return array("error" => $message);
- else
- return false;
+ return array("error" => empty($message) ? "Unauthorized" : $message);
});
$r->exceptionRoute("Exception", function (Exception $e) {
@@ -37,12 +34,12 @@ function authSuccesful($username, $password) {
}
function authenticate($user) {
- $username = isset($_COOKIE["username"]) ? $_COOKIE["username"] : $_POST["username"];
+ $username = isset($_COOKIE["username"]) ? $_COOKIE["username"] : $_GET["username"];
if (isset($user) && $user !== $username) {
throw new UnauthorizedException();
return false;
}
- $password = isset($_COOKIE["password"]) ? $_COOKIE["password"] : $_POST["password"];
+ $password = isset($_COOKIE["password"]) ? $_COOKIE["password"] : $_GET["password"];
$user = new User(array("username" => $username, "password" => $password));
try {
@@ -56,16 +53,16 @@ function authenticate($user) {
function postOrPutData() {
$data = array();
- foreach (json_decode(file_get_contents("php://input"), true) as $field => $value) {
+ foreach ((empty($_POST) ? json_decode(file_get_contents("php://input"), true) : $_POST) as $field => $value) {
if (!empty($value))
$data[$field] = $value;
}
return $data;
}
-$r->post("/login", function() {
- $username = isset($_COOKIE["username"]) ? $_COOKIE["username"] : $_POST["username"];
- $password = isset($_COOKIE["password"]) ? $_COOKIE["password"] : $_POST["password"];
+$r->get("/login", function() {
+ $username = isset($_COOKIE["username"]) ? $_COOKIE["username"] : $_GET["username"];
+ $password = isset($_COOKIE["password"]) ? $_COOKIE["password"] : $_GET["password"];
return authSuccesful($username, $password);
})->by("authenticate");
@@ -90,18 +87,19 @@ function postOrPutData() {
$r->post("/*/tasks", function($user) {
$task = new Task(postOrPutData());
+ $task->user = $user;
$task->save();
return $task->expose();
})->by("authenticate");
$r->get("/*/tasks/*", function($user, $id) {
- return Task::findOne(array("username" => $user, "id" => intval($id)))->expose();
+ return Task::findOne(array("user" => $user, "id" => intval($id)))->expose();
})->by("authenticate");
$r->put("/*/tasks/*", function($user, $id) {
$task = Task::findOne(array("user" => $user, "id" => intval($id)));
$task->update(postOrPutData());
- return true;
+ return $task->expose();
})->by("authenticate");
$r->delete("/*/tasks/*", function($user, $id) {
@@ -18,7 +18,7 @@
login: function(registerFirst) {
var _this = this;
return $.ajax({
- type: "POST",
+ cache: false,
url: ("" + app.url + "/") + (registerFirst ? "register" : "login"),
dataType: "json",
data: this.$el.serialize(),
@@ -21,7 +21,7 @@
start: function() {
var _this = this;
return $.ajax({
- type: "POST",
+ cache: false,
url: "" + app.url + "/login",
data: "",
dataType: "json",

0 comments on commit 0ab5cd5

Please sign in to comment.