diff --git a/src/stores/login.ts b/src/stores/login.ts index dbd1ee6..1a5cef8 100644 --- a/src/stores/login.ts +++ b/src/stores/login.ts @@ -61,6 +61,11 @@ export default defineStore('loginStore', { */ async refreshToken() { const api = '/api/oauth/token'; + const config = { + headers: { + 'Content-Type':'application/x-www-form-urlencoded', + }, + }; this.auth.grant_type = 'refresh_token'; this.auth.refresh_token = getCookie("luciaRefreshToken"); @@ -70,15 +75,16 @@ export default defineStore('loginStore', { 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}`; - } + document.cookie = `luciaRefreshToken=${newRefreshToken};expires=${new Date(this.expired)}`; + + axios.defaults.headers.common['Authorization'] = `Bearer ${newAccessToken}`; + } } catch(error) { // 若refresh token 失敗則導向至登入頁面 this.$router.push('/login'); + throw error; } }, /** diff --git a/tests/stores/login.test.js b/tests/stores/login.test.js index 2055336..e806dc3 100644 --- a/tests/stores/login.test.js +++ b/tests/stores/login.test.js @@ -144,6 +144,47 @@ describe('loginStore', () => { expect(store.isLoggedIn).toBe(true); }); + describe('refreshToken', () => { + it('sends request with correct config and updates tokens on success', async () => { + document.cookie = 'luciaRefreshToken=old-refresh-token'; + + axios.post.mockResolvedValue({ + status: 200, + data: { + access_token: 'new-access-token', + refresh_token: 'new-refresh-token', + }, + }); + + await store.refreshToken(); + + // Should call with content-type header (config must be defined) + expect(axios.post).toHaveBeenCalledWith( + '/api/oauth/token', + expect.objectContaining({ + grant_type: 'refresh_token', + refresh_token: 'old-refresh-token', + }), + expect.objectContaining({ + headers: { 'Content-Type': 'application/x-www-form-urlencoded' }, + }), + ); + + // Should update axios default Authorization header + expect(axios.defaults.headers.common['Authorization']) + .toBe('Bearer new-access-token'); + }); + + it('redirects to login and re-throws on failure', async () => { + document.cookie = 'luciaRefreshToken=old-refresh-token'; + axios.post.mockRejectedValue(new Error('401')); + + await expect(store.refreshToken()).rejects.toThrow('401'); + + expect(store.$router.push).toHaveBeenCalledWith('/login'); + }); + }); + describe('expired', () => { it('is approximately 6 months in the future', () => { const now = new Date();