This commit is contained in:
leca 2025-02-02 16:43:42 +03:00
parent f1b569a9ec
commit 0803970233
6 changed files with 62 additions and 7 deletions

19
public/js/login.js Normal file
View File

@ -0,0 +1,19 @@
$(document).ready(() => {
$("#loginForm").submit(() => {
$.ajax({
type: "POST",
url: "/api/login",
data: JSON.stringify({
username: $("#username").val(),
password: $("#password").val(),
}),
contentType: 'application/json',
success: function() {
window.location.href = "/index";
},
error: function(xhr) {
alert (xhr.responseText);
}
})
})
});

20
public/js/register.js Normal file
View File

@ -0,0 +1,20 @@
$(document).ready(() => {
$("#registerForm").submit(() => {
$.ajax({
type: "POST",
url: "/api/register",
data: JSON.stringify({
username: $("#username").val(),
password: $("#password").val(),
inviteToken: $("#inviteToken").val()
}),
contentType: 'application/json',
success: function() {
window.location.href = "/index";
},
error: function(xhr) {
alert (xhr.responseText);
}
})
})
});

View File

@ -11,9 +11,17 @@ import utils from '../utils.js';
dotenv.config({path: ".env"});
class UserController {
async register(req, res) {
const {username, password, passwordConfirm} = req.body;
const { username, password } = req.body;
if (password != passwordConfirm) return res.status(400).send("Passwords do not match");
if (username.length > 16) {
return res.status(400).send("Nickname is too long! Maximum length is 16 characters.");
}
const usernameRegexp = new RegExp("^[a-zA-Z0-9_]{2,16}$");
if (!usernameRegexp.test(username)) {
return res.status(400).send("Nickname can only contain alphanumeric and underscores.");
}
const hashedPassword = await bcrypt.hash(password, 8);
@ -24,7 +32,7 @@ class UserController {
}
req.session.jwt = jwt.sign({ username }, process.env.SECRET, {expiresIn: "1y"});
return res.redirect("/index");
return res.status(200).send("Ok");
}
async login(req, res) {
@ -36,7 +44,7 @@ class UserController {
return res.status(403).send("Password is not correct");
}
req.session.jwt = jwt.sign({ username }, process.env.SECRET, {expiresIn: "1y"});
return res.redirect("/index");
return res.status(200).send("Ok");
}
async changePassword(req, res) {

View File

@ -24,6 +24,7 @@ const validateInviteToken = async (req, res, next) => {
let tokenValid = false;
inviteTokens.forEach((token) => {
console.log(`${token} == ${inviteToken}`)
if (token == inviteToken) tokenValid = true;
});

View File

@ -7,11 +7,13 @@ html
link(href="css/particles.css" rel="stylesheet")
link(href="css/auth.css" rel="stylesheet")
body
iframe(name="hiddenFrame" style="position:absolute; top:-1px; left:-1px; width:1px; height:1px;")
div(id="tsparticles")
main(class="box")
h2 Вход
form(method="post", action="/api/login")
form(id="loginForm" target="hiddenFrame")
div(class="inputBox")
label(for="username") Ник
input(type="text" name="username" id="username" placeholder="ваш ник на сервере" required=true)
@ -23,7 +25,8 @@ html
div
button(type="submit" name="" style="float: left;") Войти
a(class="button" href="register" style="float: left;") Регистрация
script(src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.7.1/jquery.min.js" integrity="sha512-v2CJ7UaYy4JwqLDIrZUI/4hqeoQieOmAZNXBeQyjo21dadnwR+8ZaIJVT8EE2iyI61OV8e6M8PP2/4hpQINQ/g==" crossorigin="anonymous" referrerpolicy="no-referrer")
script(src="https://cdn.jsdelivr.net/npm/tsparticles@1.34.1/tsparticles.min.js" integrity="sha256-D6LXCdCl4meErhc25yXnxIFUtwR96gPo+GtLYv89VZo=" crossorigin="anonymous")
script(type="text/javascript" src="js/particles.js")
script(type="text/javascript" src="js/login.js")

View File

@ -8,11 +8,13 @@ html
title Регистрация
body
iframe(name="hiddenFrame" style="position:absolute; top:-1px; left:-1px; width:1px; height:1px;")
div(id="tsparticles")
main(class="box")
h2 Регистрация
form(action="/api/register" method="POST")
form(id="registerForm" target="hiddenFrame")
div(class="inputBox")
label(for="username") Ник
input(type="text" name="username" id="username" placeholder="ваш ник на сервере" required)
@ -28,5 +30,7 @@ html
input(type="text" name="inviteToken" id="inviteToken" placeholder="код приглашения" required)
button(type="submit" name="" style="float: left;") Зарегистрироваться
a(class="button" href="login" style="float: left;") Войти
script(src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.7.1/jquery.min.js" integrity="sha512-v2CJ7UaYy4JwqLDIrZUI/4hqeoQieOmAZNXBeQyjo21dadnwR+8ZaIJVT8EE2iyI61OV8e6M8PP2/4hpQINQ/g==" crossorigin="anonymous" referrerpolicy="no-referrer")
script(src="https://cdn.jsdelivr.net/npm/tsparticles@1.34.1/tsparticles.min.js" integrity="sha256-D6LXCdCl4meErhc25yXnxIFUtwR96gPo+GtLYv89VZo=" crossorigin="anonymous")
script(type="text/javascript" src="js/particles.js")
script(type="text/javascript" src="js/register.js")