-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
93 lines (89 loc) · 3.53 KB
/
index.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
89
90
91
92
93
const attendance = document.querySelector("#attendance");
const profile = document.querySelector("#profile");
const logout = document.querySelector("#logout");
attendance.addEventListener("click", attendanceAuth);
profile.addEventListener("click", profileAuth);
if(logout) logout.addEventListener("click", logOut);
function attendanceAuth() {
fetch("/user/attendance", {
headers: { 'authorization': localStorage.getItem('access_token') }
})
.then((res) => res.json())
.then((res) => {
if(res.code === 200) {
const attendanceDays = res.data;
localStorage.setItem('attendanceDays', JSON.stringify(attendanceDays));
window.location.href = "/user/attendance/output";
} else if (res.code === 419) {
fetch("/user/token/refresh", {
headers: { 'authorization': localStorage.getItem('refresh_token') }
})
.then((res) => res.json())
.then((res) => {
if(res.code === 419) {
alert(res.message);
localStorage.removeItem('access_token');
localStorage.removeItem('refresh_token');
window.location.href = "/user/login";
} else {
alert(res.message);
localStorage.setItem('access_token', res.access_token);
window.location.reload();
}
})
} else { // 401 or 500
alert(res.message);
location.href = "/user/login";
}
})
.catch((err) => {
alert('An error occurred while processing your request. Please try again later.');
});
}
function profileAuth() {
fetch("/user/profile", {
headers: { 'authorization': localStorage.getItem('access_token') }
})
.then((res) => res.json())
.then((res) => {
if(res.code === 200) {
localStorage.setItem('profileData', JSON.stringify({ email: res.data.email, profile: res.data.profile, user_name: res.data.user_name }));
location.href = "/user/profile/output";
} else if (res.code === 419) {
fetch("/user/token/refresh", {
headers: { 'authorization': localStorage.getItem('refresh_token') }
})
.then((res) => res.json())
.then((res) => {
if(res.code === 419) {
alert(res.message);
localStorage.removeItem('access_token');
localStorage.removeItem('refresh_token');
window.location.href = "/user/login";
} else {
alert(res.message);
localStorage.setItem('access_token', res.access_token);
window.location.reload();
}
})
} else { // 401 or 500
alert(res.message);
localStorage.removeItem('access_token');
localStorage.removeItem('refresh_token');
location.href = "/user/login";
}
})
.catch((err) => {
alert('An error occurred while processing your request. Please try again later.');
});
}
function logOut() {
if(!localStorage.getItem('access_token')) {
alert("Already logout state.")
location.href = "/user/login";
} else {
localStorage.removeItem('access_token');
localStorage.removeItem('refresh_token');
location.href = "/";
}
}