bugfix: organization not selected problem fixed. being logged out too often fixed.

This commit is contained in:
2026-07-12 17:51:31 +03:30
parent 39935688ad
commit 901d838a2c
14 changed files with 490 additions and 22 deletions

View File

@@ -60,9 +60,12 @@ export const authApi = {
return response.data;
},
// Refresh token
refreshToken: async (refreshToken: string): Promise<{ accessToken: string }> => {
const response = await apiClient.post('/auth/refresh', { refreshToken });
// Refresh access token using httpOnly cookies (same as the axios interceptor).
refreshSessionFromCookies: async (): Promise<{
success: boolean;
data?: { accessToken?: string; accessTokenExpiresAt?: string };
}> => {
const response = await apiClient.post('/auth/refresh', {});
return response.data;
},

View File

@@ -30,7 +30,6 @@ function isPublicInvitationRequest(url: string | undefined): boolean {
function shouldSkipRefreshRetry(url: string | undefined): boolean {
if (!url) return false;
return (
url.includes('/auth/profile') ||
url.includes('/auth/refresh') ||
url.includes('/auth/login') ||
url.includes('/auth/register') ||
@@ -57,13 +56,29 @@ apiClient.interceptors.response.use(
originalRequest._retry = true;
try {
// ✅ refresh via cookie (no body needed ideally)
// Refresh access token, then restore selected organization context on the new JWT.
await axios.post(
`${process.env.NEXT_PUBLIC_API_URL}/auth/refresh`,
{},
{ withCredentials: true }
{ withCredentials: true },
);
const orgId =
typeof window !== 'undefined'
? localStorage.getItem('currentOrganizationId')
: null;
if (orgId) {
try {
await axios.post(
`${process.env.NEXT_PUBLIC_API_URL}/auth/select-organization`,
{ organizationId: orgId },
{ withCredentials: true },
);
} catch {
/* original retry may still succeed if refresh preserved organizationId */
}
}
return apiClient(originalRequest);
} catch (refreshError) {
if (typeof window !== 'undefined') {