|
@@ -1,3 +1,19 @@
|
|
|
|
+// 取得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() {
|
|
$("#btn_login").click(function() {
|
|
var username = $('#username').val();
|
|
var username = $('#username').val();
|
|
@@ -17,22 +33,32 @@ $("#btn_login").click(function() {
|
|
url: 'https://api.ptt.cx:8750/api/v1/login/access-token',
|
|
url: 'https://api.ptt.cx:8750/api/v1/login/access-token',
|
|
data: param_string,
|
|
data: param_string,
|
|
success: function(res, textStatus, jqXHR) {
|
|
success: function(res, textStatus, jqXHR) {
|
|
|
|
+ // {access_token: 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjE2N…kyIn0.wwRK7OKPICdbcOhLfgqA5u5uxkhRVP2gQeqEF8tCY4g', token_type: 'bearer'}
|
|
console.log(res); // test
|
|
console.log(res); // test
|
|
|
|
|
|
// 將access_token寫入localStorage
|
|
// 將access_token寫入localStorage
|
|
- // {access_token: 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjE2N…kyIn0.wwRK7OKPICdbcOhLfgqA5u5uxkhRVP2gQeqEF8tCY4g', token_type: 'bearer'}
|
|
|
|
- var access_token = res.access_token;
|
|
|
|
|
|
+ localStorage.access_token = res.access_token;
|
|
|
|
+
|
|
|
|
+ // 檢查是否已登入
|
|
|
|
+ let access_token = get_access_token();
|
|
|
|
|
|
- if (access_token != null) {
|
|
|
|
|
|
+ if (access_token == undefined) {
|
|
|
|
+ window.location.replace("login.html");
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ if (access_token) {
|
|
// 導頁進Dashboard介面
|
|
// 導頁進Dashboard介面
|
|
Swal.fire({
|
|
Swal.fire({
|
|
title: '登入成功',
|
|
title: '登入成功',
|
|
icon: 'success',
|
|
icon: 'success',
|
|
confirmButtonColor: '#3085d6'
|
|
confirmButtonColor: '#3085d6'
|
|
});
|
|
});
|
|
|
|
+
|
|
window.setTimeout(() => {
|
|
window.setTimeout(() => {
|
|
window.location.href = 'index.html';
|
|
window.location.href = 'index.html';
|
|
}, 1500);
|
|
}, 1500);
|
|
|
|
+ } else {
|
|
|
|
+ console.log('else'); // test
|
|
}
|
|
}
|
|
}, error: function(xhr, status, error) {
|
|
}, error: function(xhr, status, error) {
|
|
console.log(xhr.responseText); // test
|
|
console.log(xhr.responseText); // test
|