diff --git a/public/js/auth.js b/public/js/auth.js index a5220eb..684e7aa 100644 --- a/public/js/auth.js +++ b/public/js/auth.js @@ -2,12 +2,22 @@ var page = { // user token - token: localStorage.token + token: localStorage.token, + + // HTML elements + form: null, + user: null, + pass: null } -page.do = function (dest) { - var user = document.getElementById('user').value - var pass = document.getElementById('pass').value +page.do = function (dest, onEnter) { + var user = page.user.value + var pass = page.pass.value + + // If the form is submitted with Enter button and the form is still empty + if (onEnter && !user.length && !pass.length) { return } + + console.log('page.do()\'ing: ' + dest) if (!user) { return swal('Error', 'You need to specify a username', 'error') @@ -54,14 +64,33 @@ page.verify = function () { }) } +page.formEnter = function (event) { + if (event.keyCode === 13 || event.which === 13) { + event.preventDefault() + event.stopPropagation() + page.do('login', true) + } +} + window.onload = function () { page.verify() - document.body.addEventListener('keydown', function (event) { - event = event || window.event - if (!event) { return } - var id = event.target.id - if (!['user', 'pass'].includes(id)) { return } - if (event.keyCode === 13 || event.which === 13) { page.do('login') } + page.user = document.getElementById('user') + page.pass = document.getElementById('pass') + + page.form = document.getElementById('authForm') + page.form.addEventListener('keyup', page.formEnter) + page.form.addEventListener('keypress', page.formEnter) + page.form.onsubmit = function (event) { + event.preventDefault() + event.stopPropagation() + } + + document.getElementById('loginBtn').addEventListener('click', function () { + page.do('login') + }) + + document.getElementById('registerBtn').addEventListener('click', function () { + page.do('register') }) } diff --git a/views/_globals.njk b/views/_globals.njk index 13484b1..3fb9cf9 100644 --- a/views/_globals.njk +++ b/views/_globals.njk @@ -13,7 +13,7 @@ v2: Images and config files (manifest.json, browserconfig.xml, etc). v3: CSS and JS files (libs such as bulma, lazyload, etc). #} -{% set v1 = "Xo8dt9SFWE" %} +{% set v1 = "iz81GIx05U" %} {% set v2 = "Ii3JYKIhb0" %} {% set v3 = "HrvcYD3KTh" %} diff --git a/views/auth.njk b/views/auth.njk index 89b8135..1dd1e00 100644 --- a/views/auth.njk +++ b/views/auth.njk @@ -28,34 +28,36 @@

Login or register

-
-
- +
+
+
+ +
-
-
-
- +
+
+ +
-
-
-
- - - - - Register - +
+ +
+ +
- -
+