[Frontend][全域功能] JWT 紀錄 原程序 localstorage 改成 cookie 存放

This commit is contained in:
dev01 2022-12-12 10:53:31 +08:00
parent e69dd715d7
commit 4a82696ad1
3 changed files with 16 additions and 8 deletions

View File

@ -2084,7 +2084,8 @@ License: You must have a valid license purchased only from wrapbootstrap.com (li
<script> <script>
/*$('#js-page-content').smartPanel();*/ /*$('#js-page-content').smartPanel();*/
var jwt = localStorage.getItem("JWT-Authorization"); var cookies = null;
var jwt = null;
var loadingTip = ''; var loadingTip = '';
var pageAct = {}; //記錄全頁面已選擇項目 var pageAct = {}; //記錄全頁面已選擇項目
pageAct.AreaTag = "TPE"; pageAct.AreaTag = "TPE";
@ -2145,8 +2146,8 @@ License: You must have a valid license purchased only from wrapbootstrap.com (li
function loadedMasterPack() { function loadedMasterPack() {
require( require(
[ [
"yourteam/yourteam.ajax.class",
"yourteam/yourteam.utility.class", "yourteam/yourteam.utility.class",
"yourteam/yourteam.ajax.class",
"yourteam/yourteam.jquery.datatables", "yourteam/yourteam.jquery.datatables",
"yourteam/plugins/yt-tooltip/yt-tooltip", "yourteam/plugins/yt-tooltip/yt-tooltip",
"yourteam/plugins/yt-tab/yt-tab", "yourteam/plugins/yt-tab/yt-tab",
@ -2162,7 +2163,10 @@ License: You must have a valid license purchased only from wrapbootstrap.com (li
* 全數套件讀取完成 Callback * 全數套件讀取完成 Callback
* */ * */
function loadedJsPack() { function loadedJsPack() {
cookies = new YourTeam.Utility.Cookie();
jwt = cookies.get("JWT-Authorization");
loadEvent(); loadEvent();
if (!jwt) { if (!jwt) {
myBaja = new MyBaja(); myBaja = new MyBaja();
myBaja.setMyUserAccount(Login); myBaja.setMyUserAccount(Login);
@ -2321,6 +2325,7 @@ License: You must have a valid license purchased only from wrapbootstrap.com (li
function Login(account) { function Login(account) {
console.log(account) console.log(account)
/*if ($("#login-form").valid()) {*/ /*if ($("#login-form").valid()) {*/
var url = baseApiUrl + "/api/Login/"; var url = baseApiUrl + "/api/Login/";
var send_data = { var send_data = {
account: account, account: account,
@ -2343,7 +2348,7 @@ License: You must have a valid license purchased only from wrapbootstrap.com (li
} }
else { else {
/*toast_ok(rel.msg);*/ /*toast_ok(rel.msg);*/
localStorage.setItem('JWT-Authorization', rel.data.token); cookies.create("JWT-Authorization", rel.data.token);
isValidLogin(); isValidLogin();
} }
}, },

View File

@ -47,7 +47,8 @@ class Ajax {
* @description beforeSendFunction * @description beforeSendFunction
*/ */
beforeSendFunction = function (xhr) { beforeSendFunction = function (xhr) {
let token = localStorage.getItem("JWT-Authorization"); let cookies = new YourTeam.Utility.Cookie();
let token = cookies.get("JWT-Authorization");
xhr.setRequestHeader('Authorization', "Bearer " + token); xhr.setRequestHeader('Authorization', "Bearer " + token);
} }
/** /**
@ -57,9 +58,11 @@ class Ajax {
* @return {Object} data * @return {Object} data
*/ */
successFunction = function (data, callback) { successFunction = function (data, callback) {
debugger
if (data && data.unauthorized == 401) { if (data && data.unauthorized == 401) {
let cookies = new YourTeam.Utility.Cookie();
let href = location.href; let href = location.href;
localStorage.removeItem("JWT-Authorization"); cookies.remove("JWT-Authorization");
if (href.indexOf("localhost:5966") == -1) { if (href.indexOf("localhost:5966") == -1) {
location.href = "/login"; location.href = "/login";
} else { } else {
@ -85,7 +88,7 @@ class Ajax {
window.alert("執行失敗,請聯絡客服人員。"); window.alert("執行失敗,請聯絡客服人員。");
} else if (xhr.status == "401") { } else if (xhr.status == "401") {
window.alert("登入時間超時,請重新登入!"); window.alert("登入時間超時,請重新登入!");
localStorage.removeItem("JWT-Authorization"); cookies.remove("JWT-Authorization");
location.href = "~/Login/Login"; location.href = "~/Login/Login";
} }
} }

View File

@ -229,7 +229,7 @@
} }
else { else {
toast_ok(rel.msg); toast_ok(rel.msg);
localStorage.setItem('JWT-Authorization', rel.data.token); cookies.create("JWT-Authorization", rel.data.token);
location.href = "index.html"; location.href = "index.html";
return; return;
} }