fixed refresh token
This commit is contained in:
parent
a7c53edf79
commit
6ccdd0a003
2 changed files with 27 additions and 8 deletions
|
|
@ -29,21 +29,35 @@ async function refreshAccessToken() {
|
|||
if (isRefreshing) return refreshPromise;
|
||||
|
||||
isRefreshing = true;
|
||||
refreshPromise = fetch(`${BASE_URL}/user/auth/refresh`, {
|
||||
refreshPromise = fetch(`${BASE_URL}/auth/refresh`, {
|
||||
method: 'POST',
|
||||
credentials: 'include',
|
||||
})
|
||||
|
||||
.then(async (res) => {
|
||||
if (!res.ok) throw new Error('Failed to refresh access token');
|
||||
return res.json();
|
||||
})
|
||||
.then((data) => {
|
||||
authStore.setToken(data.accessToken);
|
||||
const data = await res.json();
|
||||
console.log("Refresh response data: ", data);
|
||||
return data;
|
||||
})
|
||||
|
||||
.then((data) => {
|
||||
const token = data.access_token
|
||||
|
||||
if (!token) {
|
||||
throw new Error('No access_token in refresh response');
|
||||
}
|
||||
|
||||
authStore.setToken(data.access_token);
|
||||
console.log('refreshed token', data.access_token);
|
||||
|
||||
return data;
|
||||
})
|
||||
|
||||
.catch((error) => {
|
||||
throw error;
|
||||
})
|
||||
|
||||
.finally(() => {
|
||||
isRefreshing = false;
|
||||
refreshPromise = null;
|
||||
|
|
@ -61,11 +75,16 @@ async function request(url, options = {}, isRetry = false) {
|
|||
try {
|
||||
const data = await refreshAccessToken();
|
||||
|
||||
const token = data.access_token;
|
||||
if (!token) {
|
||||
throw new Error('Refresh response did not contain access_token');
|
||||
}
|
||||
|
||||
const newOptions = {
|
||||
...options,
|
||||
headers: {
|
||||
...options.headers,
|
||||
'Authorization': `Bearer ${data.accessToken}`,
|
||||
'Authorization': `Bearer ${token}`,
|
||||
},
|
||||
};
|
||||
return await request(url, newOptions, true);
|
||||
|
|
|
|||
|
|
@ -23,7 +23,7 @@ export const useAuthStore = defineStore('auth', () => {
|
|||
|
||||
const login = async (email, password) => {
|
||||
try {
|
||||
const response = await apiClient.post('/user/auth/login', { email, password })
|
||||
const response = await apiClient.post('/auth/login', { email, password })
|
||||
const { access_token, user: userData } = response
|
||||
|
||||
setToken(access_token)
|
||||
|
|
@ -39,7 +39,7 @@ export const useAuthStore = defineStore('auth', () => {
|
|||
setToken(null)
|
||||
user.value = null
|
||||
try {
|
||||
await apiClient.post('/user/auth/logout')
|
||||
await apiClient.post('/auth/logout')
|
||||
} catch (error) {
|
||||
console.error('Logout error:', error)
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue