Login functionality updated

This commit is contained in:
Minik Gaarde Lambrecht 2021-03-26 08:35:51 +01:00
parent 067ec202d0
commit 673af052d5
12 changed files with 322 additions and 58 deletions

View File

@ -8,3 +8,12 @@ function isAdmin(): bool
return isset($_SESSION['admin']);
}
function makeAdminLogin(string $userName)
{
setcookie("userName", $userName, [
'expires' => 0,
'samesite' => 'Strict',
'path' => '/'
]);
}

View File

@ -19,6 +19,7 @@ if(isset($_POST['aLogin'])){
$_SESSION['userName'] = $userName;
$_SESSION['admin'] = true;
$_SESSION['success'] = "You are now logged in";
makeAdminLogin($userName);
http_response_code(200);
}else{
session_destroy();

View File

@ -1,9 +1,10 @@
<?php
session_start();
session_unset();
session_destroy();
$cookieCon = array(
'expires' => -1,
'samesite' => 'Strict',
@ -11,5 +12,6 @@ $cookieCon = array(
);
setcookie("groupName", null, $cookieCon);
setcookie("groupId", null, $cookieCon);
setcookie("userName", null, $cookieCon);
echo http_response_code(200);

98
Backend/setup.php Normal file
View File

@ -0,0 +1,98 @@
<?php
//use Illuminate\Support\Facades\DB;
use \Backend\Models\AdminUser;
use JetBrains\PhpStorm\ArrayShape;
/**
* @param string $servername
* @param string $username
* @param string $password
* @param string $DBName
* @return string[]
*/
function myDB(string $servername, string $username, string $password, string $DBName): array
{
$conn = new mysqli($servername, $username, $password);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "CREATE DATABASE ".$DBName;
if ($conn->query($sql) === TRUE) {
echo "Database created successfully";
} else {
echo "Error creating database: " . $conn->error;
}
$conn->close();
return array(
"driver" => "mysql",
"host" => $servername,
"database" => $DBName,
"username" => $username,
"password" => $password
);
}
/**
* @param string $DBName
* @return string[]
*/
function liteDB(string $DBName): array
{
New SQLite3("Database/".$DBName.".sqlite");
$test = array(
'driver' => 'sqlite',
'database' => realpath(dirname(__FILE__)."/Database/".$DBName.".sqlite")
//'Backend/Database/'.$DBName.'.sqlite'
);
var_dump($test);
return $test;
}
$servername = "localhost";
$username = "root";
$password = "V#_xWL6_";
$DBName = "TestDB";
if(isset($_POST["dbSetup"])){
switch($_POST["dbType"]){
case "mysql":
$dbCon = myDB($_POST["dbServername"],$_POST["dbUsername"],$_POST["dbPassword"], $_POST["dbName"]);
break;
case "sqlite":
$dbCon = liteDB($_POST["dbName"]);
break;
default:
die("wrong dbType");
}
file_put_contents("../config/database.json", json_encode($dbCon));
require_once('../bootstrap.php');
try{
$capsule->Connection()->getPdo();
} catch (\Exception $e) {
//file_put_contents("../config/database.json", NULL);
die("Could not connect to the database. Please check your configuration. error:" . $e );
}
require_once ('Database/databaseMigration.php');
AdminUser::firstOrCreate([
'user_name' => $_POST["AdminUsername"], 'password' => password_hash($_POST["AdminPassword"],PASSWORD_DEFAULT)
]);
}else{
echo "not set";
}

View File

@ -28,6 +28,11 @@
<body>
<div class="btn-group" role="group" aria-label="Basic example">
<button type="button" class="btn btn-primary">Left</button>
<button type="button" class="btn btn-primary">Middle</button>
<button type="button" class="btn btn-primary">Right</button>
</div>
<div class="HeaderPanel">
<div class="HeaderLeft">
<a href="../Index.html">

View File

View File

@ -84,11 +84,15 @@
</a>
</li>
</ul>
<a id="NavLogin" type="button" class="nav-link LoginButton" data-bs-toggle="modal" data-bs-target="#LoginModal" style="display: block;">
<a id="AdminPanel" type="button" class="nav-link RightNavButton" style="display: none;">
Admin Panel
</a>
<a id="NavLogin" type="button" class="nav-link RightNavButton" data-bs-toggle="modal" data-bs-target="#LoginModal" style="display: block;">
Login
</a>
<a id="NavUser" type="button" class="nav-link LoginButton" style="display: none;">
<a id="NavUser" class="nav-link RightNavText" style="display: none;"></a>
<a id="UserLogout" type="button" class="nav-link RightNavButton" style="display: none;">
Logout
</a>
</div>
</div>
@ -129,7 +133,9 @@
<input id="loginUsername" type="text" name="groupName" placeholder="Gruppe navn">
<input id="loginPassword" type="password" name="password" placeholder="Password">
<input id="LoginBtn" type="submit" name="login" value="Login">
<input id="AdminLoginBtn" type="submit" name="aLogin" value="Login som admin">
</form>
</div>
<div class="tab-pane fade" id="Registrer" role="tabpanel" aria-labelledby="RegistrerTab">

View File

@ -1,28 +1,36 @@
$(document).ready(function () {
// Register Start
let selectedGameJam;
let MemberSize = $('#NUDDisplay').text();
let displayValue = 0;
let arr = [
let AvailableJams = [
{"id": "-1", "Gamejam": "Vælg aktivt GameJam"}
];
let GroupName = getCookie('groupName');
let GroupId = getCookie('groupId');
// Control Logged in users
if (GroupId != null)
{
LoggedInUser();
}
//
// Register Start
// Populate select with options from the database
axios.get('/Backend/Controllers/GameJam/GetGameJam.php')
.then(function(res) {
let resArr = res.data;
let resArr = res.data.data;
//console.log(res.data.data);
for(let i = 0; i < res.data.data.length; i++)
for(let i = 0; i < resArr.length; i++)
{
arr.push({
id: res.data.data[i].id,
Gamejam: res.data.data[i].name
AvailableJams.push({
id: resArr[i].id,
Gamejam: resArr[i].name
});
}
$.each(arr, function(i, data) {
$.each(AvailableJams, function(i, data) {
if (i === 0)
{
$('#GameJamSelect').append('<option disabled selected value="' + data.id + '">' + data.Gamejam + '</option>');
@ -154,7 +162,15 @@ $(document).ready(function () {
axios.post(URL, formData, {
header: 'multipart/form-data'
}).then(res => {
console.log(res);
if (res.status == 201)
{
let LoginModalElem = document.getElementById('LoginModal')
let LoginModal = bootstrap.Modal.getInstance(LoginModalElem);
LoginModal.hide();
LoggedInUser();
}
}).catch(error => {
console.log(error);
});
@ -189,20 +205,14 @@ $(document).ready(function () {
$('#NUDDisplay').text(displayValue);
}
});
async function GetGroupNames() {
const res = await axios.get('/Backend/Controllers/Group/GetGroup.php');
return res.data;
}
// Register end
// Login start
$('#LoginForm').submit(function(e) {
let URL = "/Backend/Controllers/Group/Login.php";
$('#loginUsername').attr('name') = "groupName";
let form = $('#LoginForm')[0];
let formData = new FormData(form);
@ -211,13 +221,17 @@ $(document).ready(function () {
formData.append(id, value);
axios.post(URL, formData, {
header: 'multipart/form-data'
}).then(res => {
if (res.status === 200)
{
console.log('Logged in');
let LoginModalElem = document.getElementById('LoginModal')
let LoginModal = bootstrap.Modal.getInstance(LoginModalElem);
LoginModal.hide();
LoggedInUser();
}
}).catch(error => {
console.log(error);
@ -226,4 +240,94 @@ $(document).ready(function () {
e.preventDefault();
})
// Login end
// Admin Login Start
$('#AdminLoginBtn').submit(function(e) {
let URL = "/Backend/Controllers/Admin/AdminLogin.php";
$('#loginUsername').attr('name') = "userName";
let form = $('#LoginForm')[0];
let formData = new FormData(form);
let id = $('#LoginBtn').attr('name');
let value = $('#LoginBtn').val();
formData.append(id, value);
axios.post(URL, formData, {
header: 'multipart/form-data'
}).then(res => {
if (res.status === 200)
{
let LoginModalElem = document.getElementById('LoginModal')
let LoginModal = bootstrap.Modal.getInstance(LoginModalElem);
LoginModal.hide();
LoggedInUser();
}
}).catch(error => {
console.log(error);
});
e.preventDefault();
});
// Admin Login End
// Logout Start
$('#UserLogout').click(function() {
axios.get('/Backend/Controllers/Group/Logout.php').then(res => {
if(res.status === 200)
{
UserLoggedOut();
}
}).catch(error => {
console.log(error);
});
});
// Logout End
// Functions Start
function LoggedInUser() {
$('#NavLogin').css({
"display": "none"
});
$('#UserLogout').css({
"display": "block"
});
$('#NavUser').text(`Logget ind som: ${GroupName}`);
$('#NavUser').css({
"display": "block"
});
}
function UserLoggedOut() {
$('#NavLogin').css({
"display": "block"
});
$('#UserLogout').css({
"display": "None"
});
$('#NavUser').css({
"display": "none"
});
}
async function GetGroupNames() {
const res = await axios.get('/Backend/Controllers/Group/GetGroup.php');
return res.data;
}
function getCookie(name) {
const value = `; ${document.cookie}`;
const parts = value.split(`; ${name}=`);
if (parts.length === 2) return parts.pop().split(';').shift();
}
// Functions End
});

19
Frontend/Javascript/qweData.js Executable file
View File

@ -0,0 +1,19 @@
$(window).on("load", function(){
dataFetch();
});
function dataFetch(){
$.ajax({
type: "GET",
url: "/Game-Jaming/Backend/controllers/GameJam/GetGameJam.php",
success: function(result){
console.log(result[0].name);
}
});
}
//console.log(result.name); ?gameJamId=1
//console.log(result[0].name); uden ?gameJamId=1

View File

@ -118,11 +118,15 @@ a:hover {
margin: 1vw 10vw 0;
}
.LoginButton {
.RightNavText {
color: rgba(255, 255, 255, .75);
}
.RightNavButton {
color: rgba(255, 255, 255, .55);
}
.LoginButton:hover {
.RightNavButton:hover {
color: rgba(255, 255, 255, .75);
}
@ -259,7 +263,7 @@ a:hover {
.modal-body {
padding: 0 0;
margin: 2vh 2vw;
margin: 1vh 1vw;
background-color: rgb(18, 18, 18);
}
@ -470,11 +474,12 @@ a:hover {
color: rgba(255, 255, 255, .75);
}
.box input[type="submit"] {
.box #LoginBtn,
.box #RegisterBtn {
border: 0;
background: none;
display: block;
margin: 20px auto;
margin: 20px auto 0 auto;
text-align: center;
border: 2px solid rgb(46, 204, 113);
padding: 14px 40px;
@ -485,7 +490,22 @@ a:hover {
cursor: pointer;
}
.box input[type="submit"]:hover {
.box #AdminLoginBtn {
border: 0;
background: transparent;
color: rgba(255, 255, 255, .55);
display: block;
margin: 0 auto;
text-align: center;
padding: 14px 10px;
outline: none;
border-radius: 24px;
transition: 0.25s;
cursor: pointer;
}
.box #LoginBtn:hover,
.box #RegisterBtn:hover {
background: rgb(46, 204, 113);
color: rgba(255, 255, 255, .75);
}

52
composer.lock generated
View File

@ -103,16 +103,16 @@
},
{
"name": "illuminate/collections",
"version": "v8.32.1",
"version": "v8.34.0",
"source": {
"type": "git",
"url": "https://github.com/illuminate/collections.git",
"reference": "d7cc717a00064b40fa63a8ad522042005e1de1ed"
"reference": "e18d6e4cf03dd597bc3ecd86fefc2023d0c7a5e8"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/illuminate/collections/zipball/d7cc717a00064b40fa63a8ad522042005e1de1ed",
"reference": "d7cc717a00064b40fa63a8ad522042005e1de1ed",
"url": "https://api.github.com/repos/illuminate/collections/zipball/e18d6e4cf03dd597bc3ecd86fefc2023d0c7a5e8",
"reference": "e18d6e4cf03dd597bc3ecd86fefc2023d0c7a5e8",
"shasum": ""
},
"require": {
@ -153,20 +153,20 @@
"issues": "https://github.com/laravel/framework/issues",
"source": "https://github.com/laravel/framework"
},
"time": "2021-03-08T17:22:22+00:00"
"time": "2021-03-19T00:05:33+00:00"
},
{
"name": "illuminate/container",
"version": "v8.32.1",
"version": "v8.34.0",
"source": {
"type": "git",
"url": "https://github.com/illuminate/container.git",
"reference": "3d6ce613f455093fdf8bd3c81b30104aef0b11e0"
"reference": "0e38ee1632d470e56aece0079e6e22d13e6bea8e"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/illuminate/container/zipball/3d6ce613f455093fdf8bd3c81b30104aef0b11e0",
"reference": "3d6ce613f455093fdf8bd3c81b30104aef0b11e0",
"url": "https://api.github.com/repos/illuminate/container/zipball/0e38ee1632d470e56aece0079e6e22d13e6bea8e",
"reference": "0e38ee1632d470e56aece0079e6e22d13e6bea8e",
"shasum": ""
},
"require": {
@ -204,20 +204,20 @@
"issues": "https://github.com/laravel/framework/issues",
"source": "https://github.com/laravel/framework"
},
"time": "2021-02-12T21:15:27+00:00"
"time": "2021-03-16T19:42:20+00:00"
},
{
"name": "illuminate/contracts",
"version": "v8.32.1",
"version": "v8.34.0",
"source": {
"type": "git",
"url": "https://github.com/illuminate/contracts.git",
"reference": "9c7a9868d7485a82663d67109429094c8e4ed56d"
"reference": "121cea1d8b8772bc7fee99c71ecf0f57c1d77b3b"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/illuminate/contracts/zipball/9c7a9868d7485a82663d67109429094c8e4ed56d",
"reference": "9c7a9868d7485a82663d67109429094c8e4ed56d",
"url": "https://api.github.com/repos/illuminate/contracts/zipball/121cea1d8b8772bc7fee99c71ecf0f57c1d77b3b",
"reference": "121cea1d8b8772bc7fee99c71ecf0f57c1d77b3b",
"shasum": ""
},
"require": {
@ -252,20 +252,20 @@
"issues": "https://github.com/laravel/framework/issues",
"source": "https://github.com/laravel/framework"
},
"time": "2021-02-26T13:17:03+00:00"
"time": "2021-03-12T14:45:30+00:00"
},
{
"name": "illuminate/database",
"version": "v8.32.1",
"version": "v8.34.0",
"source": {
"type": "git",
"url": "https://github.com/illuminate/database.git",
"reference": "f6a10cebd9bbd188ca66993168fb453439dbb50f"
"reference": "74a165fd07b36cc0ea3558fa391b762867af87e8"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/illuminate/database/zipball/f6a10cebd9bbd188ca66993168fb453439dbb50f",
"reference": "f6a10cebd9bbd188ca66993168fb453439dbb50f",
"url": "https://api.github.com/repos/illuminate/database/zipball/74a165fd07b36cc0ea3558fa391b762867af87e8",
"reference": "74a165fd07b36cc0ea3558fa391b762867af87e8",
"shasum": ""
},
"require": {
@ -320,11 +320,11 @@
"issues": "https://github.com/laravel/framework/issues",
"source": "https://github.com/laravel/framework"
},
"time": "2021-03-09T14:06:15+00:00"
"time": "2021-03-23T15:12:51+00:00"
},
{
"name": "illuminate/macroable",
"version": "v8.32.1",
"version": "v8.34.0",
"source": {
"type": "git",
"url": "https://github.com/illuminate/macroable.git",
@ -370,16 +370,16 @@
},
{
"name": "illuminate/support",
"version": "v8.32.1",
"version": "v8.34.0",
"source": {
"type": "git",
"url": "https://github.com/illuminate/support.git",
"reference": "2ef7ff288366a1ebe32f633196a1b90bd443acc3"
"reference": "b7b27e758b68aad44558c62e7374328835895386"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/illuminate/support/zipball/2ef7ff288366a1ebe32f633196a1b90bd443acc3",
"reference": "2ef7ff288366a1ebe32f633196a1b90bd443acc3",
"url": "https://api.github.com/repos/illuminate/support/zipball/b7b27e758b68aad44558c62e7374328835895386",
"reference": "b7b27e758b68aad44558c62e7374328835895386",
"shasum": ""
},
"require": {
@ -434,7 +434,7 @@
"issues": "https://github.com/laravel/framework/issues",
"source": "https://github.com/laravel/framework"
},
"time": "2021-03-05T15:22:14+00:00"
"time": "2021-03-21T13:37:37+00:00"
},
{
"name": "nesbot/carbon",

0
config/database.json Normal file
View File