forked from VahidN/DNTCaptcha.Core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.purejs.html
176 lines (160 loc) · 6.05 KB
/
index.purejs.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
<!DOCTYPE html>
<html lang="en">
<head>
<title>DNTCaptcha.Core</title>
<meta charset="utf-8"/>
<link href="/lib/bootstrap/dist/css/bootstrap.min.css" rel="stylesheet"/>
<link href="/lib/bootstrap-icons/font/bootstrap-icons.css" rel="stylesheet"/>
<link href="/content/site.css" rel="stylesheet"/>
</head>
<body>
<div class="container">
<h3>Login Form</h3>
<form action="" id="loginForm" method="post" novalidate>
<div class="mb-3">
<label class="form-label">User Name</label>
<input class="form-control" name="username" type="text"/>
</div>
<div class="mb-3">
<label>Password</label>
<input class="form-control" name="password" type="password"/>
</div>
<div class="dntCaptcha mb-3">
<img
alt="captcha"
id="dntCaptchaImg"
name="dntCaptchaImg"
style="margin-bottom: 4px"
/>
<a
class="bi-arrow-counterclockwise btn-lg"
id="dntCaptchaRefreshButton"
name="dntCaptchaRefreshButton"
onclick="doRefreshCaptcha()"
style="cursor: pointer"
></a>
<input id="DNTCaptchaText" name="DNTCaptchaText" type="hidden"/>
<div class="input-group">
<span class="input-group-text">
<span class="bi-lock"></span>
</span>
<input
autocomplete="off"
class="form-control"
dir="ltr"
id="DNTCaptchaInputText"
name="DNTCaptchaInputText"
placeholder="Security code as a number"
type="text"
/>
</div>
<input id="DNTCaptchaToken" name="DNTCaptchaToken" type="hidden"/>
</div>
<button class="btn btn-secondary" id="submitLogin" type="submit">
Submit
</button>
<div
class="alert alert-warning"
id="validationErrorMessage"
style="display: none"
></div>
</form>
</div>
<script type="text/javascript">
const captchaApiUrl =
"https://localhost:5001/api/account/CreateDNTCaptchaParams";
const loginUrl = "https://localhost:5001/api/account/login";
const loginFormId = "loginForm";
const submitButtonId = "submitLogin";
const validationErrorMessageId = "validationErrorMessage";
const validationErrorMessageElement = document.getElementById(validationErrorMessageId);
function handleErrors(response) {
if (response.ok) {
return response;
}
return Promise.reject(response);
}
function getCaptchaInfo() {
fetch(captchaApiUrl, {
method: 'GET',
headers: {}
})
.then(handleErrors)
.then(response => response.json())
.then(data => {
console.log(data);
const {
dntCaptchaImgUrl,
dntCaptchaId,
dntCaptchaTextValue,
dntCaptchaTokenValue,
} = data;
document.getElementById("dntCaptchaImg").setAttribute("src", dntCaptchaImgUrl);
document.getElementById("DNTCaptchaText").setAttribute("value", dntCaptchaTextValue);
document.getElementById("DNTCaptchaToken").setAttribute("value", dntCaptchaTokenValue);
document.querySelector("div.dntCaptcha").setAttribute("id", dntCaptchaId);
})
.catch(error => {
if (error.status === 429) {
alert('Too many requests! Please wait a minute!');
}
console.log("error", error);
});
}
function doRefreshCaptcha() {
document.getElementById("DNTCaptchaInputText").value = "";
getCaptchaInfo();
}
function errorToString(obj) {
const parts = [];
parts.push("<ul>");
for (const key in obj) {
if (obj.hasOwnProperty(key)) {
const value = obj[key];
if (typeof value === "object") {
return errorToString(value);
}
if (value !== null && value !== undefined) {
parts.push(`<li>${value}</li>`);
}
}
}
parts.push("</ul>");
return parts.join("");
}
window.onload = function () {
getCaptchaInfo();
// Post login form to the server
document.getElementById(submitButtonId).addEventListener('click', function (event) {
event.preventDefault();
validationErrorMessageElement.innerHTML = "";
validationErrorMessageElement.style.display = 'none';
const formData = new URLSearchParams(new FormData(document.getElementById(loginFormId))).toString();
console.log("formData", formData);
fetch(loginUrl, {
method: 'POST',
// NOTE: Don't post it as a JSON data. Its `Content-Type` should be `application/x-www-form-urlencoded`.
headers: {'Content-Type': 'application/x-www-form-urlencoded'},
body: formData
})
.then(handleErrors)
.then(response => response.json())
.then(data => {
console.log(data);
alert("Succeed!");
doRefreshCaptcha();
})
.catch(error => {
alert("Failed! Try again!");
error.json().then((json) => {
console.log(json);
validationErrorMessageElement.innerHTML = errorToString(json);
validationErrorMessageElement.style.display = 'block';
});
doRefreshCaptcha();
});
});
}
</script>
</body>
</html>