-
Notifications
You must be signed in to change notification settings - Fork 33
/
login.html
95 lines (86 loc) · 2.54 KB
/
login.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>Serverless Todo with Vue and AWS Lambda - Login</title>
<link rel="stylesheet" href="https://unpkg.com/todomvc-app-css@2.4.1/index.css" type="text/css" />
</head>
<body>
<div id="app">
<section class="todoapp">
<header class="header">
<h1>login</h1>
<input
id="username"
class="new-todo"
autofocus
placeholder="username"
>
<input
id="password"
class="new-todo"
autofocus
placeholder="password"
type="password"
>
</header>
</section>
<!-- BEGIN - Remove this line
<section class="main">
<p><a href="register.html">Register</a></p>
</section>
END - Remove this line -->
</div>
<script>
let apiBaseUrl = '{{API_BASE_URL}}';
if (window.location.search.indexOf('logout') !== -1) {
window.localStorage.removeItem('auth_token');
window.localStorage.setItem('logged_in', false);
window.location = 'login.html';
}
// AWS Cloud9 development
if (apiBaseUrl.indexOf('API_BASE_URL') !== -1) {
apiBaseUrl = window.location.protocol + '//' + window.location.hostname;
}
function $(el) {
return document.querySelector(el);
}
async function login(username, password) {
const loginUrl = apiBaseUrl + '/login';
const credentials = {
username: username.trim(),
password: password.trim()
};
const response = await window.fetch(loginUrl, {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify(credentials)
});
if (response.status !== 200) {
throw response.body.toString();
}
return response.json();
}
$('#username').addEventListener('keyup', (e) => {
if (e.key === 'Enter') {
$('#password').focus();
$('#password').select();
}
});
$('#password').addEventListener('keyup', async (e) => {
if (e.key === 'Enter') {
try {
const response = await login($('#username').value, $('#password').value);
window.localStorage.setItem('logged_in', true);
window.localStorage.setItem('auth_token', response.token);
window.location = 'index.html';
} catch (err) {
alert('Invalid username or password');
}
}
});
</script>
</body>
</html>