ark.js 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. // 帳號登入
  2. $("#btn_login").click(function() {
  3. var username = $('#username').val();
  4. var password = $('#password').val();
  5. var param_string = {
  6. username: username,
  7. password: password
  8. };
  9. if (username == '' || password == '') {
  10. return;
  11. }
  12. $.ajax({
  13. type: 'POST',
  14. url: 'https://api.ptt.cx:8750/api/v1/login/access-token',
  15. data: param_string,
  16. success: function(res, textStatus, jqXHR) {
  17. console.log(res); // test
  18. // 將access_token寫入localStorage
  19. // {access_token: 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjE2N…kyIn0.wwRK7OKPICdbcOhLfgqA5u5uxkhRVP2gQeqEF8tCY4g', token_type: 'bearer'}
  20. var access_token = res.access_token;
  21. if (access_token != null) {
  22. // 導頁進Dashboard介面
  23. Swal.fire({
  24. title: '登入成功',
  25. icon: 'success',
  26. confirmButtonColor: '#3085d6'
  27. });
  28. window.setTimeout(() => {
  29. window.location.href = 'index.html';
  30. }, 1500);
  31. }
  32. }, error: function(xhr, status, error) {
  33. console.log(xhr.responseText); // test
  34. var res = JSON.parse(xhr.responseText);
  35. if (res.detail == 'Incorrect email or password') {
  36. Swal.fire({
  37. title: '注意',
  38. icon: 'error',
  39. text: '帳號/密碼錯誤,請重新輸入',
  40. confirmButtonColor: '#3085d6'
  41. });
  42. }
  43. }
  44. });
  45. return false;
  46. });