sign up almost fully working

This commit is contained in:
2023-08-05 11:52:29 +02:00
parent d9adc91256
commit f9e6b8fc17
10 changed files with 379 additions and 11 deletions

View File

@@ -55,7 +55,7 @@ class SQLDB {
if ( error ) throw error;
if ( results[ 0 ][ '@@default_storage_engine' ] !== 'InnoDB' ) return 'DB HAS TO USE InnoDB!';
} );
this.sqlConnection.query( 'CREATE TABLE libreevent_users ( account_id INT ( 10 ) NOT NULL AUTO_INCREMENT, email TINYTEXT NOT NULL, pass TEXT, name TEXT, first_name TEXT, two_fa TINYTEXT, user_data VARCHAR( 60000 ), PRIMARY KEY ( account_id ) ) ENGINE=INNODB;', ( error ) => {
this.sqlConnection.query( 'CREATE TABLE libreevent_users ( account_id INT ( 10 ) NOT NULL AUTO_INCREMENT, email TINYTEXT NOT NULL, pass TEXT, name TEXT, first_name TEXT, two_fa TINYTEXT, user_data VARCHAR( 60000 ), mail_confirmed TINYTEXT, marketing_ok TINYTEXT, PRIMARY KEY ( account_id ) ) ENGINE=INNODB;', ( error ) => {
if ( error ) if ( error.code !== 'ER_TABLE_EXISTS_ERROR' ) throw error;
this.sqlConnection.query( 'CREATE TABLE libreevent_orders ( order_id INT ( 10 ) NOT NULL AUTO_INCREMENT, account_id INT ( 10 ) NOT NULL, seats VARCHAR( 60000 ), PRIMARY KEY ( order_id ), FOREIGN KEY ( account_id ) REFERENCES libreevent_users( account_id ) ) ENGINE=INNODB;', ( error ) => {
if ( error ) if ( error.code !== 'ER_TABLE_EXISTS_ERROR' ) throw error;

View File

@@ -18,6 +18,7 @@ const bodyParser = require( 'body-parser' );
let responseObjects = {};
let authOk = {};
let mailTokens = {};
module.exports = ( app, settings ) => {
app.get( '/user/details', ( request, response ) => {
@@ -134,21 +135,23 @@ module.exports = ( app, settings ) => {
app.get( '/user/logout', ( request, response ) => {
request.session.loggedInUser = false;
request.session.username = '';
response.send( 'logoutOk' );
} );
app.post( '/user/signup', bodyParser.json(), ( request, response ) => {
// TODO: Make sure that user does not exist yet first and if user
// exists, send back info that it is that way
// TODO: check for 2fa
// TODO: Send mail to confirm email address
if ( request.body.password && request.body.password === request.body.password2 && request.body.firstName && request.body.name && request.body.country && request.body.mail ) {
db.checkDataAvailability( 'users', 'email', request.body.mail ).then( status => {
if ( status ) {
response.send( 'exists' );
} else {
db.writeDataSimple( 'users', 'email', request.body.mail, { 'pass': pwdmanager.hashPassword( request.body.password ), 'first_name': request.body.firstName, 'name': request.body.name, 'two_fa': String( request.body.twoFA ), 'user_data': { 'country': request.body.country } } ).then( () => {
response.send( 'ok' );
pwdmanager.hashPassword( request.body.password ).then( hash => {
db.writeDataSimple( 'users', 'email', request.body.mail, { 'email': request.body.mail, 'pass': hash, 'first_name': request.body.firstName, 'name': request.body.name, 'two_fa': 'disabled', 'user_data': JSON.stringify( { 'country': request.body.country } ) } ).then( () => {
request.session.loggedInUser = true;
request.session.username = request.body.mail;
response.send( 'ok' );
} );
} );
}
} );
@@ -156,4 +159,37 @@ module.exports = ( app, settings ) => {
response.status( 400 ).send( 'incomplete' );
}
} );
app.get( '/user/signup/confirm', ( request, response ) => {
if ( Object.keys( mailTokens ).includes( request.query.token ) ) {
request.session.username = mailTokens[ request.query.token ];
db.writeDataSimple( 'users', 'email', request.session.username, { 'mail_confirmed': 'true' } );
delete mailTokens[ request.query.token ];
if ( settings.twoFA === 'allow' ) {
response.sendFile( path.join( __dirname + '/../ui/en/signup/allowTwoFA.html' ) );
} else if ( settings.twoFA === 'enforce' ) {
response.sendFile( path.join( __dirname + '/../ui/en/signup/enforceTwoFA.html' ) );
} else {
response.sendFile( path.join( __dirname + '/../ui/en/signup/disallowTwoFA.html' ) );
}
} else {
response.sendFile( path.join( __dirname + '/../ui/en/signup/invalid.html' ) );
}
} );
app.post( '/user/settings/:setting', bodyParser.json(), ( request, response ) => {
let call = request.params.setting;
if ( request.session.username ) {
if ( call === '2fa' ) {
db.writeDataSimple( 'users', 'email', request.session.username, { 'two_fa': request.body.twoFA } );
response.send( 'ok' );
}
} else {
response.send( 'unauthorised' );
}
} );
app.get( '/settings/2fa', ( request, response ) => {
response.send( settings.twoFA );
} );
};

View File

@@ -1,6 +1,7 @@
{
"init": true,
"twoFA": "enhanced",
"twoFA": "allow",
"twoFAMode": "enhanced",
"db": "mysql",
"payments": "stripe",
"name": "libreevent",

View File

@@ -0,0 +1,122 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Email verification</title>
<style>
body, html {
width: 100%;
height: 100%;
margin: 0;
padding: 0;
}
body {
display: flex;
justify-content: center;
align-items: center;
flex-direction: column;
font-family: Avenir, Helvetica, Arial, sans-serif;
text-align: center;
background-color: rgb(41, 40, 40);
color: white;
font-size: 150%;
}
.content {
width: 70%;
}
#code {
padding: 0.75%;
border: solid white 1px;
border-radius: 7px;
font-size: 100%;
text-align: center;
}
.submit {
margin-top: 2%;
background: linear-gradient(90deg, rgb(30, 36, 131), rgb(87, 66, 184), rgb(105, 115, 214), rgb(30, 36, 131), rgb(41, 128, 109), rgb(146, 50, 47));
background-size: 300px;
padding: 10px 20px;
border: none;
border-radius: 20px;
cursor: pointer;
transition: all 3s;
font-size: 75%;
color: white;
}
.submit:hover {
background-size: 200%;
background-position: -100%;
}
#popup {
border: none;
border-radius: 20px;
padding: 5%;
background-color: rgb(34, 34, 34);
color: white;
max-width: 70%;
}
#popup::backdrop {
background-color: rgba( 0, 0, 0, 0.8 );
}
</style>
</head>
<body>
<div class="content">
<h1>Email Verification</h1>
<p id="text">This website requires you to use Two-Factor Authentication. Please choose your mode below. By default, the enhanced mode is enabled which requires you to type a 6-character code into a field after confirming the mail address. You can change this setting at any point later.</p>
<form onsubmit="return submitFunction()" id="form">
<select name="2fa" id="2fa">
<option value="enhanced">Enhanced</option>
<option value="simple">Simple</option>
</select><br>
<input type="submit" value="Submit" class="submit">
</form>
<dialog id="popup">
<p id="popup-message"></p>
<form method="dialog">
<input type="submit" value="Ok" class="submit">
</form>
</dialog>
</div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.1/jquery.min.js"></script>
<script>
function submitFunction () {
let code = document.getElementById( '2fa' ).value;
let data = '';
let fetchOptions = {
method: 'post',
body: JSON.stringify( { 'code': data, 'token': location.search.substring( 7 ) } ),
headers: {
'Content-Type': 'application/json',
'charset': 'utf-8'
}
};
fetch( '/user/settings/2fa', fetchOptions ).then( res => {
res.text().then( data => {
if ( data === 'ok' ) {
document.getElementById( 'text' ).innerText = 'Your settings have been saved! You may change them at any point. You may now close this tab.';
document.getElementById( 'form' ).innerHTML = '';
openPopup( 'Your settings have been saved! You may change them at any point. You may now close this tab.' );
} else {
openPopup( 'An error occurred whilst saving your settings. Please try again. If it does not work, you can change this setting later at any point in the account page.' );
}
} );
} );
return false;
}
function openPopup ( message ) {
document.getElementById( 'popup-message' ).innerHTML = message;
document.getElementById( 'popup' ).showModal();
}
</script>
</body>
</html>

View File

@@ -0,0 +1,38 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Email Verification Successful</title>
<style>
body, html {
width: 100%;
height: 100%;
margin: 0;
padding: 0;
}
body {
display: flex;
justify-content: center;
align-items: center;
flex-direction: column;
font-family: Avenir, Helvetica, Arial, sans-serif;
text-align: center;
background-color: rgb(41, 40, 40);
color: white;
font-size: 150%;
}
.content {
width: 70%;
}
</style>
</head>
<body>
<div class="content">
<h1>Email Verification Successful</h1>
<p>Thank you! Your email address has been verified successfully. You may now close this tab.</p>
</div>
</body>
</html>

View File

@@ -0,0 +1,123 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Email verification</title>
<style>
body, html {
width: 100%;
height: 100%;
margin: 0;
padding: 0;
}
body {
display: flex;
justify-content: center;
align-items: center;
flex-direction: column;
font-family: Avenir, Helvetica, Arial, sans-serif;
text-align: center;
background-color: rgb(41, 40, 40);
color: white;
font-size: 150%;
}
.content {
width: 70%;
}
#code {
padding: 0.75%;
border: solid white 1px;
border-radius: 7px;
font-size: 100%;
text-align: center;
}
.submit {
margin-top: 2%;
background: linear-gradient(90deg, rgb(30, 36, 131), rgb(87, 66, 184), rgb(105, 115, 214), rgb(30, 36, 131), rgb(41, 128, 109), rgb(146, 50, 47));
background-size: 300px;
padding: 10px 20px;
border: none;
border-radius: 20px;
cursor: pointer;
transition: all 3s;
font-size: 75%;
color: white;
}
.submit:hover {
background-size: 200%;
background-position: -100%;
}
#popup {
border: none;
border-radius: 20px;
padding: 5%;
background-color: rgb(34, 34, 34);
color: white;
max-width: 70%;
}
#popup::backdrop {
background-color: rgba( 0, 0, 0, 0.8 );
}
</style>
</head>
<body>
<div class="content">
<h1>Email Verification</h1>
<p id="text">We strongly encourage you to enable Two-Factor authentication for your account. Below you have the choice between not enabling it, enabling a mode where you just have to click the link in the email and you're in (simple) and a mode where you have to click the link in the mail and confirm the login by typing the code displayed on the main window (enhanced).</p>
<form onsubmit="return submitFunction()" id="form">
<select name="2fa" id="2fa">
<option value="enhanced">Enhanced</option>
<option value="simple">Simple</option>
<option value="disabled">Disabled</option>
</select><br>
<input type="submit" value="Submit" class="submit">
</form>
<dialog id="popup">
<p id="popup-message"></p>
<form method="dialog">
<input type="submit" value="Ok" class="submit">
</form>
</dialog>
</div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.1/jquery.min.js"></script>
<script>
function submitFunction () {
let code = document.getElementById( '2fa' ).value;
let data = '';
let fetchOptions = {
method: 'post',
body: JSON.stringify( { 'code': data, 'token': location.search.substring( 7 ) } ),
headers: {
'Content-Type': 'application/json',
'charset': 'utf-8'
}
};
fetch( '/user/settings/2fa', fetchOptions ).then( res => {
res.text().then( data => {
if ( data === 'ok' ) {
document.getElementById( 'text' ).innerText = 'Your settings have been saved! You may change them at any point. You may now close this tab.';
document.getElementById( 'form' ).innerHTML = '';
openPopup( 'Your settings have been saved! You may change them at any point. You may now close this tab.' );
} else {
openPopup( 'An error occurred whilst saving your settings. Please try again. If it does not work, you can change this setting later at any point in the account page.' );
}
} );
} );
return false;
}
function openPopup ( message ) {
document.getElementById( 'popup-message' ).innerHTML = message;
document.getElementById( 'popup' ).showModal();
}
</script>
</body>
</html>

View File

@@ -0,0 +1,38 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Email Verification Token Invalid</title>
<style>
body, html {
width: 100%;
height: 100%;
margin: 0;
padding: 0;
}
body {
display: flex;
justify-content: center;
align-items: center;
flex-direction: column;
font-family: Avenir, Helvetica, Arial, sans-serif;
text-align: center;
background-color: rgb(41, 40, 40);
color: white;
font-size: 150%;
}
.content {
width: 70%;
}
</style>
</head>
<body>
<div class="content">
<h1>Email Verification Token Invalid</h1>
<p>The email verification token you specified is invalid. Please check that it is correct and try again. If it still doesn't work, please request a new one by logging into your <a href="/account">account</a> and clicking "resend email verification token"</p>
</div>
</body>
</html>