|
@@ -1,13 +1,17 @@
|
|
|
const btnLogin = document.querySelector('#btn_login');
|
|
|
-const inputPassword = document.querySelector('#password');
|
|
|
+const inputPassword = document.querySelector('#login #password');
|
|
|
+const btnRegister = document.querySelector('.btn-register');
|
|
|
const btnLoginPage = document.querySelector('.btn-login');
|
|
|
const btnUserProfile = document.querySelector('.btn-userProfile');
|
|
|
const btnLogout = document.querySelector('.btn-logout');
|
|
|
+const registerPassword = document.querySelector('#register #password');
|
|
|
|
|
|
inputPassword.addEventListener('keyup', loginByEnter);
|
|
|
-
|
|
|
btnLogin.addEventListener('click', login);
|
|
|
|
|
|
+registerPassword.addEventListener('keyup', registerByEnter);
|
|
|
+btnRegister.addEventListener('click', register);
|
|
|
+
|
|
|
function loginByEnter(e) {
|
|
|
if (e.keyCode === 13) {
|
|
|
e.preventDefault();
|
|
@@ -16,6 +20,14 @@ function loginByEnter(e) {
|
|
|
}
|
|
|
};
|
|
|
|
|
|
+function registerByEnter(e) {
|
|
|
+ if (e.keyCode === 13) {
|
|
|
+ e.preventDefault();
|
|
|
+ console.log('login!');
|
|
|
+ register();
|
|
|
+ }
|
|
|
+};
|
|
|
+
|
|
|
function login(){
|
|
|
console.log('login!');
|
|
|
var url = "http://www.choozmo.com:8887/login";
|
|
@@ -59,6 +71,65 @@ function login(){
|
|
|
console.log(result);
|
|
|
}
|
|
|
|
|
|
+function validateEmail(email) {
|
|
|
+ const re = /^(([^<>()[\]\\.,;:\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,}))$/;
|
|
|
+ return re.test(String(email).toLowerCase());
|
|
|
+}
|
|
|
+
|
|
|
+function validatePassword(psd) {
|
|
|
+ if(psd.length >=4) {
|
|
|
+ return true;
|
|
|
+ } else {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+function register() {
|
|
|
+ const userName = $('#register [name = "username"]').val();
|
|
|
+ const email = $('#register [name = "email"]').val();
|
|
|
+ const password = $('#register [name = "password"]').val();
|
|
|
+ if(userName && validateEmail(email) && validatePassword(password)) {
|
|
|
+ let userObj = {
|
|
|
+ username: userName,
|
|
|
+ email,
|
|
|
+ password
|
|
|
+ }
|
|
|
+ console.log(userObj);
|
|
|
+ const headers = {
|
|
|
+ "accept": "application/json",
|
|
|
+ "Content-Type": "application/json"
|
|
|
+ }
|
|
|
+ axios({
|
|
|
+ method: 'post',
|
|
|
+ url: 'http://www.choozmo.com:8887/register',
|
|
|
+ headers: headers,
|
|
|
+ data: userObj
|
|
|
+ }).then(res => {
|
|
|
+ console.log(res.data.msg);
|
|
|
+ if(res.data.msg == 'ok') {
|
|
|
+ Swal.fire({
|
|
|
+ title: "註冊成功",
|
|
|
+ icon: 'success',
|
|
|
+ text: res.data.msg,
|
|
|
+ confirmButtonColor: '#3085d6',
|
|
|
+ });
|
|
|
+ window.setTimeout(() => {
|
|
|
+ window.location.href = 'login.html';
|
|
|
+ }, 2000);
|
|
|
+ } else {
|
|
|
+ Swal.fire({
|
|
|
+ title: "註冊失敗",
|
|
|
+ icon: 'error',
|
|
|
+ text: res.data.msg,
|
|
|
+ confirmButtonColor: '#3085d6',
|
|
|
+ });
|
|
|
+ }
|
|
|
+ }).catch(err => {
|
|
|
+ console.log(err);
|
|
|
+ })
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
|
|
|
function loginControl() {
|
|
|
btnLoginPage.style.display = 'block';
|