Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fixed PHP api

  • Loading branch information...
commit 0ab5cd597ccab48cebb0193681b381f2b75f57a0 1 parent f782594
Juan Pablo Kaniefsky authored
BIN  db/todo.0
Binary file not shown
BIN  db/todo.ns
Binary file not shown
2  frontend/app/scripts/modules/login.coffee
@@ -15,7 +15,7 @@ define ["app", "modules/baseView", "modules/tasks"], (app, View, Tasks) ->
15 15
16 16 login: (registerFirst) ->
17 17 $.ajax
18   - type: "POST"
  18 + cache: false
19 19 url: "#{app.url}/" + if registerFirst then "register" else "login"
20 20 dataType: "json"
21 21 data: @$el.serialize()
2  frontend/app/scripts/router.coffee
@@ -16,7 +16,7 @@ define ["app", "modules/login", "modules/tasks"], (app, Login, Tasks) ->
16 16
17 17 start: ->
18 18 $.ajax
19   - type: "POST"
  19 + cache: false
20 20 url: "#{app.url}/login"
21 21 data: ""
22 22 dataType: "json"
22 php/api.php
@@ -15,10 +15,7 @@ class UnauthorizedException extends Exception {}
15 15 $r->exceptionRoute("UnauthorizedException", function (UnauthorizedException $e) {
16 16 header(" ", true, 401);
17 17 $message = $e->getMessage();
18   - if (!empty($message))
19   - return array("error" => $message);
20   - else
21   - return false;
  18 + return array("error" => empty($message) ? "Unauthorized" : $message);
22 19 });
23 20
24 21 $r->exceptionRoute("Exception", function (Exception $e) {
@@ -37,12 +34,12 @@ function authSuccesful($username, $password) {
37 34 }
38 35
39 36 function authenticate($user) {
40   - $username = isset($_COOKIE["username"]) ? $_COOKIE["username"] : $_POST["username"];
  37 + $username = isset($_COOKIE["username"]) ? $_COOKIE["username"] : $_GET["username"];
41 38 if (isset($user) && $user !== $username) {
42 39 throw new UnauthorizedException();
43 40 return false;
44 41 }
45   - $password = isset($_COOKIE["password"]) ? $_COOKIE["password"] : $_POST["password"];
  42 + $password = isset($_COOKIE["password"]) ? $_COOKIE["password"] : $_GET["password"];
46 43 $user = new User(array("username" => $username, "password" => $password));
47 44
48 45 try {
@@ -56,16 +53,16 @@ function authenticate($user) {
56 53
57 54 function postOrPutData() {
58 55 $data = array();
59   - foreach (json_decode(file_get_contents("php://input"), true) as $field => $value) {
  56 + foreach ((empty($_POST) ? json_decode(file_get_contents("php://input"), true) : $_POST) as $field => $value) {
60 57 if (!empty($value))
61 58 $data[$field] = $value;
62 59 }
63 60 return $data;
64 61 }
65 62
66   -$r->post("/login", function() {
67   - $username = isset($_COOKIE["username"]) ? $_COOKIE["username"] : $_POST["username"];
68   - $password = isset($_COOKIE["password"]) ? $_COOKIE["password"] : $_POST["password"];
  63 +$r->get("/login", function() {
  64 + $username = isset($_COOKIE["username"]) ? $_COOKIE["username"] : $_GET["username"];
  65 + $password = isset($_COOKIE["password"]) ? $_COOKIE["password"] : $_GET["password"];
69 66 return authSuccesful($username, $password);
70 67 })->by("authenticate");
71 68
@@ -90,18 +87,19 @@ function postOrPutData() {
90 87
91 88 $r->post("/*/tasks", function($user) {
92 89 $task = new Task(postOrPutData());
  90 + $task->user = $user;
93 91 $task->save();
94 92 return $task->expose();
95 93 })->by("authenticate");
96 94
97 95 $r->get("/*/tasks/*", function($user, $id) {
98   - return Task::findOne(array("username" => $user, "id" => intval($id)))->expose();
  96 + return Task::findOne(array("user" => $user, "id" => intval($id)))->expose();
99 97 })->by("authenticate");
100 98
101 99 $r->put("/*/tasks/*", function($user, $id) {
102 100 $task = Task::findOne(array("user" => $user, "id" => intval($id)));
103 101 $task->update(postOrPutData());
104   - return true;
  102 + return $task->expose();
105 103 })->by("authenticate");
106 104
107 105 $r->delete("/*/tasks/*", function($user, $id) {
2  php/app/scripts/modules/login.js
@@ -18,7 +18,7 @@
18 18 login: function(registerFirst) {
19 19 var _this = this;
20 20 return $.ajax({
21   - type: "POST",
  21 + cache: false,
22 22 url: ("" + app.url + "/") + (registerFirst ? "register" : "login"),
23 23 dataType: "json",
24 24 data: this.$el.serialize(),
2  php/app/scripts/router.js
@@ -21,7 +21,7 @@
21 21 start: function() {
22 22 var _this = this;
23 23 return $.ajax({
24   - type: "POST",
  24 + cache: false,
25 25 url: "" + app.url + "/login",
26 26 data: "",
27 27 dataType: "json",

0 comments on commit 0ab5cd5

Please sign in to comment.
Something went wrong with that request. Please try again.