Files
picpeak/frontend/src/hooks/useGallery.ts
T
paul 21b1e79672
Test and Lint / backend-test (push) Successful in 1m11s
continuous-integration/drone/push Build is passing
Test and Lint / frontend-test (push) Successful in 2m6s
Version and Release / version-bump (push) Successful in 35s
Version and Release / trigger-drone (push) Successful in 3s
fix: resolve gallery login redirect issue
- Updated API interceptor to better handle gallery authentication
- Fixed 401 error handling to prevent redirect loops on gallery pages
- Improved token extraction logic for gallery API requests
- Consolidated duplicate verifyGalleryAccess middleware
- Added proper error handling in GalleryView component
- Gallery authentication now properly distinguishes from admin routes

The issue was caused by the API interceptor redirecting to admin login
when gallery API calls failed with 401, even when users were already
on gallery pages attempting to authenticate.

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-07-15 08:35:33 +02:00

66 lines
1.7 KiB
TypeScript

import { useQuery, useMutation } from '@tanstack/react-query';
import { galleryService } from '../services';
import { toast } from 'react-toastify';
export const useGalleryInfo = (slug: string, token?: string) => {
return useQuery({
queryKey: ['gallery-info', slug, token],
queryFn: () => galleryService.getGalleryInfo(slug, token),
retry: 1,
staleTime: 5 * 60 * 1000, // 5 minutes
});
};
export const useGalleryPhotos = (slug: string, enabled: boolean = true) => {
return useQuery({
queryKey: ['gallery-photos', slug],
queryFn: () => galleryService.getGalleryPhotos(slug),
enabled,
retry: 1,
staleTime: 5 * 60 * 1000, // 5 minutes
// Add a small delay to ensure auth token is properly set
retryDelay: 100,
});
};
export const useGalleryStats = (slug: string, enabled: boolean = true) => {
return useQuery({
queryKey: ['gallery-stats', slug],
queryFn: () => galleryService.getGalleryStats(slug),
enabled,
retry: 1,
staleTime: 60 * 1000, // 1 minute
});
};
export const useDownloadPhoto = () => {
return useMutation({
mutationFn: ({
slug,
photoId,
filename,
}: {
slug: string;
photoId: number;
filename: string;
}) => galleryService.downloadPhoto(slug, photoId, filename),
onSuccess: () => {
toast.success('Photo downloaded successfully');
},
onError: () => {
toast.error('Failed to download photo');
},
});
};
export const useDownloadAllPhotos = () => {
return useMutation({
mutationFn: (slug: string) => galleryService.downloadAllPhotos(slug),
onSuccess: () => {
toast.success('Download started');
},
onError: () => {
toast.error('Failed to download photos');
},
});
};