log_in.php 1021 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. <?php
  2. $page_title = "Log In";
  3. Core::addBreadcrumb("/log-in", "Log In");
  4. $submit = http::value("submit");
  5. $csrf_token = $_SESSION["csrf_token"];
  6. if($submit) {
  7. try {
  8. Core::validateCSRFToken($submit);
  9. $email = http::value("email", "email");
  10. $password = http::value("password");
  11. if(empty($email)) throw new Exception("Email address is invalid.");
  12. if(empty($password)) throw new Exception("Password must be given.");
  13. $user = User::queryOne([
  14. "email" => db::escape($email)
  15. ]);
  16. if(!$user) throw new Exception("Account not found.");
  17. if(!password_verify($password, $user->password)) throw new Exception("Password is incorrect.");
  18. $_SESSION["user_id"] = $user->id;
  19. $redirect = $_SESSION["log_in_redirect_uri"] ?? "/account";
  20. Core::addAlert("success", "Log in successful.");
  21. HTTP::redirect($redirect);
  22. }
  23. catch(Exception $e) {
  24. Core::addAlert("danger", $e->getMessage());
  25. }
  26. }
  27. View::includeOnce("header", compact("page_title"));
  28. View::include("log_in");
  29. View::includeOnce("footer");
  30. ?>