Skip to content

Commit

Permalink
Merge pull request #19 from uconndxlab/ft/profile
Browse files Browse the repository at this point in the history
Beginnings of subscriptions stuff, just basic proof of concept for now
  • Loading branch information
joelsalisbury authored Dec 1, 2023
2 parents 2bc263f + 8a063ec commit b2dcb32
Show file tree
Hide file tree
Showing 14 changed files with 1,084 additions and 1 deletion.
Binary file added db.sqlite
Binary file not shown.
8 changes: 7 additions & 1 deletion public/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,11 @@ const userSignIn = async() => {
signInWithEmailAndPassword(auth, signInEmail, signInPassword)
.then((userCredential) => {
const user = userCredential.user;
alert("You have signed in successfully!");
// redirect to profile.html
window.location.href = 'profile.html';



})
.catch((error) => {
const errorCode = error.code;
Expand All @@ -83,6 +87,8 @@ const checkAuthState = async() => {
if(user) {
authForm.style.display = 'none';
whenSignedIn.style.display = 'block';
// redirect to profile.html
window.location.href = 'profile.html';
}
else {
authForm.style.display = 'block';
Expand Down
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;
}
127 changes: 127 additions & 0 deletions public/functions.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
<?php

$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';
require_once 'layouts/footer.php';
}

function get_all_crops() {
global $db;
$results = $db->query('SELECT * FROM crops');
$crops = [];
while ($row = $results->fetchArray()) {
$crops[] = $row;
}
return $crops;
}

function get_all_states() {
global $db;
$results = $db->query('SELECT * FROM states');
$states = [];
while ($row = $results->fetchArray()) {
$states[] = $row;
}
return $states;
}

function get_state_name($id) {
global $db;
$results = $db->query('SELECT * FROM states WHERE id = ' . $id);
$row = $results->fetchArray();
return $row['state'];
}

function get_crop_name($id) {
global $db;
$results = $db->query('SELECT * FROM crops WHERE id = ' . $id);
$row = $results->fetchArray();
return $row['crop'];
}

function get_all_deadlines() {
global $db;
$results = $db->query('SELECT * FROM crops_states_deadlines');
$deadlines = [];
while ($row = $results->fetchArray()) {
$row['state'] = get_state_name($row['state_id']);
$row['crop'] = get_crop_name($row['crop_id']);
$deadlines[] = $row;
}
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;
}
32 changes: 32 additions & 0 deletions public/index.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php
session_start();
require_once 'functions.php';

switch($_SERVER['REQUEST_URI']) {
case '/':
do_layout('pages/home');
break;
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;
}
12 changes: 12 additions & 0 deletions public/layouts/footer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@

</main>
<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>


</html>
67 changes: 67 additions & 0 deletions public/layouts/header.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<!doctype html>
<html lang="en">
<head>
<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">

<link rel="stylesheet" href="/css/app.css">
</head>

<body>
<header>
<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" 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 b2dcb32

Please sign in to comment.