New UI layout
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

login.js 3.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. const serverUrl = 'http://localhost:3088/';
  2. const emailInputEl = document.getElementById('email');
  3. const passwordInputEl = document.getElementById('password');
  4. const emailErrEl = document.getElementById('emailErrMsg');
  5. const passwordErrEl = document.getElementById('passwordErrMsg');
  6. document.getElementById('passwordBox').style.display = 'none';
  7. function showPasswordBox() {
  8. const email = emailInputEl.value;
  9. if (email.includes('@')) {
  10. document.getElementById('emailBox').style.display = 'none';
  11. document.getElementById('passwordBox').style.display = 'block';
  12. document.getElementById('entered-email').innerHTML = email;
  13. emailInputEl.classList.remove('error');
  14. emailErrEl.innerText = '';
  15. } else {
  16. emailInputEl.classList.add('error');
  17. emailErrEl.innerText = 'Please enter valid email';
  18. }
  19. }
  20. function togglePassword() {
  21. const ele = document.getElementById('showPassword');
  22. const password = passwordInputEl;
  23. if (password.value.length > 0) {
  24. passwordInputEl.value = password.value;
  25. password.focus();
  26. if (ele.checked) {
  27. password.setAttribute('type', 'text');
  28. } else {
  29. password.setAttribute('type', 'password');
  30. }
  31. }
  32. }
  33. function onPasswordFocus() {
  34. if (passwordInputEl.value.length > 0) {
  35. document.getElementById('password-label').classList.add('password-fixed');
  36. } else {
  37. document
  38. .getElementById('password-label')
  39. .classList.remove('password-fixed');
  40. }
  41. }
  42. function setCookie(name, value, days) {
  43. var expires = '';
  44. if (days) {
  45. var date = new Date();
  46. date.setTime(date.getTime() + days * 24 * 60 * 60 * 1000);
  47. expires = '; expires=' + date.toUTCString();
  48. }
  49. document.cookie = name + '=' + (value || '') + expires + '; path=/';
  50. }
  51. function loginUser(email, password) {
  52. const postData = JSON.stringify({
  53. UserName: email,
  54. Password: password,
  55. UnibaseId: '',
  56. RememberMe: false,
  57. });
  58. const requestOptions = {
  59. method: 'POST',
  60. headers: {
  61. 'Content-Type': 'application/json',
  62. },
  63. body: postData,
  64. redirect: 'follow',
  65. };
  66. fetch(serverUrl + 'account/login', requestOptions)
  67. .then((response) => response.text())
  68. .then((result) => {
  69. const user = JSON.parse(result);
  70. setCookie('authentication', JSON.stringify(user.sessionId), 1);
  71. })
  72. .catch((error) => console.log('error', error));
  73. }
  74. function validateLogin() {
  75. const email = emailInputEl.value;
  76. const password = passwordInputEl.value;
  77. if (email.includes('@') && password.trim().length !== 0) {
  78. loginUser(email, password);
  79. } else if (!email.includes('@')) {
  80. emailErrEl.innerHTML = 'Please enter valid email';
  81. } else {
  82. passwordInputEl.classList.add('error');
  83. passwordErrEl.innerText = 'Please enter valid password';
  84. }
  85. }
  86. function initializeEventListeners() {
  87. document.getElementById('emailNextBtn').addEventListener('click', () => {
  88. showPasswordBox();
  89. });
  90. document.getElementById('showPassword').addEventListener('click', () => {
  91. togglePassword();
  92. });
  93. document.getElementById('showPassword').addEventListener('onfocusout', () => {
  94. onPasswordFocus();
  95. });
  96. document.getElementById('loginSubmitBtn').addEventListener('click', () => {
  97. validateLogin();
  98. });
  99. }
  100. initializeEventListeners();