login.js 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. const btnLogin = document.querySelector('#btn_login');
  2. const inputPassword = document.querySelector('#login #password');
  3. const btnLoginPage = document.querySelector('.btn-login');
  4. const btnUserProfile = document.querySelector('.btn-userProfile');
  5. const btnLogout = document.querySelector('.btn-logout');
  6. const btnLoginPage_d = document.querySelector('.login-top .btn-login');
  7. const btnUserProfile_d = document.querySelector('.login-top .btn-userProfile');
  8. const btnLogout_d= document.querySelector('.login-top .btn-logout');
  9. inputPassword.addEventListener('keyup', loginByEnter);
  10. btnLogin.addEventListener('click', login);
  11. function loginByEnter(e) {
  12. if (e.keyCode === 13) {
  13. e.preventDefault();
  14. console.log('login!');
  15. login();
  16. }
  17. };
  18. function login(){
  19. console.log('login!');
  20. var url = "https://www.choozmo.com:8887/login";
  21. var username = $('#username').val();
  22. var password = $('#password').val();
  23. if (username == '' || password == '') {
  24. let title = "登入失敗";
  25. let text = "請先輸入您的帳號/密碼";
  26. if (lang == 'en') {
  27. title = "Login Failed!";
  28. text = "Please enter your username and password";
  29. }
  30. Swal.fire({
  31. title: title,
  32. icon: 'error',
  33. text: text,
  34. confirmButtonColor: '#3085d6',
  35. });
  36. return;
  37. }
  38. var data = "grant_type=&username=" + username + "&password="+password+"&scope=&client_id=&client_secret=";
  39. const headers = {
  40. "accept": "application/json",
  41. "Content-Type": "application/x-www-form-urlencoded"
  42. }
  43. axios({
  44. method: 'post',
  45. url: url,
  46. headers: headers,
  47. data: data
  48. }).then(res => {
  49. console.log(res.data);
  50. document.cookie = 'jwt_token='+res.data.access_token;
  51. if(res.data.access_token!=null && res.data.veri == 'ok') {
  52. var title = "登入成功";
  53. if (lang == 'en') { // 英文版訊息
  54. title = "Login Successfully!"
  55. }
  56. Swal.fire({
  57. title: title,
  58. icon: 'success',
  59. confirmButtonColor: '#3085d6',
  60. });
  61. window.setTimeout(() => {
  62. window.location.href = 'user_profile2.html';
  63. }, 2000);
  64. btnLoginPage.style.display = 'none';
  65. } else if (res.data.access_token!=null && res.data.veri != 'ok') {
  66. var title = "請前往信箱完成驗證";
  67. if (lang == 'en') { // 英文版訊息
  68. title = "Please check your email box to get validated!"
  69. }
  70. Swal.fire({
  71. title: title,
  72. icon: 'error',
  73. text: responseOBJ.detail,
  74. confirmButtonColor: '#3085d6',
  75. });
  76. } else {
  77. var title = "登入失敗";
  78. if (lang == 'en') { // 英文版訊息
  79. title = "Login Failed!"
  80. }
  81. Swal.fire({
  82. title: title,
  83. icon: 'error',
  84. text: responseOBJ.detail,
  85. confirmButtonColor: '#3085d6',
  86. });
  87. }
  88. }).catch(err => {
  89. console.log(err);
  90. var title = "錯誤處理中,請稍後再試";
  91. if (lang == 'en') { // 英文版訊息
  92. title = "Oops! Errors occurred. Please try it later!"
  93. }
  94. Swal.fire({
  95. title: title,
  96. icon: 'error',
  97. confirmButtonColor: '#3085d6',
  98. });
  99. })
  100. }
  101. /* var xhr = new XMLHttpRequest();
  102. xhr.open("POST", url);
  103. xhr.setRequestHeader("accept", "application/json");
  104. xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
  105. xhr.onreadystatechange = function () {
  106. if (xhr.readyState === 4) {
  107. console.log(xhr.responseText); // test
  108. responseOBJ = JSON.parse(xhr.responseText);
  109. // document.cookie = 'jwt_token='+responseOBJ.jwt_token // access_token -> jwt_token
  110. document.cookie = 'jwt_token='+responseOBJ.access_token;
  111. console.log(responseOBJ);
  112. if (responseOBJ.access_token!=null)
  113. {
  114. var title = "登入成功";
  115. if (lang == 'en') { // 英文版訊息
  116. title = "Login Successfully!"
  117. }
  118. Swal.fire({
  119. title: title,
  120. icon: 'success',
  121. confirmButtonColor: '#3085d6',
  122. });
  123. window.setTimeout(() => {
  124. window.location.href = 'user_profile2.html';
  125. }, 2000);
  126. btnLoginPage.style.display = 'none';
  127. }
  128. else{
  129. var title = "登入失敗";
  130. if (lang == 'en') { // 英文版訊息
  131. title = "Login Failed!"
  132. }
  133. Swal.fire({
  134. title: title,
  135. icon: 'error',
  136. text: responseOBJ.detail,
  137. confirmButtonColor: '#3085d6',
  138. });
  139. }
  140. }
  141. };
  142. // 登入資料檢查
  143. if (username == '' || password == '') {
  144. let title = "登入失敗";
  145. let text = "請先輸入您的帳號/密碼";
  146. if (lang == 'en') {
  147. title = "Login Failed!";
  148. text = "Please enter your username and password";
  149. }
  150. Swal.fire({
  151. title: title,
  152. icon: 'error',
  153. text: text,
  154. confirmButtonColor: '#3085d6',
  155. });
  156. return;
  157. }
  158. result = xhr.send(data);
  159. console.log(result);
  160. } */
  161. function loginControl() {
  162. btnLoginPage.style.display = 'block';
  163. btnLogout.style.display = 'none';
  164. btnUserProfile.style.display = 'none';
  165. btnLoginPage_d.style.display = 'block';
  166. btnLogout_d.style.display = 'none';
  167. btnUserProfile_d.style.display = 'none';
  168. }
  169. loginControl();