error mesasge on login pops up
This commit is contained in:
parent
22c397aebb
commit
a4ebdbc96c
@ -26,7 +26,7 @@
|
|||||||
</span>
|
</span>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div id="form-error" class="error"></div>
|
<div id="login-error" class="error"></div>
|
||||||
</div>
|
</div>
|
||||||
</form>
|
</form>
|
||||||
</body>
|
</body>
|
||||||
|
@ -3,26 +3,23 @@ import { login } from "./services/users.service.js";
|
|||||||
document.getElementById("loginForm").addEventListener("submit", function(event) {
|
document.getElementById("loginForm").addEventListener("submit", function(event) {
|
||||||
event.preventDefault();
|
event.preventDefault();
|
||||||
|
|
||||||
document.getElementById("form-error").style.display = "none";
|
|
||||||
|
|
||||||
const emailOrUsername = document.getElementById("emailorusn").value;
|
const emailOrUsername = document.getElementById("emailorusn").value;
|
||||||
const password = document.getElementById("psw").value;
|
const password = document.getElementById("psw").value;
|
||||||
|
|
||||||
login(emailOrUsername, password)
|
login(emailOrUsername, password).then((response) => {
|
||||||
.then(response => {
|
if(response.token && response.refreshToken){
|
||||||
if(response.token && response.refreshToken){
|
document.cookie = `auth-token=${response.token}; Path=/`;
|
||||||
document.cookie = `auth-token=${response.token}; Path=/`;
|
document.cookie = `refresh-token=${response.refreshToken}; Path=/`;
|
||||||
document.cookie = `refresh-token=${response.refreshToken}; Path=/`;
|
localStorage.setItem("user", JSON.stringify({
|
||||||
localStorage.setItem("user", JSON.stringify({
|
id: response.id,
|
||||||
id: response.id,
|
username: response.userName,
|
||||||
username: response.userName,
|
}));
|
||||||
}));
|
location.href = "/home";
|
||||||
location.href = "/home";
|
}
|
||||||
}
|
})
|
||||||
})
|
.catch(error => {
|
||||||
.catch(error => {
|
document.getElementById("login-error").innerText = error;
|
||||||
document.getElementById("form-error").innerText = error;
|
document.getElementById("login-error").style.display = "block";
|
||||||
document.getElementById("form-error").style.display = "block";
|
});
|
||||||
});
|
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -6,19 +6,11 @@ export function get() {
|
|||||||
return request("GET",`/user/get`)
|
return request("GET",`/user/get`)
|
||||||
}
|
}
|
||||||
|
|
||||||
export function login(usernameOrEmail, password) {
|
|
||||||
return fetch(`${address}/user/login`, {
|
export function login(emailOrUsrn, password){
|
||||||
method: "POST",
|
return request("POST", "/user/login", {
|
||||||
headers: {
|
emailOrUsrn,
|
||||||
"Content-Type": "application/json"
|
password,
|
||||||
},
|
|
||||||
body: JSON.stringify({ password: password, EmailOrUsrn: usernameOrEmail })
|
|
||||||
})
|
|
||||||
.then(response => {
|
|
||||||
if (!response.ok) {
|
|
||||||
return("Request failed with HTTP code " + response.status);
|
|
||||||
}
|
|
||||||
return response.json();
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -22,7 +22,7 @@ export async function request(method, path, body = null) {
|
|||||||
})
|
})
|
||||||
.then(async response => {
|
.then(async response => {
|
||||||
if (response.status === 401) {
|
if (response.status === 401) {
|
||||||
await getTokenByReferenceId(method, path, body);
|
getTokenByReferenceId(method, path, body);
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
@ -45,11 +45,10 @@ export async function request(method, path, body = null) {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function getTokenByReferenceId(method, path, body = null) {
|
export function getTokenByReferenceId(method, path, body = null) {
|
||||||
console.log("gtbri");
|
|
||||||
var token = document.cookie.match(/\brefresh-token=([^;\s]+)/);
|
var token = document.cookie.match(/\brefresh-token=([^;\s]+)/);
|
||||||
if (token != null) {
|
if (token != null) {
|
||||||
return await fetch(`${address}/user/refreshtoken/${token[1]}`, {
|
return fetch(`${address}/user/refreshtoken/${token[1]}`, {
|
||||||
method: "POST",
|
method: "POST",
|
||||||
headers: {
|
headers: {
|
||||||
"Content-Type": "application/json"
|
"Content-Type": "application/json"
|
||||||
@ -60,7 +59,6 @@ export async function getTokenByReferenceId(method, path, body = null) {
|
|||||||
|
|
||||||
document.cookie = `auth-token=${json.token}; Path=/`;
|
document.cookie = `auth-token=${json.token}; Path=/`;
|
||||||
document.cookie = `refresh-token=${json.refreshToken}; Path=/`;
|
document.cookie = `refresh-token=${json.refreshToken}; Path=/`;
|
||||||
console.log(json.token);
|
|
||||||
|
|
||||||
return request(method, path, body);
|
return request(method, path, body);
|
||||||
})
|
})
|
||||||
@ -68,7 +66,6 @@ export async function getTokenByReferenceId(method, path, body = null) {
|
|||||||
location.href = "/login";
|
location.href = "/login";
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export function logout() {
|
export function logout() {
|
||||||
|
Loading…
Reference in New Issue
Block a user