Make auth
1 parent 01478e2 commit 7b8eadcf666d1c40417288aaf4cdb081bcdd5bcb
@eugene-sukhodolskiy eugene-sukhodolskiy authored on 29 Dec
Showing 9 changed files
View
57
server/App/routes_map.php
<?php
 
namespace App;
 
function init_routes_map($router) {
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", "User name already exists");
return error_response("signup", "Username already exists");
}
 
$user_id = $auth -> signup($uname, $password);
$user = $auth -> signup($uname, $password);
 
if(!$user_id) {
if(!$user) {
return error_response("signup", "Unknown error");
}
 
return success_reponse("signup", ["status" => true]);
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);
}
View
54
server/Kernel/Classes/Auth.php
<?php
 
namespace Kernel\Classes;
 
use \Kernel\Entity\User;
use \Kernel\Entity\Session;
 
class Auth {
public function __construct() {
public function __construct() {}
 
}
 
protected function user_name_is_exists(String $uname) : bool {
$user = new User();
$sql = "SELECT COUNT(*) FROM `{$user -> get_tablename()}` WHERE `name`='{$uname}'";
public function user_name_is_exists(String $uname) :bool {
$tablename = User::get_tablename();
$sql = "SELECT COUNT(*) FROM `{$tablename}` WHERE `name`='{$uname}'";
$result = app() -> db() -> query($sql);
$d = $result -> fetch_assoc();
return $d["COUNT(*)"] ? true : false;
}
 
public function signup(String $uname, String $password): bool {
public function signup(String $uname, String $password) {
$passhash = sha1($password);
$uname = normalize_input_user_name($uname);
$uname = sanitize_input($uname);
 
$user = new User();
$user -> name = $uname;
$user -> passhash = $passhash;
$user -> create_at = get_create_at_timestamp();
 
return $user -> create_new();
return $user -> create_new() ? $user : false;
}
 
public function signin() {
public function signin(String $uname, String $password) {
$passhash = sha1($password);
$uname = sanitize_input($uname);
 
$tablename = User::get_tablename();
$sql = "SELECT `id` FROM `{$tablename}` WHERE `name`='{$uname}' AND `passhash`='{$passhash}'";
$result = app() -> db() -> query($sql);
if(!$result) {
return false;
}
 
$d = $result -> fetch_assoc();
if(!isset($d["id"])) {
return false;
}
 
$session = new Session();
 
return $session -> init_for_user($d["id"]) ?? false;
}
 
public function signout() {
public function signout(String $token) :bool {
$session = new Session();
if(!$session -> init_by_token($token)) {
return false;
}
 
return $session -> kill();
}
}
View
server/Kernel/Classes/DB.php
View
server/Kernel/Classes/Router.php
View
server/Kernel/Entity/Session.php
View
server/Kernel/Entity/User.php
View
server/libs/DataContain.php
View
server/libs/DataStorage.php
View
server/libs/utils.php