Compare commits

...

16 Commits

Author SHA1 Message Date
d28b3fac90 Merge pull request 'Login' (#1) from feat/login into don
Reviewed-on: #1
2023-01-18 08:40:25 +01:00
fbfeeb7679 trying to resolve conflict 2023-01-18 08:39:45 +01:00
Johannes Kantz
2860a01cf8 fix: login with Token 2023-01-12 15:29:58 +01:00
Johannes Kantz
a887921e5d quick safe 2023-01-12 15:22:32 +01:00
Johannes Kantz
a2178733da fix: remove print 2023-01-12 15:09:39 +01:00
Johannes Kantz
5fea5ad15f fix: update vales with quotes 2023-01-12 15:09:08 +01:00
Johannes Kantz
fe5efbe8f1 fix: remove test 2023-01-12 15:04:49 +01:00
Johannes Kantz
f25b2a72f7 add: user login with password 2023-01-12 15:04:15 +01:00
Johannes Kantz
7b5672d36b quick safe 2023-01-12 14:44:52 +01:00
Johannes Kantz
21ebecac54 fix: select fetch 2023-01-12 14:41:51 +01:00
Johannes Kantz
acfae23029 fix: remove print 2023-01-12 14:37:11 +01:00
Johannes Kantz
e1a0f112a2 fix: select values with single quotes 2023-01-12 14:36:37 +01:00
Johannes Kantz
23cf1b54e4 fix: insert vales with single quotes 2023-01-12 14:28:40 +01:00
Johannes Kantz
95a2c3e3fe fix: add Body to Post request 2023-01-12 14:09:02 +01:00
Johannes Kantz
148f95087d add: login begin 2023-01-11 14:45:18 +01:00
Johannes Kantz
81cdae85b0 add: user 2023-01-11 14:45:18 +01:00
3 changed files with 85 additions and 2 deletions

View File

@ -30,7 +30,7 @@ class Router
$this->request = $_SERVER;
}
if (isset($_POST)) {
$this->request["body"] = $_POST;
$this->request["body"] = json_decode(file_get_contents('php://input'), true);
}
if (isset($_POST)) {
$this->request["params"] = $_GET;

53
User.php Normal file
View File

@ -0,0 +1,53 @@
<?php
require_once("BancaDati/BancaDati.php");
use BancaDati\BancaDati;
class User {
public string $id;
public string $username;
public string $email;
public string $token;
private string $password;
private BancaDati $db;
public function __construct() {
$this->db = new BancaDati();
return $this;
}
public function exists() {
return true;
}
public function loginWithUsername(string $username, string $password) : string {
$userObject = $this->db->select("utente", ["nomeUtente" => $username]);
if(!$userObject){
return false;
}
$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){
$userObject = $this->db->select("utente", ["gettone" => $token]);
if(!$userObject){
return false;
}
$this->id = $userObject["id"];
$this->username = $userObject["nomeUtente"];
$this->email = $userObject["email"];
$this->password = $userObject["parolaDordine"];
$this->token = $token;
return $this;
}
}

View File

@ -3,6 +3,7 @@
require_once("Router/Router.php");
require_once("Router/Response.php");
require_once("BancaDati/BancaDati.php");
require_once("User.php");
use Router\Response;
use Router\Router;
@ -11,16 +12,45 @@ use BancaDati\BancaDati;
$app = new Router("/DirektiveDesDons");
$db = new BancaDati();
$app->use("/", function (array &$req, Response $res) {
if(isset($_COOKIE["TOKEN"])){
$user = new User();
if($user->loginWithToken($_COOKIE["TOKEN"])){
$req["user"] = $user;
}
}
});
$app->get("/", function (array $req, Response $res) {
$res->send("Hello World");
//var_dump($req["user"]);
});
$app->get("/user", function (array $req, Response $res) {
$res->send("user");
});
$app->get("/user/:id", function (array $req, Response $res) {
$app->get("/user/:id", function (array $req, Response $res) use ($db) {
$db->select("utente", ["username" => $req["id"]]);
$res->send("user " . $req["params"]["id"]);
});
$app->post("/createuser", function (array $req, Response $res) use ($db) {
$db->insert("utente", ["email" => "test@email.com", "parolaDordine" => "password", "nomeUtente" => "testuser"]);
$res->send("user ");
});
$app->post("/login", function( array $req, Response $res) use ($db) {
$username = $req["body"]["username"];
$password = $req["body"]["password"];
$user = new User();
$usertoken = $user->loginWithUsername($username, $password);
if($usertoken){
setcookie("TOKEN", $usertoken, time()+3600); // 1h
$res->send("Login successful" . "token: " . $usertoken, 200);
}else{
$res->send("Login failed", 403);
}
});
$app->start();