-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.js
88 lines (81 loc) · 2.6 KB
/
app.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
const LoginPage = "../LoginPage/LoginPage.html";
const RegisterPage = "./RegisterPage/RegisterPage.html";
const forumPage = "./Forum-AuthUsers/Forum-AuthPage.html"
const BASE_URL = "https://strangers-things.herokuapp.com/api/2101-LSU-RM-WEB-PT";
const rememberMeFunc = () => {
let item = localStorage.getItem('rememberMeBox')
if(item) {
let checkBoxStorage = JSON.parse(item)
if(checkBoxStorage.isChecked) {
$('.username').val(`${checkBoxStorage.username}`)
document.getElementById("remember-box").checked = true;
}
}
}; rememberMeFunc();
$('#remember-box').on('click', () => {
const checkBox = {
isChecked: $('#remember-box').is(":checked"),
username: $('.username').val()
};
if(checkBox.isChecked && $('#remember-box').is(":checked")) {
localStorage.setItem('rememberMeBox', JSON.stringify(checkBox));
} else localStorage.removeItem('rememberMeBox');
});
$('#userInfo').on('submit', async function registerUsers (event) {
console.log("i'm hyperJS")
event.preventDefault();
const user = {
username: $('.username').val(),
password: $('.password').val()
}
onFetchStart();
try {
const response = await fetch(`${BASE_URL}/users/login`, {
method: "POST",
headers: {'Content-Type':'application/json'},
body: JSON.stringify({user})
})
const data = await response.json();
console.log(data);
$(".loginErrorMsg").empty()
if (data.error) {
$(".loginNetworkMsg").text(`${data.error.message}`)
} else {
$(".loginNetworkMsg").text(`${data.data.message}`)
.css("color", "lightgreen")
window.location.href = forumPage
console.log(data.data.token)
localStorage.setItem("authToken", data.data.token);
}
console.log(window.location.href)
} catch (error) {
console.error(error);
} finally {
onFetchEnd();
}
function onFetchStart() {
$('#loading').addClass('active');
}
function onFetchEnd() {
$('#loading').removeClass('active');
}
});
(async () => {
if(localStorage.getItem('authToken')){
try {
const response = await fetch(`${BASE_URL}/test/me`, {
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${localStorage.getItem('authToken')}`
}
})
const data = await response.json();
console.log(data)
if(data.success) {
window.location.href = forumPage
}
} catch (error) {
console.log(error)
}
}
})();