2012b0bab9
- Added default_language field to general settings state in SettingsPage - Replaced LanguageSelector component with simple select dropdown on settings page - Fixed public settings endpoint to read general_default_language from database - Language setting now properly saved when clicking Save Settings button - Setting is correctly used by gallery login page and legal pages 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com>
84 lines
2.3 KiB
TypeScript
84 lines
2.3 KiB
TypeScript
import axios from 'axios';
|
|
import Cookies from 'js-cookie';
|
|
|
|
// Cookie keys
|
|
export const ADMIN_TOKEN_KEY = 'admin_token';
|
|
export const GALLERY_TOKEN_KEY = 'gallery_token';
|
|
|
|
// Create axios instance
|
|
export const api = axios.create({
|
|
baseURL: import.meta.env.VITE_API_URL || 'http://localhost:3001',
|
|
headers: {
|
|
'Content-Type': 'application/json',
|
|
},
|
|
});
|
|
|
|
// Request interceptor to add auth token
|
|
api.interceptors.request.use(
|
|
(config) => {
|
|
// Check if it's an admin route or gallery route
|
|
const isAdminRoute = config.url?.includes('/admin');
|
|
const token = isAdminRoute
|
|
? Cookies.get(ADMIN_TOKEN_KEY)
|
|
: Cookies.get(GALLERY_TOKEN_KEY);
|
|
|
|
if (token) {
|
|
config.headers.Authorization = `Bearer ${token}`;
|
|
}
|
|
|
|
// Don't set Content-Type for FormData - let browser set it with boundary
|
|
if (config.data instanceof FormData) {
|
|
delete config.headers['Content-Type'];
|
|
}
|
|
|
|
return config;
|
|
},
|
|
(error) => {
|
|
return Promise.reject(error);
|
|
}
|
|
);
|
|
|
|
// Response interceptor to handle errors
|
|
api.interceptors.response.use(
|
|
(response) => response,
|
|
(error) => {
|
|
if (error.response?.status === 401) {
|
|
// Clear tokens on unauthorized
|
|
Cookies.remove(ADMIN_TOKEN_KEY);
|
|
Cookies.remove(GALLERY_TOKEN_KEY);
|
|
|
|
// Redirect to appropriate login
|
|
const isAdminRoute = error.config?.url?.includes('/admin');
|
|
if (isAdminRoute) {
|
|
window.location.href = '/admin/login';
|
|
} else {
|
|
// For gallery routes, redirect to the gallery password page
|
|
const currentPath = window.location.pathname;
|
|
const gallerySlug = currentPath.split('/')[2];
|
|
if (gallerySlug) {
|
|
window.location.href = `/gallery/${gallerySlug}`;
|
|
}
|
|
}
|
|
}
|
|
|
|
return Promise.reject(error);
|
|
}
|
|
);
|
|
|
|
// Helper to set auth tokens
|
|
export const setAuthToken = (token: string, isAdmin: boolean = false) => {
|
|
const key = isAdmin ? ADMIN_TOKEN_KEY : GALLERY_TOKEN_KEY;
|
|
Cookies.set(key, token, { expires: 1 }); // 1 day expiry
|
|
};
|
|
|
|
// Helper to clear auth tokens
|
|
export const clearAuthToken = (isAdmin: boolean = false) => {
|
|
const key = isAdmin ? ADMIN_TOKEN_KEY : GALLERY_TOKEN_KEY;
|
|
Cookies.remove(key);
|
|
};
|
|
|
|
// Helper to get auth tokens
|
|
export const getAuthToken = (isAdmin: boolean = false) => {
|
|
const key = isAdmin ? ADMIN_TOKEN_KEY : GALLERY_TOKEN_KEY;
|
|
return Cookies.get(key);
|
|
}; |