Advertisement
Guest User

Untitled

a guest
Mar 27th, 2017
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.46 KB | None | 0 0
  1. <?php
  2. $app->get('/session', function() {
  3. $db = new DbHandler();
  4. $session = $db->getSession();
  5. $response["uid"] = $session['uid'];
  6. $response["email"] = $session['email'];
  7. $response["name"] = $session['name'];
  8. echoResponse(200, $session);
  9. });
  10.  
  11. $app->post('/login', function() use ($app) {
  12. require_once 'passwordHash.php';
  13. $r = json_decode($app->request->getBody());
  14. verifyRequiredParams(array('email', 'password'),$r->customer);
  15. $response = array();
  16. $db = new DbHandler();
  17. $password = $r->customer->password;
  18. $email = $r->customer->email;
  19. $user = $db->getOneRecord("select uid,name,password,email,created from customers_auth where phone='$email' or email='$email'");
  20. if ($user != NULL) {
  21. if(passwordHash::check_password($user['password'],$password)){
  22. $response['status'] = "success";
  23. $response['message'] = 'Logged in successfully.';
  24. $response['name'] = $user['name'];
  25. $response['uid'] = $user['uid'];
  26. $response['email'] = $user['email'];
  27. $response['createdAt'] = $user['created'];
  28. if (!isset($_SESSION)) {
  29. session_start();
  30. }
  31. $_SESSION['uid'] = $user['uid'];
  32. $_SESSION['email'] = $email;
  33. $_SESSION['name'] = $user['name'];
  34. } else {
  35. $response['status'] = "error";
  36. $response['message'] = 'Login failed. Incorrect credentials';
  37. }
  38. }else {
  39. $response['status'] = "error";
  40. $response['message'] = 'No such user is registered';
  41. }
  42. echoResponse(200, $response);
  43. });
  44. $app->post('/signUp', function() use ($app) {
  45. $response = array();
  46. $r = json_decode($app->request->getBody());
  47. verifyRequiredParams(array('email', 'name', 'password'),$r->customer);
  48. require_once 'passwordHash.php';
  49. $db = new DbHandler();
  50. $phone = $r->customer->phone;
  51. $name = $r->customer->name;
  52. $email = $r->customer->email;
  53. $address = $r->customer->address;
  54. $password = $r->customer->password;
  55. $isUserExists = $db->getOneRecord("select 1 from customers_auth where phone='$phone' or email='$email'");
  56. if(!$isUserExists){
  57. $r->customer->password = passwordHash::hash($password);
  58. $tabble_name = "customers_auth";
  59. $column_names = array('phone', 'name', 'email', 'password', 'city', 'address');
  60. $result = $db->insertIntoTable($r->customer, $column_names, $tabble_name);
  61. if ($result != NULL) {
  62. $response["status"] = "success";
  63. $response["message"] = "User account created successfully";
  64. $response["uid"] = $result;
  65. if (!isset($_SESSION)) {
  66. session_start();
  67. }
  68. $_SESSION['uid'] = $response["uid"];
  69. $_SESSION['phone'] = $phone;
  70. $_SESSION['name'] = $name;
  71. $_SESSION['email'] = $email;
  72. echoResponse(200, $response);
  73. } else {
  74. $response["status"] = "error";
  75. $response["message"] = "Failed to create customer. Please try again";
  76. echoResponse(201, $response);
  77. }
  78. }else{
  79. $response["status"] = "error";
  80. $response["message"] = "An user with the provided phone or email exists!";
  81. echoResponse(201, $response);
  82. }
  83. });
  84. $app->get('/logout', function() {
  85. $db = new DbHandler();
  86. $session = $db->destroySession();
  87. $response["status"] = "info";
  88. $response["message"] = "Logged out successfully";
  89. echoResponse(200, $response);
  90. });
  91. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement