This repository has been archived by the owner on Apr 30, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
login.html
262 lines (237 loc) · 11.4 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
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
<!DOCTYPE html>
<html lang="en">
<head>
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no" charset="utf-8">
<title>OptiGrader - Sign In</title>
<script src="https://code.jquery.com/jquery-3.3.1.min.js" integrity="sha384-tsQFqpEReu7ZLhBV2VZlAu7zcOV+rXbYlF2cqB8txI/8aZajjp4Bqd+V6D5IgvKT" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/umd/popper.min.js" integrity="sha384-ZMP7rVo3mIykV+2+9J3UJ46jBk0WLaUAdn689aCwoqbBJiSnjAK/l8WvCWPIPm49" crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js" integrity="sha384-ChfqqxuZUCnJSK3+MXmPNIyE6ZbWh2IMqE241rYiqJxyMiZ6OW/JmZQ5stwEULTy" crossorigin="anonymous"></script>
<script src="https://bitwiseshiftleft.github.io/sjcl/sjcl.js" integrity="sha384-zJXJqc5ftX51ojskUi8aWFg0yrz9P61p6Wyg8n0cyFYsgWiAYtxnVWzh8d8BLcw0" crossorigin="anonymous"></script>
<!-- Bootstrap CSS/JS -->
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO" crossorigin="anonymous">
<!-- Custom stylesheet for index -->
<link href="css/custom.css" rel="stylesheet">
<script type="application/javascript">
var BASE_URL = "https://" + window.location.hostname + ":8080/";
var token = document.cookie.replace(/(?:(?:^|.*;\s*)token\s*\=\s*([^;]*).*$)|^.*$/, "$1");
// Automatic login
if (token !== "")
{
// Check for valid session.
$.ajax(
{
type: "POST",
url: BASE_URL + "login",
data: JSON.stringify(
{
"token": token,
"action": "is_session_valid"
}),
dataType: "json",
success: function (data, textStatus, jqXHR)
{
if (typeof data.success !== "undefined")
{
if (data.success === "Session invalid")
{
document.cookie = "token=; Max-Age=0;";
$("#invalidSessionAlert").show();
}
else
{
window.location.href = "dashboard.html";
}
}
},
error: function (jqXHR, textStatus, errorThrown)
{
if (typeof jqXHR.responseJSON !== "undefined" && typeof jqXHR.responseJSON.error !== "undefined")
{
console.log("SERVER ERROR: " + jqXHR.responseJSON.error);
document.getElementById("serverErrorAlert").innerHTML = jqXHR.responseJSON.error;
$("#serverErrorAlert").show();
}
else
{
$("#connectionFailedAlert").show();
}
}
});
}
$(document).ready(function ()
{
var token = document.cookie.replace(/(?:(?:^|.*;\s*)token\s*\=\s*([^;]*).*$)|^.*$/, "$1");
// Automatic login
if (token !== "")
{
// Check for valid session.
$.ajax(
{
type: "POST",
url: BASE_URL + "login",
data: JSON.stringify(
{
"token": token,
"action": "is_session_valid"
}),
dataType: "json",
success: function (data, textStatus, jqXHR)
{
if (typeof data.success !== "undefined")
{
if (data.success === "Session invalid")
{
document.cookie = "token=; Max-Age=0;";
$("#invalidSessionAlert").show();
}
else
{
window.location.href = "dashboard.html";
}
}
},
error: function (jqXHR, textStatus, errorThrown)
{
if (typeof jqXHR.responseJSON !== "undefined" && typeof jqXHR.responseJSON.error !== "undefined")
{
console.log("SERVER ERROR: " + jqXHR.responseJSON.error);
document.getElementById("serverErrorAlert").innerHTML = jqXHR.responseJSON.error;
$("#serverErrorAlert").show();
}
else
{
$("#connectionFailedAlert").show();
}
}
});
}
var url_vars = window.location.search.substring(1);
var key_value = url_vars.split("&")[0].split("=");
// Signed out alert. (Redirect from dashboard)
if (key_value[0] === "logout")
{
if (key_value[1] === "1")
{
$("#signedOutAlert").show();
}
else if (key_value[1] === "0")
{
$("#invalidSessionAlert").show();
}
}
$("#login_form").submit(function (e)
{
e.preventDefault();
// Hash password with SHA-256
var hash = sjcl.codec.hex.fromBits(sjcl.hash.sha256.hash($("#inputPassword").val()));
// Authenticate user
$.ajax(
{
type: "POST",
url: BASE_URL + "login",
data: JSON.stringify(
{
"login": $("#inputEmail").val(),
"password": hash
}),
dataType: "json",
success: function (data, textStatus, jqXHR)
{
$("#invalidSessionAlert").hide();
$("#loginInvalidAlert").hide();
$("#signedOutAlert").hide();
$("#connectionFailedAlert").hide();
if (typeof data.token !== "undefined")
{
document.cookie = "token=" + data.token;
window.location.href = "dashboard.html";
}
else
{
$("#inputPassword").val("");
}
},
error: function (jqXHR, textStatus, errorThrown)
{
$("#invalidSessionAlert").hide();
$("#signedOutAlert").hide();
if (typeof jqXHR.responseJSON !== "undefined" && typeof jqXHR.responseJSON.error !== "undefined")
{
console.log("SERVER ERROR: " + jqXHR.responseJSON.error);
$("#connectionFailedAlert").hide();
$("#loginInvalidAlert").html(jqXHR.responseJSON.error)
$("#loginInvalidAlert").show();
}
else
{
$("#loginInvalidAlert").hide();
$("#connectionFailedAlert").show();
}
$("#inputPassword").val("");
}
});
});
});
</script>
</head>
<body>
<div class="cover-container d-flex w-100 h-100 p-3 mx-auto flex-column">
<header class="masthead mb-auto">
<div class="inner">
<h3 class="masthead-brand">OptiGrader</h3>
<nav class="nav nav-masthead justify-content-center">
<a class="nav-link" href="index.html">Home</a>
<a class="nav-link active" href="login.html">Sign In</a>
<a class="nav-link " href="register.html">Register</a>
</nav>
</div>
</header>
<!-- Connection Failed Error/Alert -->
<div class="alert alert-danger collapse" role="alert" id="connectionFailedAlert" style="z-index: 1">
Unable to connect to server!
</div>
<!-- Logged Out Alert -->
<div class="alert alert-success collapse" role="alert" id="signedOutAlert" style="z-index: 1">
You've successfully signed out!
</div>
<!-- Invalid Login Alert -->
<div class="alert alert-danger collapse" role="alert" id="loginInvalidAlert" style="z-index: 1">
Invalid login credentials!
</div>
<!-- Invalid Session Alert -->
<div class="alert alert-warning collapse" role="alert" id="invalidSessionAlert" style="z-index: 1">
Invalid session: You have signed in somewhere else
</div>
<!-- Login Form -->
<div class="container-fluid">
<form id="login_form">
<div class="row">
<div class="container card p-3 bg-light">
<span class="mx-auto"><h4>Please Sign In</h4></span>
<hr>
<div class="form-group">
<label for="inputEmail">Email</label>
<input type="text" class="form-control" id="inputEmail" aria-describedby="emailHelp" placeholder="name@email.com" required pattern="[^@\s]+@[^@\s]+\.[^@\s]+">
</div>
<div class="form-group">
<label for="inputPassword">Password</label>
<input type="password" class="form-control" id="inputPassword" required pattern="(?=.*\d)(?=.*[a-z])(?=.*[A-Z]).{8,}">
</div>
<button type="submit" class="btn btn-info">Sign In</button>
<hr>
<div class="row mx-auto ">
<p class="mb-0">Need to create an account?
<a class="text-black-50" href="register.html"> Register Now.</a></p>
</div>
</div>
</div>
</form>
</div>
<footer class="mastfoot mt-auto text-center">
<div class="inner">
<p><img src="images/GitHub-Mark-Light-32px.png"> Visit us on <a href="https://github.com/Bonfire/OptiGrader" target="_blank">GitHub</a>.</p>
</div>
</footer>
</div>
</body>
</html>