feat: logout button

now you can easy logout
This commit is contained in:
Arek 2022-06-10 12:23:44 +02:00
parent d3961bee19
commit cf2b59b751
5 changed files with 110 additions and 29 deletions

View File

@ -11,15 +11,16 @@ async function showTournaments() {
async function renderTournaments() {
let tournaments = await showTournaments();
let html = '';
console.log(tournaments)
tournaments.forEach(tournament => {
let htmlSegment = `<div class = "tournamentBox">
let htmlSegment = `<div class = "tournamentBox" onclick="window.location.href='_main page/pojedynczy_turniej/index.html';loca">
<div class="tournamentIconBox">
<img class="tournamentIcon" src="_main%20page/cup.svg" alt="">
</div>
<div class="tournamentDataBox">
<div class="tournamentData">
<h4>${tournament.name}</h4>
Propadel, Warszawa
${tournament.place}
${tournament.from} do ${tournament.to}
<button class="tournamentOpen">OPEN</button>
</div>
@ -32,3 +33,30 @@ async function renderTournaments() {
let container = document.querySelector('.tournament-list');
container.innerHTML = html;
}
function logout() {
localStorage.clear();
location.reload();
}
function logoutVisibility() {
if (localStorage.getItem("token") !== null) {
document.getElementById("logout").style.display = "";
}
else {
document.getElementById("logout").style.display = "none";
}
}
function loginVisibility() {
if (localStorage.getItem("token") !== null) {
document.getElementById("login").style.display = "none";
}
else {
document.getElementById("login").style.display = "";
}
}

View File

@ -10,7 +10,7 @@
<script type="text/javascript" src="app.js"></script>
</head>
<body onload="renderTournaments()">
<body onload="renderTournaments();logoutVisibility(); loginVisibility()">
<header>
<div class="logoBox">
<a class="header_link" href="./index.html"><img class="logo" src="./_global/logo.svg" alt="Logo error"></a>
@ -18,10 +18,11 @@
<div class="linkBox">
<!-- <span class="dot">JM</span>-->
<button class="loginButton" type="submit"
onclick="window.location.href='panel_organizatora/logowanie/index.html'">LOGOWANIE</button>
onclick="window.location.href='panel_organizatora/logowanie/index.html'" id="login">LOGOWANIE</button>
<a class="header_link" href="./kontakt/index.html">Kontakt</a>
<a class="header_link" href="./ranking/index.html">Ranking</a>
<a class="header_link" href="./index.html">Kalendarz turniejów</a>
<button class="loginButton" id="logout" onclick="logout()">Wyloguj</button>
</div>
</header>

View File

@ -38,14 +38,24 @@ function createTournament() {
body: JSON.stringify(
{
"name": document.getElementById("name").value,
"place": document.getElementById("place").value,
"ranked": document.getElementById("rank").value,
"typeOfLadder": "string",
"pointsForTournament": "1",
"places": "1",
"roles": document.getElementById("category").value,
"phone": document.getElementById("phone").value,
"director": document.getElementById("director").value,
"phone": document.getElementById("regphone").value,
"ranked": document.getElementById("rank").value,
"place": document.getElementById("place").value,
"from": "string",
"to": "string",
"rang": "rang1",
"entryFee": document.getElementById("entry-fee").value,
"additionalInformations": document.getElementById("add-info").value
"director": document.getElementById("director").value,
"phone": document.getElementById("phone").value,
"entriesTo": "string",
"additionalInformations": document.getElementById("add-info").value,
"categotry": "cat1",
"visibility": "TRUE"
}
)
}
@ -55,3 +65,30 @@ function createTournament() {
.then(data => console.log(data))
.catch(error => console.log(error))
}
function logout() {
localStorage.clear();
location.reload();
}
function logoutVisibility() {
if (localStorage.getItem("token") !== null) {
document.getElementById("logout").style.display = "";
}
else {
document.getElementById("logout").style.display = "none";
}
}
function loginVisibility() {
if (localStorage.getItem("token") !== null) {
document.getElementById("login").style.display = "none";
}
else {
document.getElementById("login").style.display = "";
}
}

View File

@ -9,21 +9,25 @@
<link href="style.css" rel="stylesheet">
</head>
<body>
<body onload="logoutVisibility(); loginVisibility()">
<script type="text/javascript" src="app.js"></script>
<header>
<div class="logoBox">
<a class="header_link" href="../../index.html"><img class="logo" src="../../_global/logo.svg" alt="Logo error"></a>
<a class="header_link" href="../../index.html"><img class="logo" src="../../_global/logo.svg"
alt="Logo error"></a>
</div>
<div class="linkBox">
<!-- <span class="dot">JM</span>-->
<button class="loginButton" type="submit" onclick="window.location.href='../logowanie/index.html'">LOGOWANIE</button>
<button class="loginButton" type="submit" onclick="window.location.href='../logowanie/index.html'"
id="login">LOGOWANIE</button>
<a class="header_link" href="../../kontakt/index.html">Kontakt</a>
<a class="header_link" href="../../ranking/index.html">Ranking</a>
<a class="header_link" href="../../index.html">Kalendarz turniejów</a>
<button id="logout" onclick="logout()">Wyloguj</button>
</div>
</header>
<!-- TODO sztuczny padding-->
<!-- TODO sztuczny padding-->
<div> .</div>
<div> .</div>

View File

@ -1,4 +1,25 @@
function checkRole() {
fetch('https://dragonmaster.pl/inz/user', {
headers: {
Authorization: ("Bearer " + localStorage.getItem("token"))
}
})
.then(res => res.json())
.then(json => {
if (json['role'] == '1') {
window.location.href = '../../index.html'
}
else {
window.location.href = '../dodawanie turnieju/index.html'
}
})
}
function login() {
localStorage.clear()
fetch('https://dragonmaster.pl/inz/user/login', {
method: "POST",
headers: {
@ -20,9 +41,11 @@ function login() {
return res
})
.then(res => res.json())
.then(data => console.log(data['token']?.length > 0 ? localStorage.setItem("token", data['token']) : alert("Błędne dane!")))
.then(data => {
console.log(data['token']?.length > 0 ? localStorage.setItem("token", data['token']) : alert("Błędne dane!"))
checkRole();
})
.catch(error => console.log(error))
checkRole();
}
@ -51,16 +74,4 @@ function login() {
// .then(data => console.log(data['token']?.length > 0 ? localStorage.setItem("token", data['token']) : 'niedziaua'))
// .catch(error => console.log(error))
// .then(token1 => token = token1)
// }
function checkRole() {
fetch('https://dragonmaster.pl/inz/user', {
headers: {
Authorization: ("Bearer " + localStorage.getItem("token"))
}
})
.then(res => res.json())
.then(json => console.log(json['role']?.length == 1 ? window.location.href = '../index.html' : window.location.href = '../dodawanie-turnieju/index.html '))
}
// }