script_index.js 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. const btnLogin = document.querySelector('#btn_login');
  2. const inputPassword = document.querySelector('#password');
  3. inputPassword.addEventListener('keyup', loginByEnter);
  4. btnLogin.addEventListener('click', login);
  5. function loginByEnter(e) {
  6. if (e.keyCode === 13) {
  7. e.preventDefault();
  8. console.log('login!');
  9. login();
  10. }
  11. };
  12. function login(){
  13. console.log('login!');
  14. var url = "login";
  15. var xhr = new XMLHttpRequest();
  16. xhr.open("POST", url);
  17. xhr.setRequestHeader("accept", "application/json");
  18. xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
  19. xhr.onreadystatechange = function () {
  20. if (xhr.readyState === 4) {
  21. responseOBJ = JSON.parse(xhr.responseText)
  22. // document.cookie = 'jwt_token='+responseOBJ.jwt_token // access_token -> jwt_token
  23. document.cookie = 'jwt_token='+responseOBJ.access_token;
  24. if (responseOBJ.access_token!=null)
  25. {
  26. Swal.fire({
  27. title: "登入成功",
  28. icon: 'success',
  29. confirmButtonColor: '#3085d6',
  30. },function(isConfirm){
  31. if(isConfirm){window.location.replace("/make_video");}
  32. }
  33. );
  34. }
  35. else{
  36. Swal.fire({
  37. title: "登入失敗",
  38. icon: 'error',
  39. text: responseOBJ.detail,
  40. confirmButtonColor: '#3085d6',
  41. });
  42. }
  43. }
  44. };
  45. var data = "grant_type=&username=" + $('#username').val() + "&password="+$('#password').val()+"&scope=&client_id=&client_secret=";
  46. result = xhr.send(data);
  47. console.log(result);
  48. }