log_in.php 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  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. if(!$user->activated) {
  19. $user->sendActivationEmail();
  20. throw new Exception("Your email address is not verified. An activation email has been sent to your email address.");
  21. }
  22. $_SESSION["user_id"] = $user->id;
  23. $redirect = $_SESSION["log_in_redirect_uri"] ?? "/account";
  24. Core::addAlert("success", "Log in successful.");
  25. HTTP::redirect($redirect);
  26. }
  27. catch(Exception $e) {
  28. Core::addAlert("danger", $e->getMessage());
  29. }
  30. }
  31. View::includeOnce("header", compact("page_title"));
  32. View::include("log_in");
  33. View::includeOnce("footer");
  34. ?>