feat: Refresh Token in processes.
This commit is contained in:
@@ -12,9 +12,9 @@ export default defineStore('loginStore', {
|
|||||||
password: '',
|
password: '',
|
||||||
refresh_token: ''
|
refresh_token: ''
|
||||||
},
|
},
|
||||||
refreshToken: '',
|
|
||||||
isInvalid: false,
|
isInvalid: false,
|
||||||
userData: {},
|
userData: {},
|
||||||
|
// expired: new Date().setMonth(6), // 設定 Refresh Token 的到期日為半年後
|
||||||
}),
|
}),
|
||||||
actions: {
|
actions: {
|
||||||
/**
|
/**
|
||||||
@@ -32,17 +32,12 @@ export default defineStore('loginStore', {
|
|||||||
try {
|
try {
|
||||||
const response = await axios.post(api, this.auth, config);
|
const response = await axios.post(api, this.auth, config);
|
||||||
const accessToken = response.data.access_token;
|
const accessToken = response.data.access_token;
|
||||||
|
const refreshToken = response.data.refresh_token;
|
||||||
|
|
||||||
this.refreshToken = response.data.refresh_token;
|
|
||||||
// 將 token 儲存在 cookie
|
// 將 token 儲存在 cookie
|
||||||
document.cookie = `luciaToken=${accessToken};`;
|
document.cookie = `luciaToken=${accessToken}`;
|
||||||
// // 取得當前日期
|
// document.cookie = `luciaRefreshToken=${refreshToken};expires=${new Date(this.expired)};`;
|
||||||
// const currentDate = new Date();
|
|
||||||
// // 設定 cookie 的過期日期為一天後
|
|
||||||
// const expirationDate = new Date();
|
|
||||||
// expirationDate.setDate(currentDate.getDate() + 1);
|
|
||||||
// // 格式化過期日期為 Cookie 格式
|
|
||||||
// const expires = expirationDate.toUTCString();
|
|
||||||
this.$router.push('/files');
|
this.$router.push('/files');
|
||||||
} catch(error) {
|
} catch(error) {
|
||||||
this.isInvalid = true;
|
this.isInvalid = true;
|
||||||
@@ -53,16 +48,25 @@ export default defineStore('loginStore', {
|
|||||||
*/
|
*/
|
||||||
async refreshTokenLogin() {
|
async refreshTokenLogin() {
|
||||||
const api = '/api/oauth/token';
|
const api = '/api/oauth/token';
|
||||||
this.auth.grant_type = 'refresh_token';
|
const refreshToken = document.cookie.replace(/(?:(?:^|.*;\s*)luciaRefreshToken\s*\=\s*([^;]*).*$)|^.*$/, "$1");
|
||||||
this.auth.refresh_token = this.refreshToken;
|
console.log(document.cookie);
|
||||||
|
|
||||||
try {
|
|
||||||
const response = await axios.post(api, this.auth, config);
|
this.auth.grant_type = 'refresh_token';
|
||||||
const newAccessToken = response.data.access_token;
|
this.auth.refresh_token = refreshToken;
|
||||||
document.cookie = `luciaToken=${newAccessToken};`;
|
|
||||||
} catch(error) {
|
// try {
|
||||||
this.$router.push('/login');
|
// const response = await axios.post(api, this.auth, config);
|
||||||
}
|
// const newAccessToken = response.data.access_token;
|
||||||
|
// const newRefreshToken = response.data.refresh_token;
|
||||||
|
|
||||||
|
// document.cookie = `luciaToken=${newAccessToken}`;
|
||||||
|
// document.cookie = `luciaRefreshToken=${newRefreshToken};expires=${this.expired}`;
|
||||||
|
|
||||||
|
// defaults.headers.common['Authorization'] = `Bearer ${newAccessToken}`;
|
||||||
|
// } catch(error) {
|
||||||
|
// this.$router.push('/login');
|
||||||
|
// }
|
||||||
},
|
},
|
||||||
/**
|
/**
|
||||||
* Logout, tooken expired
|
* Logout, tooken expired
|
||||||
|
|||||||
@@ -97,6 +97,7 @@
|
|||||||
|
|
||||||
<script>
|
<script>
|
||||||
import { storeToRefs } from 'pinia';
|
import { storeToRefs } from 'pinia';
|
||||||
|
import LoginStore from '@/stores/login.js';
|
||||||
import filesStore from '@/stores/files.js';
|
import filesStore from '@/stores/files.js';
|
||||||
import AllMapDataStore from '@/stores/allMapData.js';
|
import AllMapDataStore from '@/stores/allMapData.js';
|
||||||
import LoadingStore from '@/stores/loading.js';
|
import LoadingStore from '@/stores/loading.js';
|
||||||
@@ -143,6 +144,7 @@
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
setup() {
|
setup() {
|
||||||
|
const loginStore = LoginStore();
|
||||||
const store = filesStore();
|
const store = filesStore();
|
||||||
const allMapDataStore = AllMapDataStore();
|
const allMapDataStore = AllMapDataStore();
|
||||||
const loadingStore = LoadingStore();
|
const loadingStore = LoadingStore();
|
||||||
@@ -150,7 +152,7 @@
|
|||||||
const { createFilterId, baseLogId } = storeToRefs(allMapDataStore);
|
const { createFilterId, baseLogId } = storeToRefs(allMapDataStore);
|
||||||
const { isLoading } = storeToRefs(loadingStore);
|
const { isLoading } = storeToRefs(loadingStore);
|
||||||
|
|
||||||
return { store, dependentsData, allMapDataStore, createFilterId, baseLogId, isLoading }
|
return { loginStore, store, dependentsData, allMapDataStore, createFilterId, baseLogId, isLoading }
|
||||||
},
|
},
|
||||||
components: {
|
components: {
|
||||||
IconDataFormat,
|
IconDataFormat,
|
||||||
|
|||||||
Reference in New Issue
Block a user