Make login work

This commit is contained in:
Li 2023-10-29 20:56:58 +13:00
parent 1700df921f
commit 2a17c387a3
20 changed files with 254 additions and 19 deletions

View File

@ -1,6 +1,7 @@
a {
color: black;
font-weight: bold;
text-decoration: none;
}
@ -8,7 +9,6 @@ a:hover{
text-decoration: underline;
}
.heading {
font-size: 200%;
font-weight: bold;
@ -18,4 +18,25 @@ a:hover{
margin-left: 30%;
margin-right: 30%;
font-size: 130%;
}
.textinput{
width: 100%;
padding: 10px;
background-color: #f7f7f7;
border-radius: 10px;
border-color: gray;
}
.button{
padding: 10px;
background-color: #f7f7f7;
border-radius: 10px;
border-color: gray;
}
.button:hover{
background-color: gray;
border-color: black;
color: white;
}

View File

@ -50,8 +50,9 @@ html, body {
}
.navbar a {
display: table-row;
display: contents;
text-decoration: none;
white-space: nowrap;
}
.navbar #entry {

1
errors/errorFoot.php Normal file
View File

@ -0,0 +1 @@
<hr/>

1
errors/errorHead.php Normal file
View File

@ -0,0 +1 @@
<hr/>

View File

@ -0,0 +1,9 @@
<?php include("../errorHead.php"); ?>
<span class="heading">
<p>No System exists on your account.</p>
</span>
<span class="info">
<p>Maybe create a system?</p>
</span>
<?php include("../errorFoot.php"); ?>

View File

@ -0,0 +1,9 @@
<?php include("../errorHead.php"); ?>
<span class="heading">
<p>Username already exists.</p>
</span>
<span class="info">
<p>Please try again with a different username.</p>
</span>
<?php include("../errorFoot.php"); ?>

View File

@ -0,0 +1,9 @@
<?php include("../errorHead.php"); ?>
<span class="heading">
<p>Username doesn't exist.</p>
</span>
<span class="info">
<p>Please try again with a different username.</p>
</span>
<?php include("../errorFoot.php"); ?>

View File

@ -0,0 +1,9 @@
<?php include("../errorHead.php"); ?>
<span class="heading">
<p>Password is incorrect.</p>
</span>
<span class="info">
<p>Please enter the correct password.</p>
</span>
<?php include("../errorFoot.php"); ?>

View File

@ -1,4 +1,6 @@
<?php include("consts.php"); ?>
<?php include("lib/all.php"); ?>
<?php session_start() ?>
<!DOCTYPE HTML>
<html>
@ -41,15 +43,9 @@
</div>
<div class="navbar">
<a href="/login.php"> <div id="entry">Login</div> </a>
<?php include("navbar.php"); ?>
</div>
<?php
?>
</div>
<div class="content">

BIN
img/Thumbs.db Normal file

Binary file not shown.

5
lib/all.php Normal file
View File

@ -0,0 +1,5 @@
<?php
include("sql.php");
include("users.php");
include("hlp.php");
?>

8
lib/hlp.php Normal file
View File

@ -0,0 +1,8 @@
<?php
function redirect(string $page){
header("Location: ".$page);
exit();
}
?>

View File

@ -1,13 +1,10 @@
<?php
include("sqlcfg.php");
function createTables($db) {
mysql_query($db, "CREATE TABLE IF NOT EXISTS Users(Id INT NOT NULL AUTO_INCREMENT, Username TEXT, PasswordHash TEXT, PRIMARY KEY(Id))");
mysql_query($db, "CREATE TABLE IF NOT EXISTS Systems(Id INT NOT NULL AUTO_INCREMENT, IsPluralSystem BOOL, SubSystem INT, UserCreated INT, PRIMARY KEY(Id))");
mysql_query($db, "CREATE TABLE IF NOT EXISTS Members(Id INT NOT NULL AUTO_INCREMENT, FromSystem INT, PRIMARY KEY(Id))");
function createTables($conn) {
$conn->query("CREATE TABLE IF NOT EXISTS Users(Id INT NOT NULL AUTO_INCREMENT, Username TEXT, PasswordHash TEXT, PRIMARY KEY(Id))");
$conn->query("CREATE TABLE IF NOT EXISTS Systems(Id INT NOT NULL AUTO_INCREMENT, Name TEXT, IsPluralSystem BOOL, SubSystem INT, UserCreated INT, PRIMARY KEY(Id))");
$conn->query("CREATE TABLE IF NOT EXISTS Members(Id INT NOT NULL AUTO_INCREMENT, Name TEXT, Pronouns TEXT, FromSystem INT, PRIMARY KEY(Id))");
}
@ -26,6 +23,33 @@ function connect(){
return $conn;
}
function userExist(string $username) {
$conn = connect();
$prep = $conn->prepare("SELECT COUNT(*) FROM Users WHERE Username=?");
$prep->bind_param("s", $username);
$prep->execute();
return intval($prep->get_result()->fetch_row()[0]);
}
function createUser(string $username, string $password) {
$hashedPassword = password_hash($password, PASSWORD_BCRYPT);
$conn = connect();
$prep = $conn->prepare("INSERT INTO Users VALUES(NULL, ?, ?)");
$prep->bind_param("ss", $username, $hashedPassword);
$prep->execute();
}
function verifyLogin(string $username, string $passwordAttempt){
$conn = connect();
$prep = $conn->prepare("SELECT PasswordHash FROM Users WHERE Username=?");
$prep->bind_param("s", $username);
$prep->execute();
$expectedHash = $prep->get_result()->fetch_row()[0];
return password_verify($passwordAttempt, $expectedHash);
}
?>

0
lib/system.php Normal file
View File

32
lib/users.php Normal file
View File

@ -0,0 +1,32 @@
<?php
function logout() {
$_SESSION["loggedIn"] = false;
session_destroy();
}
function login(string $username) {
$_SESSION["username"] = $username;
$_SESSION["loggedIn"] = true;
}
function getUsername() {
return htmlspecialchars($_SESSION["username"], ENT_QUOTES);
}
function isLoggedIn() {
if(isset($_SESSION["loggedIn"])){
return $_SESSION["loggedIn"];
}
else {
return false;
}
}
function requireLogin() {
if(!isLoggedIn()) {
redirect("/login.php");
}
}
?>

View File

@ -1,5 +1,30 @@
<?php include("head.php"); ?>
<?php
/* handle form */
if(isset($_POST["name"], $_POST["password"])){
$name = $_POST["name"];
$password = $_POST["password"];
if(userExist($name)) {
if(verifyLogin($name, $password)) {
login($name);
redirect("/user.php");
}
else{
include("errors/users/userPasswordIncorrect.php");
}
}
else{
include("errors/users/userNotExists.php");
}
}
?>
<span class="heading">
<p>Login</p>
</span>
@ -10,10 +35,15 @@
<div class="info">
<form action="login.php" method="post">
<p><input type="text" name="name" placeholder="Username"/></p>
<p><input type="password" name="password" placeholder="Password"/></p>
<p><input type="submit"/></p>
<p><input class="textinput" type="text" name="name" placeholder="Username"/></p>
<p><input class="textinput" type="password" name="password" placeholder="Password"/></p>
<p><input class="button" type="submit" value="Login"/></p>
</form>
</div>
<span class="info">
<p>Don't have an account? <a href="/register.php">Create an Account</a></p>
</span>
<?php include("foot.php"); ?>

8
logout.php Normal file
View File

@ -0,0 +1,8 @@
<?php include("head.php"); ?>
<?php
logout();
redirect("/login.php");
?>
<?php include("foot.php"); ?>

9
navbar.php Normal file
View File

@ -0,0 +1,9 @@
<?php
if(isLoggedIn()){
echo(' <a href="/user.php"> <div id="entry">'.getUsername().'</div> </a>');
echo(' <a href="/logout.php"> <div id="entry">Logout</div> </a>');
}
else{
echo(' <a href="/login.php"> <div id="entry">Login</div> </a>');
}
?>

44
register.php Normal file
View File

@ -0,0 +1,44 @@
<?php include("head.php"); ?>
<?php
/* handle form */
if(isset($_POST["name"], $_POST["password"])){
$name = $_POST["name"];
$password = $_POST["password"];
if(!userExist($name)) {
createUser($name, $password);
login($name);
redirect("/user.php");
}
else{
include("errors/users/userExists.php");
}
}
?>
<span class="heading">
<p>Create an Account</p>
</span>
<span class="info">
<p>Enter a username and password:</p>
</span>
<div class="info">
<form action="register.php" method="post">
<p><input class="textinput" type="text" name="name" placeholder="Username"/></p>
<p><input class="textinput" type="password" name="password" placeholder="Password"/></p>
<p><input class="button" type="submit" value="Create Account"/></p>
</form>
</div>
<span class="info">
<p>Already got an account? <a href="/login.php">Login</a></p>
</span>
<?php include("foot.php"); ?>

19
user.php Normal file
View File

@ -0,0 +1,19 @@
<?php include("head.php"); ?>
<?php requireLogin(); ?>
<span class="heading">
<p>Welcome <?php echo(getUsername()); ?>!</p>
</span>
<span id="info">
<p>Here you can put information about your plural system.</p>
</span>
<hr/>
<span class="heading">
<p>Systems:</p>
</span>
<?php include("foot.php"); ?>