77 lines
2.2 KiB
JavaScript
77 lines
2.2 KiB
JavaScript
|
|
|
|
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 = '../index.html'
|
|
}
|
|
})
|
|
|
|
}
|
|
|
|
function login() {
|
|
localStorage.removeItem('token')
|
|
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!"))
|
|
checkRole();
|
|
})
|
|
.catch(error => console.log(error))
|
|
}
|
|
|
|
|
|
// 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)
|
|
// }
|