feat: Refresh Token in progress.

This commit is contained in:
chiayin
2024-01-16 11:07:59 +08:00
parent 9753ed535d
commit 02deb591a1

View File

@@ -7,10 +7,12 @@ export default defineStore('loginStore', {
// state, actions, getters // state, actions, getters
state: () => ({ state: () => ({
auth: { auth: {
grant_type: 'password', grant_type: 'password', // password | refresh_token
username: '', username: '',
password: '', password: '',
refresh_token: ''
}, },
refreshToken: '',
isInvalid: false, isInvalid: false,
userData: {}, userData: {},
}), }),
@@ -29,9 +31,11 @@ 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 token = response.data.access_token; const accessToken = response.data.access_token;
this.refreshToken = response.data.refresh_token;
// 將 token 儲存在 cookie // 將 token 儲存在 cookie
document.cookie = `luciaToken=${token};`; document.cookie = `luciaToken=${accessToken};`;
// // 取得當前日期 // // 取得當前日期
// const currentDate = new Date(); // const currentDate = new Date();
// // 設定 cookie 的過期日期為一天後 // // 設定 cookie 的過期日期為一天後
@@ -44,6 +48,22 @@ export default defineStore('loginStore', {
this.isInvalid = true; this.isInvalid = true;
}; };
}, },
/**
* Refresh Token
*/
async refreshTokenLogin() {
const api = '/api/oauth/token';
this.auth.grant_type = 'refresh_token';
this.auth.refresh_token = this.refreshToken;
try {
const response = await axios.post(api, this.auth, config);
const newAccessToken = response.data.access_token;
document.cookie = `luciaToken=${newAccessToken};`;
} catch(error) {
this.$router.push('/login');
}
},
/** /**
* Logout, tooken expired * Logout, tooken expired
*/ */