New register page

This commit is contained in:
xfarrow 2024-03-25 10:07:54 +01:00
parent 6414eff42f
commit e1bb44ca18
3 changed files with 285 additions and 103 deletions

View File

@ -1,6 +1,7 @@
<html>
<head>
<title>Blink - Sign In</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet"
integrity="sha384-QWTKZyjpPEjISv5WaRU9OFeRpok6YctnYmDr5pNlyT2bRjXh0JMhjY6hW+ALEwIH" crossorigin="anonymous">
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/js/bootstrap.bundle.min.js"
@ -53,9 +54,9 @@
<!-- Email input -->
<div class="form-outline mb-4">
<input type="email" id="email" class="form-control form-control-lg"
placeholder="Enter a valid email address" onblur="emailFieldLosesFocus();" />
placeholder="Enter your email address" onblur="emailFieldLosesFocus();" />
<div class="invalid-feedback">
Please enter a valid e-mail
Please enter an email address
</div>
</div>
@ -82,8 +83,7 @@
<div class="text-center text-lg-start mt-4 pt-2">
<button type="button" onclick="login()" class="btn btn-primary btn-lg"
style="padding-left: 2.5rem; padding-right: 2.5rem;">Login</button>
<p class="small fw-bold mt-2 pt-1 mb-0">Don't have an account? <a href="register.html"
class="link-danger">Register</a></p>
<p class="small fw-bold mt-2 pt-1 mb-0">Don't have an account? <a href="register.html">Register</a></p>
</div>
</form>
</div>
@ -99,13 +99,13 @@
}
/*
* This function is responsible for the log-in process
*/
* This function is responsible for the log-in process
*/
async function login() {
const email = document.getElementById("email").value;
const password = document.getElementById("password").value;
if (!email || !password) {
if (!validateFields()) {
return;
}
@ -129,9 +129,22 @@
}
/*
* This function is responsible for loading a random stock photo seen on the
* left side of the page
* Validate the field before the submit. This helps to prevent useless API
* calls. Retrurns true or false
*/
function validateFields(){
const email = document.getElementById("email").value;
const password = document.getElementById("password").value;
if(!email || !password || !validateEmail(email) || password.length < 5){
return false;
}
return true;
}
/*
* This function is responsible for loading a random stock photo seen on the
* left side of the page
*/
function loadImage() {
const random = Math.floor(Math.random() * 4);
if (random == 0) {
@ -146,9 +159,9 @@
}
/*
* This function gets called when the e-mail field loses focus. This is
* done to inform the user whether the entered e-mail is in a valid format
*/
* This function gets called when the e-mail field loses focus. This is
* done to inform the user whether the entered e-mail is in a valid format
*/
function emailFieldLosesFocus() {
const emailField = document.getElementById("email");
if (!emailField.value || !validateEmail(emailField.value)) {
@ -159,20 +172,9 @@
}
/*
* Validates an e-mail using a RegExpression
*/
function validateEmail(email) {
return String(email)
.toLowerCase()
.match(
/^(([^<>()[\]\\.,;:\s@"]+(\.[^<>()[\]\\.,;:\s@"]+)*)|.(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/
);
}
/*
* This function gets called when the password field loses focus. This is
* done to inform the user that they are required to enter a password
*/
* This function gets called when the password field loses focus. This is
* done to inform the user that they are required to enter a password
*/
function passwordFieldLosesFocus() {
const passwordField = document.getElementById("password");
if (!passwordField.value) {
@ -183,8 +185,8 @@
}
/*
* This function shows an error using a Bootstrap's alert element
*/
* This function shows an error using a Bootstrap's alert element
*/
function showError(message) {
const alertDiv = document.getElementById('hiddenAlertMessage');
alertDiv.innerHTML = message;

View File

@ -1,89 +1,248 @@
<!DOCTYPE html>
<html lang="en">
<html>
<head>
<meta charset="UTF-8">
<title>Sign Up to Blink</title>
<link rel="stylesheet" href="../css/login-register.css">
<title>Blink - Sign Up</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet"
integrity="sha384-QWTKZyjpPEjISv5WaRU9OFeRpok6YctnYmDr5pNlyT2bRjXh0JMhjY6hW+ALEwIH" crossorigin="anonymous">
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/js/bootstrap.bundle.min.js"
integrity="sha384-YvpcrYf0tY3lHB60NNkmXc5s9fDVZLESaAA55NDzOxhy9GkcIdslK1eN7N6jIeHz" crossorigin="anonymous">
</script>
</head>
<body>
<div id="login-form-wrap">
<h2>Sign Up</h2>
<form id="login-form">
<p>
<input type="text" id="displayname" name="displayname" placeholder="Your name" required><i
class="validation"><span></span><span></span></i>
</p>
<p>
<input type="email" id="email" name="email" placeholder="Email Address" required><i
class="validation"><span></span><span></span></i>
</p>
<p>
<input type="password" id="password" name="password" placeholder="Password" required><i
class="validation"><span></span><span></span></i>
</p>
<p>
<button type="button" onclick="register(); return false;">Register</button>
</p>
</form>
<div id="create-account-wrap">
<p>Already a member? <a href="./login.html">Login</a>
<p>
<div class="alert alert-success" role="alert" id="successAlert" style="display: none;">
</div>
</div>
<script src="../js/constants.js"></script>
<script src="../js/utils.js"></script>
<div class="alert alert-danger" role="alert" id="errorAlert" style="display: none;">
</div>
<script>
async function register() {
const display_name = document.getElementById('displayname').value;
const email = document.getElementById('email').value;
const password = document.getElementById('password').value;
<div class="bg-light py-3 py-md-5">
<div class="container">
<div class="row justify-content-md-center">
<div class="col-12 col-md-11 col-lg-8 col-xl-7 col-xxl-6">
<div class="bg-white p-4 p-md-5 rounded shadow-sm">
<div class="row">
<div class="col-12">
<div class="text-center mb-5">
<!-- <a href="#!">
<img src="./assets/img/bsb-logo.svg" alt="BootstrapBrain Logo" width="175"
height="57">
</a> -->
<h1 style="font-family: sans-serif">Sign up to Blink</h1>
</div>
</div>
</div>
<div class="row gy-3 gy-md-4 overflow-hidden">
<div class="col-12">
<label for="firstName" class="form-label">Name <span
class="text-danger">*</span></label>
<div class="input-group">
<span class="input-group-text">
<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16"
fill="currentColor" class="bi bi-person-vcard" viewBox="0 0 16 16">
<path
d="M5 8a2 2 0 1 0 0-4 2 2 0 0 0 0 4m4-2.5a.5.5 0 0 1 .5-.5h4a.5.5 0 0 1 0 1h-4a.5.5 0 0 1-.5-.5M9 8a.5.5 0 0 1 .5-.5h4a.5.5 0 0 1 0 1h-4A.5.5 0 0 1 9 8m1 2.5a.5.5 0 0 1 .5-.5h3a.5.5 0 0 1 0 1h-3a.5.5 0 0 1-.5-.5" />
<path
d="M2 2a2 2 0 0 0-2 2v8a2 2 0 0 0 2 2h12a2 2 0 0 0 2-2V4a2 2 0 0 0-2-2zM1 4a1 1 0 0 1 1-1h12a1 1 0 0 1 1 1v8a1 1 0 0 1-1 1H8.96c.026-.163.04-.33.04-.5C9 10.567 7.21 9 5 9c-2.086 0-3.8 1.398-3.984 3.181A1.006 1.006 0 0 1 1 12z" />
</svg>
</span>
<input type="text" class="form-control" name="displayname" id="displayname" required
onblur="displaynameFieldLosesFocus();">
<div class="invalid-feedback">
Please fill out this field
</div>
</div>
</div>
<div class="col-12">
<label for="email" class="form-label">Email <span class="text-danger">*</span></label>
<div class="input-group">
<span class="input-group-text">
<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16"
fill="currentColor" class="bi bi-envelope" viewBox="0 0 16 16">
<path
d="M0 4a2 2 0 0 1 2-2h12a2 2 0 0 1 2 2v8a2 2 0 0 1-2 2H2a2 2 0 0 1-2-2V4Zm2-1a1 1 0 0 0-1 1v.217l7 4.2 7-4.2V4a1 1 0 0 0-1-1H2Zm13 2.383-4.708 2.825L15 11.105V5.383Zm-.034 6.876-5.64-3.471L8 9.583l-1.326-.795-5.64 3.47A1 1 0 0 0 2 13h12a1 1 0 0 0 .966-.741ZM1 11.105l4.708-2.897L1 5.383v5.722Z" />
</svg>
</span>
<input type="email" class="form-control" name="email" id="email" required
onblur="emailFieldLosesFocus();">
<div class="invalid-feedback">
Please enter an email address
</div>
</div>
</div>
<div class="col-12">
<label for="password" class="form-label">Password <span
class="text-danger">*</span></label>
<div class="input-group">
<span class="input-group-text">
<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16"
fill="currentColor" class="bi bi-key" viewBox="0 0 16 16">
<path
d="M0 8a4 4 0 0 1 7.465-2H14a.5.5 0 0 1 .354.146l1.5 1.5a.5.5 0 0 1 0 .708l-1.5 1.5a.5.5 0 0 1-.708 0L13 9.207l-.646.647a.5.5 0 0 1-.708 0L11 9.207l-.646.647a.5.5 0 0 1-.708 0L9 9.207l-.646.647A.5.5 0 0 1 8 10h-.535A4 4 0 0 1 0 8zm4-3a3 3 0 1 0 2.712 4.285A.5.5 0 0 1 7.163 9h.63l.853-.854a.5.5 0 0 1 .708 0l.646.647.646-.647a.5.5 0 0 1 .708 0l.646.647.646-.647a.5.5 0 0 1 .708 0l.646.647.793-.793-1-1h-6.63a.5.5 0 0 1-.451-.285A3 3 0 0 0 4 5z" />
<path d="M4 8a1 1 0 1 1-2 0 1 1 0 0 1 2 0z" />
</svg>
</span>
<input type="password" class="form-control" name="password" id="password" value=""
required onblur="passwordFieldLosesFocus();">
<div class="invalid-feedback" id="passwordErrorLabel">
</div>
</div>
</div>
<div class="col-12">
<div class="form-check">
<input class="form-check-input" type="checkbox" value="" name="iAgree" id="iAgree"
required>
<label class="form-check-label text-secondary" for="iAgree">
I agree to the <a href="./privacypolicy_and_tou/Terms-Of-Use.html"
class="link-primary text-decoration-none">terms
and conditions</a> and to the <a class="link-primary text-decoration-none"
href="./privacypolicy_and_tou/Privacy-Policy-English.html">privacy
policy</a>
</label>
</div>
</div>
<div class="col-12">
<div class="d-grid">
<button class="btn btn-primary btn-lg" type="button" onclick="register();">Sign
Up</button>
</div>
</div>
</div>
<div class="row">
<div class="col-12">
<hr class="mt-5 mb-4 border-secondary-subtle">
<p class="m-0 text-secondary text-center">Already have an account? <a href="#!"
class="link-primary text-decoration-none">Sign in</a></p>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
if (!display_name || !email || !password) {
alert('Please fill in all fields');
return;
}
<script src="../js/constants.js"></script>
<script src="../js/utils.js"></script>
<script>
async function register() {
const display_name = document.getElementById('displayname').value;
const email = document.getElementById('email').value;
const password = document.getElementById('password').value;
const options = {
method: 'POST',
headers: createHeaders(null),
body: JSON.stringify({
display_name,
email,
password
}),
};
fetch(`${API_URL}/persons`, options)
.then(response => {
response.json().then(data => {
if (response.ok) {
if (!data.enabled) { // is the user already enabled or do they need email verification?
alert("Congratulations! You've successfully registered to Blink. " +
"Please click on the e-mail we sent you to confirm your account");
} else {
alert("Congratulations! You've successfully registered to Blink. " +
"You can now log in");
}
window.location.href = '/login.html';
} else {
callbackErrors(data.errors, alert);
if (!validateFields()) {
return;
}
});
})
.catch(err => {
alert("An error has occurred :-( please try again later");
console.error(err);
});
return false;
}
</script>
const options = {
method: 'POST',
headers: createHeaders(null),
body: JSON.stringify({
display_name,
email,
password
}),
};
fetch(`${API_URL}/persons`, options)
.then(response => {
response.json().then(data => {
if (response.ok) {
clearInputFields();
if (!data.enabled) { // is the user already enabled or do they need email verification?
showSuccessAlert("Congratulations! You've successfully registered to Blink. " +
"Please click on the e-mail we sent you to confirm your account");
} else {
showSuccessAlert("Congratulations! You've successfully registered to Blink. " +
"You can now <a href='login.html'>log in</a>");
}
window.location.href = '/login.html';
} else {
callbackErrors(data.errors, showErrorAlert);
}
});
})
.catch(err => {
alert("An error has occurred :-( please try again later");
console.error(err);
});
return false;
}
/*
* This function gets called when the displayname field loses focus.
* This is
* done to inform the user that they need to enter at least one
* character in said field.
*/
function displaynameFieldLosesFocus() {
const nameField = document.getElementById("displayname");
if (!nameField.value) {
nameField.classList.add("is-invalid");
} else {
nameField.classList.remove("is-invalid");
}
}
/*
* This function gets called when the e-mail field loses focus. This is
* done to inform the user whether the entered e-mail is in a valid format
*/
function emailFieldLosesFocus() {
const emailField = document.getElementById("email");
if (!emailField.value || !validateEmail(emailField.value)) {
emailField.classList.add("is-invalid");
} else {
emailField.classList.remove("is-invalid");
}
}
/*
* This function gets called when the password field loses focus. This is
* done to inform the user that they are required to enter a password
*/
function passwordFieldLosesFocus() {
const passwordField = document.getElementById("password");
if (!passwordField.value) {
passwordField.classList.add("is-invalid");
document.getElementById('passwordErrorLabel').innerHTML = 'Please fill out this field';
} else if (passwordField.value.length < 5) {
passwordField.classList.add("is-invalid");
document.getElementById('passwordErrorLabel').innerHTML = 'Password must be at least 5 characters';
} else {
passwordField.classList.remove("is-invalid");
}
}
/*
* Validate the field before the submit. This helps to prevent useless API
* calls. Retrurns true or false
*/
function validateFields() {
const email = document.getElementById("email").value;
const password = document.getElementById("password").value;
const displayname = document.getElementById("displayname").value;
const iAgree = document.getElementById('iAgree');
if (!email || !password || !displayname || !validateEmail(email) || password.length < 5 || !iAgree
.checked) {
return false;
}
return true;
}
function showSuccessAlert(message) {
const successAlert = document.getElementById('successAlert');
successAlert.innerHTML = message;
successAlert.style.display = 'block';
}
function showErrorAlert(message){
const errorAlert = document.getElementById('errorAlert');
errorAlert.innerHTML = message;
errorAlert.style.display = 'block';
}
</script>
</body>
</html>

View File

@ -10,6 +10,20 @@ function getCookie(name) {
return null;
}
/**
* Validates an e-mail using a RegExpression
*
* @param {*} email
* @returns
*/
function validateEmail(email) {
return String(email)
.toLowerCase()
.match(
/^(([^<>()[\]\\.,;:\s@"]+(\.[^<>()[\]\\.,;:\s@"]+)*)|.(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/
);
}
function callbackErrors(errors, func) {
errors.forEach(error => func(error.msg));
}
@ -20,3 +34,10 @@ function createHeaders(token) {
"Authorization": `Bearer ${token}`
}
}
function clearInputFields() {
var inputs = document.querySelectorAll('input');
inputs.forEach(function(input) {
input.value = '';
});
}