<?php namespace App\Models; use \App\Entity\User; use \App\Entity\Session; class Auth { public function __construct() {} 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) { $passhash = sha1($password); $user = new User(); $user -> name = $uname; $user -> passhash = $passhash; $user -> create_at = get_create_at_timestamp(); return $user -> create_new() ? $user : false; } public function signin(String $uname, String $password) { $passhash = sha1($password); $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(String $token) :bool { $session = new Session(); if(!$session -> init_by_token($token)) { return false; } return $session -> kill(); } }