Newer
Older
flow-task / server / App / Controller / AuthController.php
  1. <?php
  2.  
  3. namespace App\Controller;
  4.  
  5. use \App\Models\Auth;
  6.  
  7. class AuthController {
  8.  
  9. public function signup() {
  10. list($uname, $password) = get_expected_vars($_GET, ["username", "password"]);
  11. $uname = sanitize_input($uname);
  12.  
  13. $auth = new Auth();
  14.  
  15. if($auth -> user_name_is_exists($uname)) {
  16. return error_response("signup", "Username already exists");
  17. }
  18.  
  19. $user = $auth -> signup($uname, $password);
  20.  
  21. if(!$user) {
  22. return error_response("signup", "Unknown error");
  23. }
  24.  
  25. return success_response([
  26. "user_id" => $user -> get_id()
  27. ]);
  28. }
  29.  
  30. public function signin() {
  31. list($uname, $password) = get_expected_vars($_GET, ["username", "password"]);
  32. $uname = sanitize_input($uname);
  33.  
  34. $auth = new Auth();
  35.  
  36. $session = $auth -> signin($uname, $password);
  37.  
  38. if(!$session) {
  39. return error_response("signin", "Wrong username or password");
  40. }
  41.  
  42. return success_response([
  43. "session" => [
  44. "token" => $session -> token,
  45. "user" => [
  46. "id" => $session -> user() -> get_id(),
  47. "name" => $session -> user() -> name
  48. ]
  49. ]
  50. ]);
  51. }
  52.  
  53. public function signout() {
  54. list($token) = get_expected_vars($_GET, ["token"]);
  55. $token = sanitize_input($token);
  56. $auth = new Auth();
  57.  
  58. if(!$auth -> signout($token)) {
  59. return error_response("signout", "Unknown error");
  60. }
  61.  
  62. return success_response();
  63. }
  64. }