Issue #140:
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
import { defineStore } from "pinia";
|
||||
import axios from 'axios';
|
||||
import apiError from '@/module/apiError.js';
|
||||
|
||||
export default defineStore('loginStore', {
|
||||
// data, methods, computed
|
||||
@@ -12,11 +13,6 @@ export default defineStore('loginStore', {
|
||||
isInvalid: false,
|
||||
userData: {},
|
||||
}),
|
||||
|
||||
// 讓元件取用相關的資料狀態
|
||||
getters: {
|
||||
},
|
||||
|
||||
actions: {
|
||||
/**
|
||||
* fetch Login For Access Token api
|
||||
@@ -32,23 +28,18 @@ export default defineStore('loginStore', {
|
||||
|
||||
try {
|
||||
const response = await axios.post(api, this.auth, config);
|
||||
|
||||
if(response.status === 200){
|
||||
// 將 token 儲存在 cookie
|
||||
const token = response.data.access_token;
|
||||
// 取得當前日期
|
||||
const currentDate = new Date();
|
||||
// 設定 cookie 的過期日期為一天後
|
||||
const expirationDate = new Date();
|
||||
expirationDate.setDate(currentDate.getDate() + 1);
|
||||
// 格式化過期日期為 Cookie 格式
|
||||
const expires = expirationDate.toUTCString();
|
||||
|
||||
document.cookie = `luciaToken=${token}; expires=${expires};`;
|
||||
this.$router.push('/files');
|
||||
}
|
||||
const token = response.data.access_token;
|
||||
// 將 token 儲存在 cookie
|
||||
document.cookie = `luciaToken=${token};`;
|
||||
// // 取得當前日期
|
||||
// const currentDate = new Date();
|
||||
// // 設定 cookie 的過期日期為一天後
|
||||
// const expirationDate = new Date();
|
||||
// expirationDate.setDate(currentDate.getDate() + 1);
|
||||
// // 格式化過期日期為 Cookie 格式
|
||||
// const expires = expirationDate.toUTCString();
|
||||
this.$router.push('/files');
|
||||
} catch(error) {
|
||||
console.dir(error); // safari 測試
|
||||
this.isInvalid = true;
|
||||
};
|
||||
},
|
||||
@@ -56,11 +47,8 @@ export default defineStore('loginStore', {
|
||||
* Logout, tooken expired
|
||||
*/
|
||||
logOut() {
|
||||
let isCookie = document.cookie.split(';').some(c => c.trim().startsWith('luciaToken='));
|
||||
let expires = new Date();
|
||||
expires.setTime(expires.getTime() - 60000);
|
||||
|
||||
if(isCookie) document.cookie = `luciaToken=; expires=${expires.toGMTString()}`;
|
||||
delete axios.defaults.headers.common["Authorization"];
|
||||
document.cookie = 'luciaToken=; expires=Thu, 01 Jan 1970 00:00:00 UTC;';
|
||||
this.$router.push('/login');
|
||||
},
|
||||
/**
|
||||
@@ -74,7 +62,7 @@ export default defineStore('loginStore', {
|
||||
|
||||
this.userData = response.data;
|
||||
} catch(error) {
|
||||
console.dir(error); // safari 測試
|
||||
apiError(error, 'Failed to load the Map.');
|
||||
};
|
||||
},
|
||||
/**
|
||||
@@ -85,11 +73,9 @@ export default defineStore('loginStore', {
|
||||
|
||||
try {
|
||||
const response = await axios.get(api);
|
||||
if(response.status !== 200) this.$router.push('/login');
|
||||
} catch(error) {
|
||||
console.dir(error); // safari 測試
|
||||
this.$router.push('/login');
|
||||
};
|
||||
},
|
||||
}
|
||||
})
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user