Newer
Older
flow-task / server / Kernel / Classes / Auth.php
<?php

namespace Kernel\Classes;

use \Kernel\Entity\User;
use \Kernel\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);
		$uname = sanitize_input($uname);

		$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);
		$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(String $token) :bool {
		$session = new Session();
		if(!$session -> init_by_token($token)) {
			return false;
		}

		return $session -> kill();
	}
}