From 5b3f5a11230f278cadf4510c53881e37dde6cc39 Mon Sep 17 00:00:00 2001 From: Patryk Hegenberg Date: Sun, 8 Jan 2023 13:51:00 +0100 Subject: [PATCH] added login, logout and registration sites and functionality as well as an protected profile page --- config.php | 4 ++-- controllers/login.php | 29 +++++++++++++++++++++++++-- controllers/logout.php | 4 ++++ controllers/profile.php | 8 ++++++++ controllers/register.php | 39 +++++++++++++++++++++++++++++++++++++ index.php | 5 +++++ routes.php | 5 ++++- views/index.view.php | 2 +- views/login.view.php | 12 ++++++------ views/partials/nav.php | 31 ++++++++++++++--------------- views/profile.view.php | 10 ++++++++++ views/register.view.php | 42 ++++++++++++++++++++++++++++++++++++++++ 12 files changed, 163 insertions(+), 28 deletions(-) create mode 100644 controllers/logout.php create mode 100644 controllers/profile.php create mode 100644 controllers/register.php create mode 100644 views/profile.view.php create mode 100644 views/register.view.php diff --git a/config.php b/config.php index a344aca..dbc0544 100644 --- a/config.php +++ b/config.php @@ -4,7 +4,7 @@ return [ 'database' => [ 'host' => 'localhost', 'port' => 3306, - 'dbname' => 'myapp', + 'dbname' => 'MatheApp', 'charset' => 'utf8mb4' ] -]; \ No newline at end of file +]; diff --git a/controllers/login.php b/controllers/login.php index d9af2fd..3efc7a8 100644 --- a/controllers/login.php +++ b/controllers/login.php @@ -1,3 +1,28 @@ query("SELECT * FROM user WHERE username = :user", ['user' => $_POST["username"]])->get(); + $count = sizeof($stmt); + if ($count == 1) { + $stmt = $stmt[0];; + + if (password_verify($_POST['password'], $stmt["password"])) { + session_start(); + $_SESSION["username"] = $stmt['username']; + //require "views/profile.php"; + //urlIs("/profile"); + header("Location: /profile"); + } else { + echo "Anmeldung fehlgeschlagen!"; + } + } else { + echo "Anmeldung fehlgeschlagen!"; + } +} + + require "views/login.view.php"; diff --git a/controllers/logout.php b/controllers/logout.php new file mode 100644 index 0000000..6c6f91c --- /dev/null +++ b/controllers/logout.php @@ -0,0 +1,4 @@ +query("SELECT * FROM user WHERE username = :user", ['user' => $_POST['Username']])->get(); + $count = sizeof($stmt); + if($count == 0 && Validator::string($_POST['Username'], 1, 255)){ + $checkEmail = $db->query("SELECT * FROM user WHERE email = :email", ['email' => $_POST['Email-Adresse']])->find(); + if(!$checkEmail && Validator::string($_POST['Email-Adresse'], 1, 255)) { + if($_POST["Passwort"] == $_POST["pw2"] && Validator::string($_POST['Passwort'], 8, 255)) { + //Username ist frei + //User anlegen + $hash = password_hash($_POST["Passwort"], PASSWORD_BCRYPT); + $db->query("INSERT INTO user (username, vorname, nachname, email, password, level, xp, coins) VALUES ( + :username, :vorname, :nachname, :email, :password, :level, :xp, :coins )", [ + 'username' => $_POST['Username'], + 'vorname' => $_POST['Vorname'], + 'nachname' => $_POST['Nachname'], + 'email' => $_POST['Email-Adresse'], + 'password' => $hash, + 'level' => 1, + 'xp' => 0, + 'coins' => 0 + ]); + } else { + echo "Die Passwörter stimmen nicht überein"; + } + } else { + echo "Der Username ist bereits vergeben"; + } +} +} + + require "views/register.view.php"; diff --git a/index.php b/index.php index 6b040be..18d0ddf 100644 --- a/index.php +++ b/index.php @@ -4,4 +4,9 @@ require 'Database.php'; require 'router.php'; require 'Response.php'; +$username = 'MatheApp'; +$password = 'password'; +$config = require('config.php'); +$db = new Database($config['database'], $username, $password); + //dd($posts); diff --git a/routes.php b/routes.php index bb07a2c..51df8e9 100644 --- a/routes.php +++ b/routes.php @@ -5,5 +5,8 @@ return[ '/mathe' => 'controllers/mathe.php', '/game' => 'controllers/game.php', '/addition' => 'controllers/addition.php', - '/login' => 'controllers/login.php' + '/login' => 'controllers/login.php', + '/register' => 'controllers/register.php', + '/profile' => 'controllers/profile.php', + '/logout' => 'controllers/logout.php' ]; diff --git a/views/index.view.php b/views/index.view.php index 5564b6b..a56571d 100644 --- a/views/index.view.php +++ b/views/index.view.php @@ -12,7 +12,7 @@

Automatisiere spielerisch das 1x1

Zeige was du kannst und kämpfe dich durch denn Dungeon.

- nes-btn is-primary"> + nes-btn is-primary"> Los geht's diff --git a/views/login.view.php b/views/login.view.php index 91b40f6..fe59a62 100644 --- a/views/login.view.php +++ b/views/login.view.php @@ -3,21 +3,21 @@
-
+
- - + +
- +
-
@@ -44,14 +44,14 @@
+
+ +
+
+