Padel-Tournaments-System/frontend/panel_organizatora/logowanie/app.js
Arek d3961bee19 feat: Connected with api
workign login, registration, tournaments get
2022-06-10 00:56:06 +02:00

66 lines
2.1 KiB
JavaScript

function login() {
fetch('https://dragonmaster.pl/inz/user/login', {
method: "POST",
headers: {
'Content-type': 'application/json'
},
body: JSON.stringify(
{
"username": document.getElementById("loginusername").value,
"password": document.getElementById("loginpassword").value
}
)
}
)
.then(res => {
if (res.ok) {
// alert("Teraz mozesz się zalogować!")
}
else { console.log("Coś poszło nie tak!") }
return res
})
.then(res => res.json())
.then(data => console.log(data['token']?.length > 0 ? localStorage.setItem("token", data['token']) : alert("Błędne dane!")))
.catch(error => console.log(error))
checkRole();
}
// function checkRole() {
// fetch('https://dragonmaster.pl/inz/user', {
// method: "GET",
// headers: {
// 'Content-type': 'application/json'
// },
// body: JSON.stringify(
// {
// "token": document.getElementById("loginusername").value,
// "password": document.getElementById("loginpassword").value
// }
// )
// }
// )
// .then(res => {
// if (res.ok) {
// alert("Teraz mozesz się zalogować!")
// }
// else { console.log("Coś poszło nie tak!") }
// return res
// })
// .then(res => res.json())
// .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 '))
}