Captcha implemented

This commit is contained in:
MathMan05 2024-07-11 14:45:29 -05:00
parent 7274a7a3f1
commit 97c264d888
3 changed files with 51 additions and 12 deletions

View file

@ -145,13 +145,17 @@ if (instancein) {
checkInstance("https://spacebar.chat/"); checkInstance("https://spacebar.chat/");
} }
} }
async function login(username, password) { async function login(username, password, captcha) {
if (captcha === "") {
captcha = undefined;
}
const options = { const options = {
method: "POST", method: "POST",
body: JSON.stringify({ body: JSON.stringify({
"login": username, "login": username,
"password": password, "password": password,
"undelete": false "undelete": false,
"captcha_key": captcha
}), }),
headers: { headers: {
"Content-type": "application/json; charset=UTF-8", "Content-type": "application/json; charset=UTF-8",
@ -168,9 +172,22 @@ async function login(username, password) {
console.log("test"); console.log("test");
} }
//this.serverurls||!this.email||!this.token //this.serverurls||!this.email||!this.token
adduser({ serverurls: JSON.parse(localStorage.getItem("instanceinfo")), email: username, token: response.token }); console.log(response);
window.location.href = '/channels/@me'; if (response.captcha_sitekey) {
return response.token; const capt = document.getElementById("h-captcha");
const capty = document.createElement("div");
capty.classList.add("h-captcha");
capty.setAttribute("data-sitekey", response.captcha_sitekey);
const script = document.createElement("script");
script.src = "https://js.hcaptcha.com/1/api.js";
capt.append(script);
capt.append(capty);
}
else {
adduser({ serverurls: JSON.parse(localStorage.getItem("instanceinfo")), email: username, token: response.token });
window.location.href = '/channels/@me';
return response.token;
}
}); });
} }
catch (error) { catch (error) {
@ -203,7 +220,7 @@ async function setInstance(url) {
} }
async function check(e) { async function check(e) {
e.preventDefault(); e.preventDefault();
let h = await login(e.srcElement[1].value, e.srcElement[2].value); let h = await login(e.srcElement[1].value, e.srcElement[2].value, e.srcElement[3].value);
document.getElementById("wrong").textContent = h; document.getElementById("wrong").textContent = h;
console.log(h); console.log(h);
} }

View file

@ -18,6 +18,10 @@
<label for="psw"><b>Password:</b></label><br> <label for="psw"><b>Password:</b></label><br>
<input type="password" placeholder="Enter Password" name="psw" required><br><br><br><br> <input type="password" placeholder="Enter Password" name="psw" required><br><br><br><br>
<p class="wrongred" id="wrong"></p> <p class="wrongred" id="wrong"></p>
<div id="h-captcha">
</div>
<button type="submit">Login</button> <button type="submit">Login</button>
</form> </form>
<a href="/register.html">Don't have an account?</a> <a href="/register.html">Don't have an account?</a>

View file

@ -144,13 +144,17 @@ if(instancein){
} }
async function login(username, password){ async function login(username:string, password:string, captcha:string){
if(captcha===""){
captcha=undefined;
}
const options={ const options={
method: "POST", method: "POST",
body:JSON.stringify({ body:JSON.stringify({
"login": username, "login": username,
"password": password, "password": password,
"undelete":false "undelete":false,
"captcha_key":captcha
}), }),
headers: { headers: {
"Content-type": "application/json; charset=UTF-8", "Content-type": "application/json; charset=UTF-8",
@ -166,9 +170,23 @@ async function login(username, password){
console.log("test") console.log("test")
} }
//this.serverurls||!this.email||!this.token //this.serverurls||!this.email||!this.token
adduser({serverurls:JSON.parse(localStorage.getItem("instanceinfo")),email:username,token:response.token}); console.log(response);
window.location.href = '/channels/@me';
return response.token; if(response.captcha_sitekey){
const capt=document.getElementById("h-captcha");
const capty=document.createElement("div");
capty.classList.add("h-captcha");
capty.setAttribute("data-sitekey", response.captcha_sitekey);
const script=document.createElement("script");
script.src="https://js.hcaptcha.com/1/api.js";
capt.append(script);
capt.append(capty);
}else{
adduser({serverurls:JSON.parse(localStorage.getItem("instanceinfo")),email:username,token:response.token});
window.location.href = '/channels/@me';
return response.token;
}
}) })
}catch(error){ }catch(error){
console.error('Error:', error); console.error('Error:', error);
@ -202,7 +220,7 @@ async function setInstance(url){
async function check(e){ async function check(e){
e.preventDefault(); e.preventDefault();
let h=await login(e.srcElement[1].value,e.srcElement[2].value); let h=await login(e.srcElement[1].value,e.srcElement[2].value,e.srcElement[3].value);
document.getElementById("wrong").textContent=h; document.getElementById("wrong").textContent=h;
console.log(h); console.log(h);
} }