add: user login with password

This commit is contained in:
Johannes Kantz 2023-01-12 15:04:15 +01:00
parent 7b5672d36b
commit f25b2a72f7
2 changed files with 21 additions and 11 deletions

View File

@ -2,6 +2,7 @@
require_once("BancaDati/BancaDati.php"); require_once("BancaDati/BancaDati.php");
use BancaDati\BancaDati; use BancaDati\BancaDati;
class User { class User {
public string $id;
public string $username; public string $username;
public string $email; public string $email;
public string $token; public string $token;
@ -9,7 +10,7 @@ class User {
private BancaDati $db; private BancaDati $db;
public function __construct() { public function __construct() {
$db = new BancaDati(); $this->db = new BancaDati();
return $this; return $this;
} }
@ -17,12 +18,24 @@ class User {
return true; return true;
} }
public function loginWithUsername(string $username, string $password) : string { public function loginWithUsername(string $username, string $password) : string {
$userObject = $this->db->select("utente", ["username" => $username]); $userObject = $this->db->select("utente", ["nomeUtente" => $username]);
var_dump($userObject);
if(!$userObject){ if(!$userObject){
return false; return false;
} }
return "token"; $this->id = $userObject["id"];
$this->username = $userObject["nomeUtente"];
$this->email = $userObject["email"];
$this->password = $userObject["parolaDordine"];
$this->token = $this->db->createUUID();
if($this->password != $password){
return false; // ungültiges password
}
$this->db->update("utente", $this->id, ["gettone" => $this->token]);
return $this->token;
} }
public function loginWithToken(string $token){ public function loginWithToken(string $token){

View File

@ -41,15 +41,12 @@ $app->post("/login", function( array $req, Response $res) use ($db) {
$password = $req["body"]["password"]; $password = $req["body"]["password"];
$user = $db->select("utente", ["nomeUtente" => $username]); $user = $db->select("utente", ["nomeUtente" => $username]);
var_dump($user);
return;
$user = new User(); $user = new User();
$user->loginWithUsername($username, $password); $usertoken = $user->loginWithUsername($username, $password);
return;
if(isset($user)){ if($usertoken){
setcookie($user->token, "TOKEN"); setcookie("TOKEN", $usertoken, time()+3600); // 1h
$res->send("Login successful", 200); $res->send("Login successful" . "token: " . $usertoken, 200);
}else{ }else{
$res->send("Login failed", 403); $res->send("Login failed", 403);
} }