// 取得access_token function get_access_token() { console.log(localStorage.access_token); // test return localStorage.access_token; } // 檢查是否可存取該頁面 function checkRoute() { let access_token = get_access_token(); if(access_token == undefined) { window.location.replace("login.html"); } } // 帳號登入 $("#btn_login").click(function() { var username = $('#username').val(); var password = $('#password').val(); var param_string = { username: username, password: password }; if (username == '' || password == '') { return; } $.ajax({ type: 'POST', url: 'https://api.ptt.cx:8750/api/v1/login/access-token', data: param_string, success: function(res, textStatus, jqXHR) { // {access_token: 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjE2N…kyIn0.wwRK7OKPICdbcOhLfgqA5u5uxkhRVP2gQeqEF8tCY4g', token_type: 'bearer'} console.log(res); // test // 將access_token寫入localStorage localStorage.access_token = res.access_token; // 檢查是否已登入 let access_token = get_access_token(); if (access_token == undefined) { Swal.fire({ title: '權限不足', icon: 'info', confirmButtonColor: '#3085d6' }); window.location.replace("login.html"); } if (access_token) { // 導頁進Dashboard介面 Swal.fire({ title: '登入成功', icon: 'success', confirmButtonColor: '#3085d6' }); window.setTimeout(() => { window.location.href = 'index.html'; }, 1500); } else { console.log('else'); // test } }, error: function(xhr, status, error) { console.log(xhr.responseText); // test var res = JSON.parse(xhr.responseText); console.log(res.detail); if (res.detail == 'Incorrect email or password') { Swal.fire({ title: '注意', icon: 'error', text: '帳號/密碼錯誤,請重新輸入', confirmButtonColor: '#3085d6' }); } else { Swal.fire({ title: '注意', icon: 'error', text: '權限不足', confirmButtonColor: '#3085d6' }); } } }); return false; });