Newer
Older
flow-task / server / App / routes_map.php
<?php

namespace App;

use \Kernel\Classes\Auth;

function init_auth($router) {
	$router -> linking("GET", "signup", function(){
		$uname = $_GET["username"];
		$password = $_GET["password"];

		$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()
		]);
	});


	$router -> linking("GET", "signin", function(){
		$uname = $_GET["username"];
		$password = $_GET["password"];

		$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
				]
			]
		]);
	});

	$router -> linking("GET", "signout", function(){
		$token = sanitize_input($_GET["token"]);
		$auth = new Auth();

		if(!$auth -> signout($token)) {
			return error_response("signout", "Unknown error");
		}

		return success_response();
	});
}

function init_routes_map($router) {
	init_auth($router);
}