Migracao Logto + Supabase - backend e frontend atualizados para nova autenticação
This commit is contained in:
@@ -1,9 +1,9 @@
|
||||
// API service configuration v1.4 - with auth and error interceptors
|
||||
// API service configuration v2.0 - Logto Auth
|
||||
import axios from 'axios';
|
||||
import { triggerGuestWarning } from '../utils/toastHandler';
|
||||
import { getToken } from '../main';
|
||||
|
||||
export const getBaseUrl = () => {
|
||||
// Priority: Env var -> Relative path (handled by Vite proxy in dev, or Nginx/Vercel in prod)
|
||||
if (import.meta.env.VITE_API_URL) {
|
||||
return import.meta.env.VITE_API_URL;
|
||||
}
|
||||
@@ -17,41 +17,26 @@ const api = axios.create({
|
||||
},
|
||||
});
|
||||
|
||||
// Store the current user's clerk ID and Organization ID/Name
|
||||
let currentClerkUserId: string | null = null;
|
||||
let currentOrgId: string | null = null;
|
||||
let currentOrgName: string | null = null;
|
||||
|
||||
// Function to set the clerk user ID (called from AuthContext)
|
||||
export const setApiClerkUserId = (clerkId: string | null) => {
|
||||
currentClerkUserId = clerkId;
|
||||
};
|
||||
|
||||
// Function to set the organization ID and Name (called from Layout/Context)
|
||||
export const setApiOrgData = (orgId: string | null, orgName: string | null = null) => {
|
||||
currentOrgId = orgId;
|
||||
currentOrgName = orgName;
|
||||
};
|
||||
|
||||
// Legacy support
|
||||
export const setApiOrgId = (orgId: string | null) => {
|
||||
setApiOrgData(orgId, null);
|
||||
};
|
||||
export const setApiOrganizationId = setApiOrgData;
|
||||
|
||||
// Alias for consistency
|
||||
export const setApiOrganizationId = setApiOrgId;
|
||||
|
||||
// Request interceptor to add clerk user ID and Org ID headers
|
||||
api.interceptors.request.use(
|
||||
(config) => {
|
||||
if (currentClerkUserId) {
|
||||
config.headers['x-clerk-user-id'] = currentClerkUserId;
|
||||
const token = getToken();
|
||||
if (token) {
|
||||
config.headers['Authorization'] = `Bearer ${token}`;
|
||||
}
|
||||
if (currentOrgId) {
|
||||
config.headers['x-organization-id'] = currentOrgId;
|
||||
}
|
||||
if (currentOrgName) {
|
||||
// Encode to handle special characters
|
||||
config.headers['x-organization-name'] = encodeURIComponent(currentOrgName);
|
||||
}
|
||||
return config;
|
||||
@@ -61,12 +46,10 @@ api.interceptors.request.use(
|
||||
}
|
||||
);
|
||||
|
||||
// Response interceptor to handle 403 errors (guest access denied)
|
||||
api.interceptors.response.use(
|
||||
(response) => response,
|
||||
(error) => {
|
||||
if (error.response?.status === 403) {
|
||||
// Check if it's a guest permission error
|
||||
const errorMessage = error.response?.data?.error || '';
|
||||
if (errorMessage.includes('Convidados') || errorMessage.includes('guest') || errorMessage.includes('permissão')) {
|
||||
triggerGuestWarning();
|
||||
|
||||
Reference in New Issue
Block a user