Skip to content

Commit

Permalink
login stuff works now
Browse files Browse the repository at this point in the history
  • Loading branch information
joelsalisbury committed Dec 1, 2023
1 parent 98df66c commit 8a063ec
Show file tree
Hide file tree
Showing 11 changed files with 447 additions and 133 deletions.
Binary file modified db.sqlite
Binary file not shown.
25 changes: 25 additions & 0 deletions public/css/app.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/** footer is always at the bottom of the page */
body {
/* Your CSS properties here */
overflow-y: scroll;
display: flex;
flex-direction: column;
min-height: 100vh;
}

/** make the h1-h6 a little smaller than normal */
h1 {
font-size:1.75em;
}
h2 {
font-size: 1.5rem;
}

main {
flex: 1;
}

footer {
flex-shrink: 0;
background-color: #f5f5f5;
}
72 changes: 72 additions & 0 deletions public/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,70 @@

$db = new SQLite3('../db.sqlite');

function do_register() {
global $db;
$firstname = $_POST['firstname'];
$lastname = $_POST['lastname'];
$email = $_POST['email'];
$password = $_POST['password'];
$password_confirm = $_POST['password_confirm'];
if ($password !== $password_confirm) {
$_SESSION['error'] = 'Passwords do not match';
header('Location: /');
exit;
}

if (user_exists($email)) {
$_SESSION['error'] = 'User already exists';
header('Location: /');
exit;
}
$password = password_hash($password, PASSWORD_DEFAULT);
$sql = "INSERT INTO users (firstname, lastname, email, password) VALUES ('$firstname', '$lastname', '$email', '$password')";

$db->exec($sql);
$_SESSION['user_id'] = $db->lastInsertRowID();
$_SESSION['firstname'] = $firstname;
$_SESSION['success'] = 'You are now registered and logged in as ' . $email;
header('Location: /profile');
exit;
}

function user_exists($email) {
global $db;
$results = $db->query("SELECT * FROM users WHERE email = '$email'");
$row = $results->fetchArray();
if ($row) {
return true;
}
return false;
}

function do_login() {
global $db;
$email = $_POST['email'];
$password = $_POST['password'];
$results = $db->query("SELECT * FROM users WHERE email = '$email'");
$row = $results->fetchArray();
if (password_verify($password, $row['password'])) {
$_SESSION['user_id'] = $row['id'];
$_SESSION['firstname'] = $row['firstname'];
$_SESSION['success'] = 'You are now logged in as ' . $row['email'];
header('Location: /profile');
exit;
} else {
$_SESSION['error'] = 'Invalid email or password';
header('Location: /');
exit;
}
}

function do_logout() {
session_destroy();
header('Location: /');
exit;
}

function do_layout($file) {
require_once 'layouts/header.php';
require_once $file . '.php';
Expand Down Expand Up @@ -53,3 +117,11 @@ function get_all_deadlines() {
}
return $deadlines;
}

function get_logged_in_user() {
global $db;
$user_id = $_SESSION['user_id'];
$results = $db->query('SELECT * FROM users WHERE id = ' . $user_id);
$row = $results->fetchArray();
return $row;
}
23 changes: 21 additions & 2 deletions public/index.php
Original file line number Diff line number Diff line change
@@ -1,13 +1,32 @@
<?php
session_start();
require_once 'functions.php';

switch($_SERVER['REQUEST_URI']) {
case '/':
do_layout('pages/home');
break;
case '/otherstuff':
echo 'goodbye';
case '/profile':
do_layout('pages/profile');
break;
case '/post/login':
do_login();
break;
case '/logout':
do_logout();
break;
case '/post/register':
do_register();
break;
case '/login':
do_layout('pages/login');
break;
case '/register':
do_layout('pages/register');
break;
default:
// set http response code to 404
http_response_code(404);
echo '404';
break;
}
7 changes: 4 additions & 3 deletions public/layouts/footer.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@

</main>
<footer>
<p> &copy;
<?php echo date('Y'); ?> New England Crop Insurance
<footer class="bg-dark text-light py-5">
<div class="container">
<p> &copy; <?php echo date('Y'); ?> New England Crop Insurance </p>
</div>
</footer>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js" integrity="sha384-MrcW6ZMFYlzcLA8Nl+NtUVF0sA7MsXsP1UyJoMp4YLEuNSfAP+JcXn/tWtIaxVXM" crossorigin="anonymous"></script>
</body>
Expand Down
58 changes: 50 additions & 8 deletions public/layouts/header.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
<meta charset="UTF-8">
<title>New England Crop Insurance Stuff</title>



<!-- bootstrap 5 CDN -->
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC" crossorigin="anonymous">

Expand All @@ -12,14 +14,54 @@

<body>
<header>
<h1>New England Crop Insurance</h1>
<nav>
<ul>
<li><a href="/">Management Dashboard</a></li>
<li><a href="/about">My Profile</a></li>
</ul>
</nav>
<nav class="navbar navbar-expand-lg navbar-light bg-light">
<div class="container-fluid">
<a class="navbar-brand" href="#">Crop Insurance Alerts</a>
<button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarSupportedContent">
<ul class="navbar-nav me-auto mb-2 mb-lg-0">
<li class="nav-item">
<a class="nav-link active" aria-current="page" href="/">Management Dashboard</a>
</li>
<li class="nav-item">
<a class="nav-link" href="/profile">My Grower Profile</a>
</li>
</ul>
</div>
<!-- login/logout -->
<div class="d-flex">
<?php if (isset($_SESSION['user_id'])) : ?>
<span class="me-3">Welcome, <?php echo $_SESSION['firstname']; ?></span>
<a href="/logout" class="btn btn-danger">Logout</a>=
<?php else : ?>
<a href="/login" class="btn btn-primary me-2">Login</a>
<a href="/register" class="btn btn-secondary">Register</a>
<?php endif; ?>
</div>
</div>
</nav>
</header>

<main id="app">
<main id="app" class="mt-4 mb-4">
<?php if (isset($_SESSION['error'])) : ?>
<div class="alert alert-danger" role="alert">
<?php
echo $_SESSION['error'];
unset($_SESSION['error']);
?>

</div>
<?php endif; ?>

<?php if (isset($_SESSION['success'])) : ?>
<div class="alert alert-success" role="alert">
<?php
echo $_SESSION['success'];
unset($_SESSION['success']);
?>

</div>
<?php endif; ?>

Loading

0 comments on commit 8a063ec

Please sign in to comment.