feature: refresh token. if not logged in then refresh token; else redirect to login page.

This commit is contained in:
Cindy Chang
2024-06-28 12:10:19 +08:00
parent 9b2bab9124
commit a4aab21b98
5 changed files with 84 additions and 50 deletions

View File

@@ -1,7 +1,7 @@
import { defineStore } from "pinia";
import axios from 'axios';
import apiError from '@/module/apiError.js';
import { deleteCookie, setCookie } from "../utils/cookieUtil";
import { deleteCookie, setCookie, getCookie } from "../utils/cookieUtil";
export default defineStore('loginStore', {
// data, methods, computed
@@ -11,13 +11,13 @@ export default defineStore('loginStore', {
grant_type: 'password', // password | refresh_token
username: '',
password: '',
refresh_token: ''
refresh_token: undefined,
},
isInvalid: false,
userData: {},
isLoggedIn: false,
rememberedReturnToUrl: "",
// expired: new Date().setMonth(6), // 設定 Refresh Token 的到期日為半年後
expired: new Date().setMonth(6), // 設定 Refresh Token 的到期日為半年後
}),
actions: {
/**
@@ -35,10 +35,10 @@ export default defineStore('loginStore', {
try {
const response = await axios.post(api, this.auth, config);
const accessToken = response.data.access_token;
const refreshToken = response.data.refresh_token;
const refresh_token = response.data.refresh_token;
// 將 token 儲存在 cookie
document.cookie = `luciaToken=${accessToken}`;
// document.cookie = `luciaRefreshToken=${refreshToken};expires=${new Date(this.expired)};`;
document.cookie = `luciaRefreshToken=${refresh_token};expires=${new Date(this.expired)};`;
this.isLoggedIn = true;
setCookie("isLuciaLoggedIn", "true");
@@ -57,27 +57,31 @@ export default defineStore('loginStore', {
};
},
/**
* Refresh Token (暫時沒做)
* Refresh Token
*/
async refreshTokenLogin() {
async refreshToken() {
console.log('TODO:TODO:', this.auth);
const api = '/api/oauth/token';
const refreshToken = document.cookie.replace(/(?:(?:^|.*;\s*)luciaRefreshToken\s*\=\s*([^;]*).*$)|^.*$/, "$1");
this.auth.grant_type = 'refresh_token';
this.auth.refresh_token = refreshToken;
this.auth.refresh_token = getCookie("luciaRefreshToken");
// try {
// 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');
// }
try {
const response = await axios.post(api, this.auth, config);
console.log('response', response);
if(response.status === 200) {
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) {
// 若refresh token 失敗則導向至登入頁面
this.$router.push('/login');
}
},
/**
* Logout, tooken expired