feat: Implement authentication checks for profile actions and user layout, and add presigned URL fetching for file viewing.

This commit is contained in:
pradeepkumar
2026-01-29 00:02:59 +05:30
parent 4ffd6305b1
commit b41bd8a3eb
9 changed files with 243 additions and 40 deletions

View File

@@ -62,6 +62,19 @@ api.interceptors.request.use(
}
);
// Public API endpoints that don't require authentication
const publicEndpoints = [
'/agents',
'/agent-types',
'/profiles',
];
// Check if URL is a public endpoint
const isPublicEndpoint = (url?: string): boolean => {
if (!url) return false;
return publicEndpoints.some(endpoint => url.includes(endpoint));
};
// Response interceptor with token refresh
api.interceptors.response.use(
(response) => {
@@ -72,6 +85,17 @@ api.interceptors.response.use(
// Handle 401 Unauthorized
if (error.response?.status === 401 && !originalRequest._retry) {
// For public endpoints, just reject the error without redirecting to signout
// The request will work without auth anyway
if (isPublicEndpoint(originalRequest.url)) {
// Remove the auth header and retry once
if (originalRequest.headers) {
delete originalRequest.headers.Authorization;
}
originalRequest._retry = true;
return api(originalRequest);
}
// Don't try to refresh if this was the refresh token request itself
if (originalRequest.url?.includes('/auth/refresh')) {
// Refresh token is also invalid, clear everything and sign out properly
@@ -105,13 +129,11 @@ api.interceptors.response.use(
const refreshToken = typeof window !== 'undefined' ? localStorage.getItem('refreshToken') : null;
if (!refreshToken) {
// No refresh token available, sign out properly
// No refresh token available, just clear tokens but don't redirect for better UX
if (typeof window !== 'undefined') {
localStorage.removeItem('accessToken');
localStorage.removeItem('refreshToken');
localStorage.removeItem('user');
// Use NextAuth's signout endpoint to properly clear the session
window.location.href = '/api/auth/signout?callbackUrl=/login';
}
isRefreshing = false;
return Promise.reject(error);
@@ -145,13 +167,11 @@ api.interceptors.response.use(
// Retry original request
return api(originalRequest);
} catch (refreshError) {
// Refresh failed, clear tokens and sign out properly
// Refresh failed, clear tokens but don't redirect
if (typeof window !== 'undefined') {
localStorage.removeItem('accessToken');
localStorage.removeItem('refreshToken');
localStorage.removeItem('user');
// Use NextAuth's signout endpoint to properly clear the session
window.location.href = '/api/auth/signout?callbackUrl=/login';
}
processQueue(refreshError as AxiosError);
isRefreshing = false;

View File

@@ -8,6 +8,7 @@ export interface RegisterRequest {
email: string;
password: string;
role: 'USER' | 'AGENT';
agentTypeId?: string;
}
export interface LoginRequest {