Advertisement
Guest User

Untitled

a guest
Nov 22nd, 2017
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.64 KB | None | 0 0
  1. public function login($nick, $email, $password)
  2. {
  3.  
  4. $query = "
  5. SELECT
  6. *
  7. FROM
  8. " . $this->table_name . "
  9. WHERE
  10. email = ?
  11. OR
  12. nick = ?
  13. ";
  14.  
  15. // prepare query statement
  16. $stmt = $this->conn->prepare($query);
  17.  
  18. //bind values from user
  19. $stmt->bindParam(1, $email);
  20. $stmt->bindParam(2, $nick);
  21.  
  22. $stmt->execute();
  23.  
  24. if($stmt === true) {
  25. $row = $stmt->fetch(PDO::FETCH_ASSOC);
  26. $num = $stmt->rowCount();
  27.  
  28. if ($num === 1) {
  29. if (password_verify($password, $row['password'])) {
  30. $token = array();
  31. $token['id'] = $row['id'];
  32. $encoded_token = JWT::encode($token, 'secret_server_key');
  33.  
  34. $query = "
  35. UPDATE
  36. " . $this->table_name . "
  37. SET
  38. token = " . $encoded_token . "
  39. WHERE
  40. id = " . $row['id'] . "
  41. ";
  42.  
  43. $stmt_token = $this->conn->prepare($query);
  44. $stmt_token->execute();
  45.  
  46. if ($stmt_token === true)
  47. {
  48. http_response_code(200);
  49. return true;
  50. }
  51. }
  52. else
  53. {
  54. http_response_code(402);
  55. return false;
  56. }
  57. }
  58. else
  59. {
  60. http_response_code(401);
  61. return false;
  62. }
  63. }
  64. else
  65. {
  66. http_response_code(400);
  67. }
  68. }
  69.  
  70. <?php
  71. if(isset($_POST['submit']))
  72. {
  73. if(
  74. (
  75. (isset($_POST['nick']) && !empty($_POST['nick']))
  76. ||
  77. (isset($_POST['email']) && !empty($_POST['email']))
  78. ) &&
  79. (isset($_POST['password']) && !empty($_POST['password']))
  80.  
  81. )
  82. {
  83. // instantiate database
  84. require_once '../config/database.php';
  85. $database = new Database();
  86. $db = $database->getConnection();
  87.  
  88. require_once '../classes/user.php';
  89. $user = new User($db);
  90.  
  91. if(!isset($_POST['nick']))
  92. {
  93. $user_nick = null;
  94. $user->login($user_nick, $_POST['email'], $_POST['password']);
  95. }
  96. else if(!isset($_POST['email']))
  97. {
  98. $user_email = null;
  99. $user->login($_POST['nick'], $user_email, $_POST['password']);
  100. }
  101. }
  102. else
  103. {
  104. http_response_code(400);
  105. }
  106. }
  107. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement