-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
72 lines (66 loc) · 2.56 KB
/
index.html
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Spring Security Test</title>
</head>
<body>
<div>
<button onclick="login('bob')">Login Bob</button>
<button onclick="login('admin')">Login Admin</button>
<button onclick="login('sponsor')">Login Sponsor</button>
<button onclick="logout()">Logout</button>
<span>Logged In:</span><span style="color: green; margin-left: 8px" id="user"></span>
</div>
<div>
<button onclick="call('coffee/public')">Coffee Public</button>
<button onclick="call('coffee/user')">Coffee User</button>
<button onclick="call('coffee/admin')">Coffee Admin</button>
<button onclick="call('coffee/sponsor')">Coffee Sponsor</button>
</div>
<pre id="result"></pre>
<script>
function logout() {
call("user/logout", "POST").then(() => setUser(null));
}
function setUser(user) {
document.getElementById('user').textContent = user?.username
}
function login(username) {
call("user/login", "POST", {username, password: "12345678", rememberMe: true}).then(data => {
setUser(data);
});
}
function showResult(content) {
document.getElementById('result').textContent = content;
}
function call(url, method = "GET", data = null) {
const body = data ? JSON.stringify(data) : null;
return fetch("http://localhost:8080/" + url,
{ method, headers: {"Content-Type": "application/json"}, body, mode: "cors", credentials: "include"})
.then(response => {
if (!response.ok) {
return response.json().then(error => {
throw new Error(`${response.status}: ${error.message}`);
});
}
if (response.headers.get('content-length') === '0') {
return null;
}
return response.json();
})
.then(data => {
showResult(data ? JSON.stringify(data, null, 2) : null);
return data;
})
.catch(error => {
showResult('Error: ' + error?.message);
});
}
window.onload = () => {
call("user/checklogin").then(data => setUser(data));
}
</script>
</body>
</html>