-
Notifications
You must be signed in to change notification settings - Fork 33
/
register.html
132 lines (119 loc) · 3.24 KB
/
register.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>Serverless Todo with Vue and AWS Lambda - Register</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>register</h1>
<input
id="username"
class="new-todo"
autofocus
placeholder="username"
required
>
<input
id="password"
class="new-todo"
autofocus
placeholder="password"
type="password"
required
>
<input
id="fullname"
class="new-todo"
autofocus
placeholder="Full name"
required
>
<input
id="email"
class="new-todo"
autofocus
placeholder="email"
type="email"
required
>
</header>
</section>
<!-- BEGIN - Remove this line
<section class="main">
<p><a href="login.html">Login</a></p>
</section>
END - Remove this line -->
</div>
<script>
let apiBaseUrl = '{{API_BASE_URL}}';
// AWS Cloud9 development
if (apiBaseUrl.indexOf('API_BASE_URL') !== -1) {
apiBaseUrl = window.location.protocol + '//' + window.location.hostname;
}
if (window.localStorage.getItem('logged_in') === 'true') {
window.location = 'index.html';
}
function $(el) {
return document.querySelector(el);
}
async function register(params) {
const loginUrl = apiBaseUrl + '/register';
const userData = {
username: params.username.trim(),
password: params.password.trim(),
fullname: params.fullname.trim(),
email: params.email.trim()
};
const response = await window.fetch(loginUrl, {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify(userData)
});
if ([200, 201].indexOf(response.status) === -1) {
throw (await response.json()).message;
}
return response.json();
}
$('#username').addEventListener('keyup', (e) => {
if (e.key === 'Enter') {
$('#password').focus();
$('#password').select();
}
});
$('#password').addEventListener('keyup', (e) => {
if (e.key === 'Enter') {
$('#fullname').focus();
$('#fullname').select();
}
});
$('#fullname').addEventListener('keyup', (e) => {
if (e.key === 'Enter') {
$('#email').focus();
$('#email').select();
}
});
$('#email').addEventListener('keyup', async (e) => {
if (e.key === 'Enter') {
try {
const response = await register({
username: $('#username').value,
password: $('#password').value,
fullname: $('#fullname').value,
email: $('#email').value
});
console.log(response);
window.location = 'login.html';
} catch (err) {
alert(err);
}
}
});
</script>
</body>
</html>