diff --git a/.gitignore b/.gitignore index 9c39416c539b1b5aef50acb58fd01f8e226bb2cb..656e2c5e305cd1258d2f019c7cde2f224a0f319e 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ db/* +config/* diff --git a/app/controller/user/UserDb.php b/app/controller/user/UserDb.php index deeed4e53f20d1765736f32ed5e181fac8117fba..1d59300ae70129dae2006f8b71b106df2287a9c6 100644 --- a/app/controller/user/UserDb.php +++ b/app/controller/user/UserDb.php @@ -84,5 +84,34 @@ } return null; } + + function isEmailExist($email){ + $sql = 'SELECT 1 from user WHERE email=?'; + $stmt = $this->conn->prepare($sql); + + if ($stmt->execute([$email])){ + if (count($stmt->fetchAll()) == 0) { + return false; + } else { + return true; + } + } + + return false; + } + + function isUsernameExist($username){ + $sql = 'SELECT 1 from user WHERE username=?'; + $stmt = $this->conn->prepare($sql); + + if ($stmt->execute([$username])){ + if (count($stmt->fetchAll()) == 0) { + return false; + } else { + return true; + } + } + return false; + } } ?> \ No newline at end of file diff --git a/app/controller/user/UserUsecase.php b/app/controller/user/UserUsecase.php index 3e75c4087df6c2da769af027821589a12ad42641..c773eac6e70ff9867ba387e5508547a6a6bf9555 100644 --- a/app/controller/user/UserUsecase.php +++ b/app/controller/user/UserUsecase.php @@ -91,6 +91,26 @@ writeResponse(400, "Incorrect username or password"); } } + + function validateUsername(Request $request){ + $username = $request->queries["username"]; + $isExist = $this->userDb->isUsernameExist($username); + if ($isExist) { + writeResponse(200, "Username exist", $isExist); + } else { + writeResponse(200, "Username not exist", $isExist); + } + } + + function validateEmail(Request $request){ + $email = $request->queries["email"]; + $isExist = $this->userDb->isEmailExist($email); + if ($isExist) { + writeResponse(200, "Email exist", $isExist); + } else { + writeResponse(200, "Email not exist", $isExist); + } + } } ?> \ No newline at end of file diff --git a/app/controller/user/routes.php b/app/controller/user/routes.php index 8e57f437cc0144e3afcb78a263b3e7006bc1430e..8f05eaf4f4fb39ec9b7bdd6b849545dfb9f846a8 100644 --- a/app/controller/user/routes.php +++ b/app/controller/user/routes.php @@ -6,6 +6,8 @@ $router->add("/api/user/:user_id/", "PUT", array($userUsecase,'editProfile')); // Auth $router->add("/auth/login/", "POST", array($userUsecase,'login')); + $router->add("/api/user/validateEmail/", "GET", array($userUsecase,'validateEmail')); + $router->add("/api/user/validateUsername/", "GET", array($userUsecase,'validateUsername')); return $router; } ?> \ No newline at end of file diff --git a/config/devel.php b/config/devel.php index 7a9949ef27af01a4190ad6b83cb445ec68148915..ad2af678967799b27b8760715bed5fd89e87b67d 100644 --- a/config/devel.php +++ b/config/devel.php @@ -3,7 +3,7 @@ "db" => array( "host" => "localhost", "user" => "root", - "password" => "06071998", + "password" => "rahasiailahi", "db_name" => "tayo_book_store" ), "base_url" => "http://localhost:4000/", diff --git a/public/static/html/register.html b/public/static/html/register.html index 9ce2c82da6d84a045d9b89475f23d5d0ee3f5be7..bb27b105d48b9a4a6dcbe025707f6eae8c04cbd1 100644 --- a/public/static/html/register.html +++ b/public/static/html/register.html @@ -1,7 +1,7 @@ <link rel="stylesheet" href="/static/css/register.css"> <link rel="stylesheet" href="/static/css/base.css"> -<div class="container"> +<div class="container" > <div class="register-form"> <h1 class="form-title">REGISTER</h1> <div class="row"> @@ -15,7 +15,8 @@ </div> <div class="row"> <label for="email_form" class="form-label">Email</label> - <input class="form-input validation" type="email" name="email" id="email_form"> + <input class="form-input validation" type="email" name="email" id="email_form" + > <span class="check" id="email_check">✓</span> </div> <div class="row"> @@ -37,7 +38,9 @@ <a class="register-link" href="/login/">Already have an account?</a> <br> <div class="row"> - <button class="submit-button">Register</button> + <button class="submit-button" id="register_button">Register</button> </div> </div> -</div> \ No newline at end of file +</div> + +<script src="/static/js/register.js"></script> \ No newline at end of file diff --git a/public/static/js/login.js b/public/static/js/login.js index 9ba787e6d97044d812bff7735ec24792cdf6c2c4..296e4c0ab4b971566b50aeaa7b86926699a831ab 100644 --- a/public/static/js/login.js +++ b/public/static/js/login.js @@ -8,7 +8,6 @@ const doLogin = function(){ } doAjax('/auth/login/',"POST", payload, function(response){ - console.log(response); setCookie("Authorization", response.data.token, 300); window.location.replace('http://localhost:4000/browse/'); }, function(response){ diff --git a/public/static/js/register.js b/public/static/js/register.js index df4dfba0a480eeea22f05022a07f8bdf7bbc5cc3..daa27f523257d636b190360c06878ce355da1297 100644 --- a/public/static/js/register.js +++ b/public/static/js/register.js @@ -1,29 +1,173 @@ const name = document.getElementById('name_form'); const username = document.getElementById('username_form'); +const usernameCheck = document.getElementById('username_check'); const email = document.getElementById('email_form'); +const emailCheck = document.getElementById('email_check'); const password = document.getElementById('password_form'); const confirmPassword = document.getElementById('confirm_password_form'); const address = document.getElementById('address_form'); const phone = document.getElementById('phone_form'); -function doRegister(){ - const nameValue = name.value; - const usernameValue = username.value; - const emailValue = email.value; - const passwordValue = password.value; - const confirmPasswordValue = confirmPassword.value; - const addressValue = address.value; - const phoneValue = phone.value; +let isNameValid = false; +let isEmailValid = false; +let isUsernameValid = false; +let isPasswordValid = false; +let isConfirmPasswordValid = false; +let isAddressValid = false; +let isPhoneValid = false; + +const errorStyle = "1pt solid red"; + +function enableValidateUsername(){ + username.addEventListener("focusout", function(){ + const usernameValue = username.value; + const validateUsernameURL = '/api/user/validateUsername/?username='+username.value; + doAjax(validateUsernameURL, "GET", null, function(response){ + if ((usernameValue.length<=20 && usernameValue.length>0) && !response.data){ + isUsernameValid = true; + username.style.border = ""; + usernameCheck.style.display = "block"; + }else { + isUsernameValid = false; + username.style.border = errorStyle; + usernameCheck.style.display = "none"; + } + }) + }); } -function validateUsername(){ - let isValid = false; - username.addEventListener("change", function(){ - +function enableValidateEmail(){ + const emailRegex = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/; + email.addEventListener("focusout", function(){ + const emailValue = email.value; + const validateEmailURL = '/api/user/validateEmail/?email='+email.value; + doAjax(validateEmailURL, "GET", null, function(response){ + console.log(response.data); + if (emailValue.length>0 && !response.data && emailRegex.test(email.value) ){ + isEmailValid = true; + email.style.border = ""; + emailCheck.style.display = "block"; + }else { + isEmailValid = false; + email.style.border = errorStyle; + emailCheck.style.display = "none"; + } + }) }); } +function enableValidateName(){ + name.addEventListener("focusout", function(){ + const nameValue = name.value; + if (nameValue.length>0){ + name.style.border = ""; + isNameValid = true; + } else { + name.style.border = errorStyle; + isNameValid = false; + } + }); +} -window.onload = function(){ +function enableValidatePassword(){ + password.addEventListener("focusout", function(){ + const passwordValue = password.value; + if (passwordValue.length>0){ + password.style.border = ""; + isPasswordValid = true; + } else { + password.style.border = errorStyle; + isPasswordValid = false; + } + }); +} + +function enableValidateConfirmPassword(){ + confirmPassword.addEventListener("focusout", function(){ + const confirmPasswordValue = confirmPassword.value; + if (confirmPasswordValue.length>0 && confirmPasswordValue == password.value){ + isConfirmPasswordValid = true; + confirmPassword.style.border = ""; + } else { + isConfirmPasswordValid = false; + confirmPassword.style.border = errorStyle; + } + }); +} + +function enableValidateAddress(){ + address.addEventListener("focusout", function(){ + const addressValue = address.value; + if (addressValue.length>0){ + isAddressValid = true; + address.style.border = ""; + } else { + isAddressValid = false; + address.style.border = errorStyle; + } + }); +} + +function enableValidatePhone(){ + phone.addEventListener("focusout", function(){ + const phoneValue = phone.value; + if (phoneValue.length>=9 && phoneValue.length<=12){ + isPhoneValid = true; + phone.style.border = ""; + } else { + isPhoneValid = false; + phone.style.border = errorStyle; + } + }); +} + + +function validateForm(){ + enableValidateName(); + enableValidateUsername(); + enableValidateEmail(); + enableValidatePassword(); + enableValidateConfirmPassword(); + enableValidateAddress(); + enableValidatePhone(); +} + +const doRegister = function(){ + console.log("test"); + if (isNameValid && isEmailValid && isPasswordValid && isUsernameValid && isConfirmPasswordValid && isAddressValid && isPhoneValid){ + const nameValue = name.value; + const usernameValue = username.value; + const emailValue = email.value; + const passwordValue = password.value; + const confirmPasswordValue = confirmPassword.value; + const addressValue = address.value; + const phoneValue = phone.value; + + let registerPayload = { + "fullname" : nameValue, + "username" : usernameValue, + "email" : emailValue, + "password" : passwordValue, + "address" : addressValue, + "phone" : phoneValue + } + doAjax('/api/user/', "POST", registerPayload, function(response){ + let loginPayload = { + "username" : usernameValue, + "password" : passwordValue + } + doAjax('/auth/login/',"POST", loginPayload, function(loginResponse){ + setCookie("Authorization", loginResponse.data.token, 300); + window.location.replace('http://localhost:4000/browse/'); + }); + }) + + } +} + +window.onload = function(){ + validateForm(); + let registerButton = document.getElementById("register_button"); + registerButton.onclick = doRegister; } \ No newline at end of file