diff --git a/public/action.php b/public/action.php index b27f059..4bb46d3 100644 --- a/public/action.php +++ b/public/action.php @@ -29,32 +29,79 @@ switch ($VARS['action']) { } else { $cart[$item] += $qty; } - $_SESSION['cart'] = $cart; - header('Location: ./?page=cart&msg=itemadded'); die(); - break; case "updatecart": $item = $VARS['item']; $qty = $VARS['qty']; - $cart = []; - if (!empty($_SESSION['cart'])) { $cart = $_SESSION['cart']; } - $cart[$item] = $qty; - if ($qty <= 0) { unset($cart[$item]); } - $_SESSION['cart'] = $cart; - header('Location: ./?page=cart&msg=itemupdated'); + break; + case "login": + $email = $VARS['email']; + $password = $VARS['password']; + if (!filter_var($email, FILTER_VALIDATE_EMAIL)) { + header('Location: ./?page=login&msg=invalidemail'); + die("Invalid email address."); + } + if ($database->has('customers', ['email' => $email])) { + $hash = $database->get('customers', 'password', ['email' => $email]); + if (password_verify($password, $hash)) { + $_SESSION['shop_account'] = $database->get('customers', ['customerid (id)', 'name', 'password (hashed_password)', 'email'], ['email' => $email]); + header('Location: ./?page=account'); + die(); + } else { + header('Location: ./?page=login&msg=badlogin'); + die("Bad login."); + } + } else { + header('Location: ./?page=login&msg=badlogin'); + die("Bad login."); + } + break; + case "logout": + $_SESSION['shop_account'] = null; + header('Location: ./'); + break; + case "signup": + $name = $VARS['name']; + $email = $VARS['email']; + $password = $VARS['password']; + $phone = $VARS['phone']; + + if (empty($name) || empty($email) || empty($password)) { + header('Location: ./?page=signup&msg=missingdata'); + die("Missing required data."); + } + if (!filter_var($email, FILTER_VALIDATE_EMAIL)) { + header('Location: ./?page=signup&msg=invalidemail'); + die("Invalid email address."); + } + + if ($database->has('customers', ['OR' => ['name' => $name, 'email' => $email]])) { + header('Location: ./?page=signup&msg=accountinuse'); + die("Name or email already in use."); + } + + if (empty($phone)) { + $phone = null; + } + + $database->insert('customers', ['name' => $name, 'email' => $email, 'password' => password_hash($password, PASSWORD_BCRYPT), 'phone' => $phone]); + + $_SESSION['shop_account'] = $database->get('customers', ['name', 'password (hashed_password)', 'email'], ['email' => $email]); + header('Location: ./?page=account'); + die(); break; } \ No newline at end of file diff --git a/public/index.php b/public/index.php index 14779ab..460579d 100644 --- a/public/index.php +++ b/public/index.php @@ -46,6 +46,15 @@ if (isset($_GET['page'])) { case "cart": $page = "cart"; break; + case "account": + $page = "account"; + break; + case "login": + $page = "login"; + break; + case "signup": + $page = "signup"; + break; case "home": default: $page = "home"; diff --git a/public/parts/account.php b/public/parts/account.php new file mode 100644 index 0000000..303d403 --- /dev/null +++ b/public/parts/account.php @@ -0,0 +1,75 @@ + + +