- <?php
-
- namespace App\Controller;
-
- use \App\Models\Auth;
-
- class AuthController {
-
- public function signup() {
- list($uname, $password) = get_expected_vars($_GET, ["username", "password"]);
- $uname = sanitize_input($uname);
-
- $auth = new Auth();
-
- if($auth -> user_name_is_exists($uname)) {
- return error_response("signup", "Username already exists");
- }
-
- $user = $auth -> signup($uname, $password);
-
- if(!$user) {
- return error_response("signup", "Unknown error");
- }
-
- return success_response([
- "user_id" => $user -> get_id()
- ]);
- }
-
- public function signin() {
- list($uname, $password) = get_expected_vars($_GET, ["username", "password"]);
- $uname = sanitize_input($uname);
-
- $auth = new Auth();
-
- $session = $auth -> signin($uname, $password);
-
- if(!$session) {
- return error_response("signin", "Wrong username or password");
- }
-
- return success_response([
- "session" => [
- "token" => $session -> token,
- "user" => [
- "id" => $session -> user() -> get_id(),
- "name" => $session -> user() -> name
- ]
- ]
- ]);
- }
-
- public function signout() {
- list($token) = get_expected_vars($_GET, ["token"]);
- $token = sanitize_input($token);
-
- $auth = new Auth();
-
- if(!$auth -> signout($token)) {
- return error_response("signout", "Unknown error");
- }
-
- return success_response();
- }
- }