register + push to sign in on success
This commit is contained in:
parent
ba4d769591
commit
8934f7b219
3 changed files with 79 additions and 64 deletions
|
|
@ -1,83 +1,81 @@
|
|||
import { useAuthStore } from '@/stores/authStore.js';
|
||||
import { useAuthStore } from '@/stores/authStore.js'
|
||||
|
||||
const BASE_URL = 'http://localhost:9000/api/v2';
|
||||
const BASE_URL = 'http://localhost:9000/api/v2'
|
||||
|
||||
let isRefreshing = false;
|
||||
let refreshPromise = null;
|
||||
let isRefreshing = false
|
||||
let refreshPromise = null
|
||||
|
||||
function createConfig(options = {}) {
|
||||
const authStore = useAuthStore();
|
||||
const authStore = useAuthStore()
|
||||
const headers = {
|
||||
'Content-Type': 'application/json',
|
||||
...options.headers,
|
||||
};
|
||||
}
|
||||
|
||||
if (authStore.accessToken) {
|
||||
headers['Authorization'] = `Bearer ${authStore.accessToken}`;
|
||||
headers['Authorization'] = `Bearer ${authStore.accessToken}`
|
||||
}
|
||||
|
||||
return {
|
||||
headers,
|
||||
credentials: 'include',
|
||||
...options,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
async function refreshAccessToken() {
|
||||
const authStore = useAuthStore();
|
||||
const authStore = useAuthStore()
|
||||
|
||||
if (isRefreshing) return refreshPromise;
|
||||
if (isRefreshing) return refreshPromise
|
||||
|
||||
isRefreshing = true;
|
||||
isRefreshing = true
|
||||
refreshPromise = fetch(`${BASE_URL}/user/auth/refresh`, {
|
||||
method: 'POST',
|
||||
credentials: 'include',
|
||||
})
|
||||
|
||||
.then(async (res) => {
|
||||
if (!res.ok) throw new Error('Failed to refresh access token');
|
||||
const data = await res.json();
|
||||
console.log("Refresh response data: ", data);
|
||||
return data;
|
||||
if (!res.ok) throw new Error('Failed to refresh access token')
|
||||
return await res.json()
|
||||
})
|
||||
|
||||
.then((data) => {
|
||||
const token = data.access_token
|
||||
|
||||
if (!token) {
|
||||
throw new Error('No access_token in refresh response');
|
||||
throw new Error('No access_token in refresh response')
|
||||
}
|
||||
|
||||
authStore.setToken(data.access_token);
|
||||
console.log('refreshed token', data.access_token);
|
||||
authStore.setToken(data.access_token)
|
||||
|
||||
return data;
|
||||
|
||||
return data
|
||||
})
|
||||
|
||||
.catch((error) => {
|
||||
throw error;
|
||||
throw error
|
||||
})
|
||||
|
||||
.finally(() => {
|
||||
isRefreshing = false;
|
||||
refreshPromise = null;
|
||||
});
|
||||
isRefreshing = false
|
||||
refreshPromise = null
|
||||
})
|
||||
|
||||
return refreshPromise;
|
||||
return refreshPromise
|
||||
}
|
||||
|
||||
async function request(url, options = {}, isRetry = false) {
|
||||
const config = createConfig(options);
|
||||
const config = createConfig(options)
|
||||
|
||||
const response = await fetch(`${BASE_URL}${url}`, config);
|
||||
const response = await fetch(`${BASE_URL}${url}`, config)
|
||||
|
||||
if (response.status === 401 && !isRetry) {
|
||||
try {
|
||||
const data = await refreshAccessToken();
|
||||
const data = await refreshAccessToken()
|
||||
|
||||
const token = data.access_token;
|
||||
const token = data.access_token
|
||||
if (!token) {
|
||||
throw new Error('Refresh response did not contain access_token');
|
||||
throw new Error('Refresh response did not contain access_token')
|
||||
}
|
||||
|
||||
const newOptions = {
|
||||
|
|
@ -86,36 +84,40 @@ async function request(url, options = {}, isRetry = false) {
|
|||
...options.headers,
|
||||
'Authorization': `Bearer ${token}`,
|
||||
},
|
||||
};
|
||||
return await request(url, newOptions, true);
|
||||
}
|
||||
return await request(url, newOptions, true)
|
||||
} catch (e) {
|
||||
const authStore = useAuthStore();
|
||||
authStore.forceLogout();
|
||||
throw e;
|
||||
const authStore = useAuthStore()
|
||||
authStore.forceLogout()
|
||||
throw e
|
||||
}
|
||||
}
|
||||
|
||||
if (!response.ok) {
|
||||
let errorData;
|
||||
let errorData
|
||||
try {
|
||||
errorData = await response.json();
|
||||
errorData = await response.json()
|
||||
} catch {
|
||||
errorData = {};
|
||||
errorData = {}
|
||||
}
|
||||
throw new Error(errorData.message || `HTTP Error: ${response.status}`);
|
||||
throw new Error(errorData.message || `HTTP Error: ${response.status}`)
|
||||
}
|
||||
|
||||
const contentType = response.headers.get('content-type');
|
||||
let data = null
|
||||
const contentType = response.headers.get('content-type')
|
||||
if (contentType?.includes('application/json')) {
|
||||
try {
|
||||
return await response.json();
|
||||
data = await response.json()
|
||||
} catch (e) {
|
||||
console.warn('Failed to parse JSON response', e);
|
||||
return null;
|
||||
console.warn('Failed to parse JSON response', e)
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
return {
|
||||
status: response.status,
|
||||
ok: response.ok,
|
||||
data,
|
||||
}
|
||||
}
|
||||
|
||||
export const apiClient = {
|
||||
|
|
@ -129,4 +131,4 @@ export const apiClient = {
|
|||
body: JSON.stringify(data),
|
||||
}),
|
||||
delete: (url) => request(url, { method: 'DELETE' }),
|
||||
};
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue