Compare commits
2 Commits
feat/login
...
1d80d93b1d
| Author | SHA1 | Date | |
|---|---|---|---|
| 1d80d93b1d | |||
| 151b1ab549 |
@@ -6,7 +6,7 @@ time_zone = "+00:00";
|
|||||||
|
|
||||||
CREATE TABLE `ingredienti`
|
CREATE TABLE `ingredienti`
|
||||||
( /*Zutaten*/
|
( /*Zutaten*/
|
||||||
`id` varchar(36) NOT NULL,
|
`id` int auto_increment NOT NULL,
|
||||||
`cognome` varchar(200) NOT NULL, /*Name*/
|
`cognome` varchar(200) NOT NULL, /*Name*/
|
||||||
`caloriePerCento` integer(5) NOT NULL, /*Kalorien pro Gramm*/
|
`caloriePerCento` integer(5) NOT NULL, /*Kalorien pro Gramm*/
|
||||||
`ilPeso` integer(5) NULL, /*Gewicht*/
|
`ilPeso` integer(5) NULL, /*Gewicht*/
|
||||||
@@ -16,14 +16,14 @@ CREATE TABLE `ingredienti`
|
|||||||
|
|
||||||
CREATE TABLE `folla`
|
CREATE TABLE `folla`
|
||||||
( /*Menge*/
|
( /*Menge*/
|
||||||
`id` varchar(36) NOT NULL,
|
`id` int auto_increment NOT NULL,
|
||||||
`unita` varchar(200) NOT NULL, /*Einheit*/
|
`unita` varchar(200) NOT NULL, /*Einheit*/
|
||||||
`created` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP
|
`created` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP
|
||||||
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
|
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
|
||||||
|
|
||||||
CREATE TABLE `elenco`
|
CREATE TABLE `elenco`
|
||||||
( /*Liste*/
|
( /*Liste*/
|
||||||
`id` varchar(36) NOT NULL,
|
`id` int auto_increment NOT NULL,
|
||||||
`creatore` varchar(200) NOT NULL, /*Ersteller*/
|
`creatore` varchar(200) NOT NULL, /*Ersteller*/
|
||||||
`coloreDiSfondo` integer(10) NOT NULL, /*Hintergrundfarbe*/
|
`coloreDiSfondo` integer(10) NOT NULL, /*Hintergrundfarbe*/
|
||||||
`created` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP
|
`created` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP
|
||||||
@@ -31,7 +31,7 @@ CREATE TABLE `elenco`
|
|||||||
|
|
||||||
CREATE TABLE `utente`
|
CREATE TABLE `utente`
|
||||||
( /*Benutzer*/
|
( /*Benutzer*/
|
||||||
`id` varchar(36) NOT NULL,
|
`id` int auto_increment NOT NULL,
|
||||||
`email` varchar(200) NOT NULL, /*Email*/
|
`email` varchar(200) NOT NULL, /*Email*/
|
||||||
`parolaDordine` varchar(255) NOT NULL, /*Passwort*/
|
`parolaDordine` varchar(255) NOT NULL, /*Passwort*/
|
||||||
`nomeUtente` varchar(50) UNIQUE NOT NULL, /*Benutzernamen*/
|
`nomeUtente` varchar(50) UNIQUE NOT NULL, /*Benutzernamen*/
|
||||||
@@ -41,7 +41,7 @@ CREATE TABLE `utente`
|
|||||||
|
|
||||||
CREATE TABLE `elencoIngredienti`
|
CREATE TABLE `elencoIngredienti`
|
||||||
( /*Liste_Zutaten*/
|
( /*Liste_Zutaten*/
|
||||||
`id` varchar(36) NOT NULL,
|
`id` int auto_increment NOT NULL,
|
||||||
`ingredientiID` varchar(36) NOT NULL, /*ZutatenID*/
|
`ingredientiID` varchar(36) NOT NULL, /*ZutatenID*/
|
||||||
`elencoID` varchar(36) NOT NULL, /*ListeID*/
|
`elencoID` varchar(36) NOT NULL, /*ListeID*/
|
||||||
`follaID` varchar(36) NOT NULL, /*MengeID*/
|
`follaID` varchar(36) NOT NULL, /*MengeID*/
|
||||||
@@ -50,7 +50,7 @@ CREATE TABLE `elencoIngredienti`
|
|||||||
|
|
||||||
CREATE TABLE `utenteElenco`
|
CREATE TABLE `utenteElenco`
|
||||||
( /*Benutzer_Liste*/
|
( /*Benutzer_Liste*/
|
||||||
`id` varchar(36) NOT NULL,
|
`id` int auto_increment NOT NULL,
|
||||||
`elencoID` varchar(36) NOT NULL, /*ListeID*/
|
`elencoID` varchar(36) NOT NULL, /*ListeID*/
|
||||||
`utenteID` varchar(36) NOT NULL, /*BenutzerID*/
|
`utenteID` varchar(36) NOT NULL, /*BenutzerID*/
|
||||||
`created` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP
|
`created` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP
|
||||||
|
|||||||
@@ -30,7 +30,7 @@ class Router
|
|||||||
$this->request = $_SERVER;
|
$this->request = $_SERVER;
|
||||||
}
|
}
|
||||||
if (isset($_POST)) {
|
if (isset($_POST)) {
|
||||||
$this->request["body"] = json_decode(file_get_contents('php://input'), true);
|
$this->request["body"] = $_POST;
|
||||||
}
|
}
|
||||||
if (isset($_POST)) {
|
if (isset($_POST)) {
|
||||||
$this->request["params"] = $_GET;
|
$this->request["params"] = $_GET;
|
||||||
|
|||||||
53
User.php
53
User.php
@@ -1,53 +0,0 @@
|
|||||||
<?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;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
32
index.php
32
index.php
@@ -3,7 +3,6 @@
|
|||||||
require_once("Router/Router.php");
|
require_once("Router/Router.php");
|
||||||
require_once("Router/Response.php");
|
require_once("Router/Response.php");
|
||||||
require_once("BancaDati/BancaDati.php");
|
require_once("BancaDati/BancaDati.php");
|
||||||
require_once("User.php");
|
|
||||||
|
|
||||||
use Router\Response;
|
use Router\Response;
|
||||||
use Router\Router;
|
use Router\Router;
|
||||||
@@ -12,45 +11,16 @@ use BancaDati\BancaDati;
|
|||||||
$app = new Router("/DirektiveDesDons");
|
$app = new Router("/DirektiveDesDons");
|
||||||
$db = new BancaDati();
|
$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) {
|
$app->get("/", function (array $req, Response $res) {
|
||||||
$res->send("Hello World");
|
$res->send("Hello World");
|
||||||
//var_dump($req["user"]);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
$app->get("/user", function (array $req, Response $res) {
|
$app->get("/user", function (array $req, Response $res) {
|
||||||
$res->send("user");
|
$res->send("user");
|
||||||
});
|
});
|
||||||
$app->get("/user/:id", function (array $req, Response $res) use ($db) {
|
$app->get("/user/:id", function (array $req, Response $res) {
|
||||||
$db->select("utente", ["username" => $req["id"]]);
|
|
||||||
$res->send("user " . $req["params"]["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();
|
$app->start();
|
||||||
Reference in New Issue
Block a user