jquery
This commit is contained in:
parent
f1b569a9ec
commit
0803970233
|
@ -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);
|
||||||
|
}
|
||||||
|
})
|
||||||
|
})
|
||||||
|
});
|
|
@ -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);
|
||||||
|
}
|
||||||
|
})
|
||||||
|
})
|
||||||
|
});
|
|
@ -11,9 +11,17 @@ import utils from '../utils.js';
|
||||||
dotenv.config({path: ".env"});
|
dotenv.config({path: ".env"});
|
||||||
class UserController {
|
class UserController {
|
||||||
async register(req, res) {
|
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);
|
const hashedPassword = await bcrypt.hash(password, 8);
|
||||||
|
|
||||||
|
@ -24,7 +32,7 @@ class UserController {
|
||||||
}
|
}
|
||||||
|
|
||||||
req.session.jwt = jwt.sign({ username }, process.env.SECRET, {expiresIn: "1y"});
|
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) {
|
async login(req, res) {
|
||||||
|
@ -36,7 +44,7 @@ class UserController {
|
||||||
return res.status(403).send("Password is not correct");
|
return res.status(403).send("Password is not correct");
|
||||||
}
|
}
|
||||||
req.session.jwt = jwt.sign({ username }, process.env.SECRET, {expiresIn: "1y"});
|
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) {
|
async changePassword(req, res) {
|
||||||
|
|
|
@ -24,6 +24,7 @@ const validateInviteToken = async (req, res, next) => {
|
||||||
let tokenValid = false;
|
let tokenValid = false;
|
||||||
|
|
||||||
inviteTokens.forEach((token) => {
|
inviteTokens.forEach((token) => {
|
||||||
|
console.log(`${token} == ${inviteToken}`)
|
||||||
if (token == inviteToken) tokenValid = true;
|
if (token == inviteToken) tokenValid = true;
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
|
@ -7,11 +7,13 @@ html
|
||||||
link(href="css/particles.css" rel="stylesheet")
|
link(href="css/particles.css" rel="stylesheet")
|
||||||
link(href="css/auth.css" rel="stylesheet")
|
link(href="css/auth.css" rel="stylesheet")
|
||||||
body
|
body
|
||||||
|
iframe(name="hiddenFrame" style="position:absolute; top:-1px; left:-1px; width:1px; height:1px;")
|
||||||
|
|
||||||
div(id="tsparticles")
|
div(id="tsparticles")
|
||||||
main(class="box")
|
main(class="box")
|
||||||
h2 Вход
|
h2 Вход
|
||||||
|
|
||||||
form(method="post", action="/api/login")
|
form(id="loginForm" target="hiddenFrame")
|
||||||
div(class="inputBox")
|
div(class="inputBox")
|
||||||
label(for="username") Ник
|
label(for="username") Ник
|
||||||
input(type="text" name="username" id="username" placeholder="ваш ник на сервере" required=true)
|
input(type="text" name="username" id="username" placeholder="ваш ник на сервере" required=true)
|
||||||
|
@ -23,7 +25,8 @@ html
|
||||||
div
|
div
|
||||||
button(type="submit" name="" style="float: left;") Войти
|
button(type="submit" name="" style="float: left;") Войти
|
||||||
a(class="button" href="register" 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(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/particles.js")
|
||||||
|
script(type="text/javascript" src="js/login.js")
|
||||||
|
|
|
@ -8,11 +8,13 @@ html
|
||||||
title Регистрация
|
title Регистрация
|
||||||
|
|
||||||
body
|
body
|
||||||
|
iframe(name="hiddenFrame" style="position:absolute; top:-1px; left:-1px; width:1px; height:1px;")
|
||||||
|
|
||||||
div(id="tsparticles")
|
div(id="tsparticles")
|
||||||
main(class="box")
|
main(class="box")
|
||||||
h2 Регистрация
|
h2 Регистрация
|
||||||
|
|
||||||
form(action="/api/register" method="POST")
|
form(id="registerForm" target="hiddenFrame")
|
||||||
div(class="inputBox")
|
div(class="inputBox")
|
||||||
label(for="username") Ник
|
label(for="username") Ник
|
||||||
input(type="text" name="username" id="username" placeholder="ваш ник на сервере" required)
|
input(type="text" name="username" id="username" placeholder="ваш ник на сервере" required)
|
||||||
|
@ -28,5 +30,7 @@ html
|
||||||
input(type="text" name="inviteToken" id="inviteToken" placeholder="код приглашения" required)
|
input(type="text" name="inviteToken" id="inviteToken" placeholder="код приглашения" required)
|
||||||
button(type="submit" name="" style="float: left;") Зарегистрироваться
|
button(type="submit" name="" style="float: left;") Зарегистрироваться
|
||||||
a(class="button" href="login" 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(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/particles.js")
|
||||||
|
script(type="text/javascript" src="js/register.js")
|
||||||
|
|
Loading…
Reference in New Issue