Minimax correcao
This commit is contained in:
@@ -17,8 +17,6 @@ import { DeveloperDashboard } from './pages/DeveloperDashboard';
|
||||
import { CalculatorDashboard } from './pages/CalculatorDashboard';
|
||||
import { StockDashboard } from './pages/StockDashboard';
|
||||
import { GuestDashboard } from './pages/GuestDashboard';
|
||||
import { Login } from './pages/Login';
|
||||
import { Callback } from './pages/Callback';
|
||||
import InstrumentList from './pages/InstrumentList';
|
||||
|
||||
const DeveloperRoute: React.FC<{ children: React.ReactNode }> = ({ children }) => {
|
||||
@@ -39,8 +37,6 @@ const AppContent: React.FC = () => {
|
||||
<Layout>
|
||||
<Routes>
|
||||
<Route path="/" element={<ProjectList />} />
|
||||
<Route path="/login" element={<Login />} />
|
||||
<Route path="/callback" element={<Callback />} />
|
||||
<Route path="/guest-dashboard" element={<GuestDashboard />} />
|
||||
<Route path="/projects" element={<ProjectList />} />
|
||||
<Route path="/project/:id" element={<ProjectDetails />} />
|
||||
|
||||
@@ -2,9 +2,8 @@ import React, { useState } from 'react';
|
||||
import NotificationBell from './NotificationBell';
|
||||
import { TeamPresence } from './TeamPresence';
|
||||
import { Link, useLocation, useNavigate } from 'react-router-dom';
|
||||
import { Menu, X, FolderOpen, Layers, ClipboardCheck, LogOut, TrendingUp, Sun, Moon, HelpCircle, Shield, Wrench, Terminal, LayoutDashboard, Package, Thermometer, User } from 'lucide-react';
|
||||
import { Menu, X, FolderOpen, Layers, ClipboardCheck, TrendingUp, Sun, Moon, HelpCircle, Shield, Wrench, Terminal, LayoutDashboard, Package, Thermometer, User } from 'lucide-react';
|
||||
import { clsx } from 'clsx';
|
||||
import { useLogto } from '@logto/react';
|
||||
import { TechnicalManual } from './TechnicalManual';
|
||||
import { useAuth } from '../context/useAuth';
|
||||
|
||||
@@ -20,8 +19,7 @@ export const Layout: React.FC<LayoutProps> = ({ children }) => {
|
||||
return saved === 'dark' || (!saved && window.matchMedia('(prefers-color-scheme: dark)').matches);
|
||||
});
|
||||
const location = useLocation();
|
||||
const { signOut } = useLogto();
|
||||
const { isAdmin, isUser, isDeveloper, appUser, isSignedIn } = useAuth();
|
||||
const { isAdmin, isUser, isDeveloper, appUser } = useAuth();
|
||||
|
||||
// Helper to get role display name
|
||||
const getRoleDisplay = () => {
|
||||
@@ -54,14 +52,6 @@ export const Layout: React.FC<LayoutProps> = ({ children }) => {
|
||||
}
|
||||
}, [appUser, location.pathname, navigate]);
|
||||
|
||||
// Redirect to login if not signed in (except for login and callback pages)
|
||||
React.useEffect(() => {
|
||||
const publicPaths = ['/login', '/callback'];
|
||||
if (!isSignedIn && !publicPaths.includes(location.pathname)) {
|
||||
navigate('/login');
|
||||
}
|
||||
}, [isSignedIn, location.pathname, navigate]);
|
||||
|
||||
interface NavItem {
|
||||
icon: React.ElementType;
|
||||
label: string;
|
||||
@@ -87,10 +77,6 @@ export const Layout: React.FC<LayoutProps> = ({ children }) => {
|
||||
return false;
|
||||
};
|
||||
|
||||
const handleLogout = () => {
|
||||
signOut(window.location.origin);
|
||||
};
|
||||
|
||||
if (location.pathname === '/login' || location.pathname === '/callback') {
|
||||
return <>{children}</>;
|
||||
}
|
||||
@@ -216,14 +202,6 @@ export const Layout: React.FC<LayoutProps> = ({ children }) => {
|
||||
)}
|
||||
</button>
|
||||
|
||||
<button
|
||||
onClick={handleLogout}
|
||||
className="flex items-center gap-3 px-4 py-3 rounded-xl text-sm font-semibold text-text-secondary hover:text-error hover:bg-error/5 transition-all w-full"
|
||||
>
|
||||
<LogOut size={18} />
|
||||
Sair
|
||||
</button>
|
||||
|
||||
<div className="pt-2 flex items-center justify-between px-2">
|
||||
<div className="flex items-center gap-2">
|
||||
<NotificationBell />
|
||||
@@ -348,13 +326,6 @@ export const Layout: React.FC<LayoutProps> = ({ children }) => {
|
||||
{isDarkMode ? <Sun size={20} className="text-yellow-500" /> : <Moon size={20} className="text-primary" />}
|
||||
{isDarkMode ? 'Modo Claro' : 'Modo Escuro'}
|
||||
</button>
|
||||
<button
|
||||
onClick={handleLogout}
|
||||
className="flex items-center gap-4 w-full text-text-main font-bold"
|
||||
>
|
||||
<LogOut size={20} />
|
||||
Sair
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
|
||||
@@ -23,21 +23,7 @@ export const ProtectedRoute: React.FC<ProtectedRouteProps> = ({
|
||||
requireEdit = false,
|
||||
redirectTo = '/',
|
||||
}) => {
|
||||
const { appUser, isLoading, canEdit, isSignedIn } = useAuth();
|
||||
|
||||
// Show loading state
|
||||
if (isLoading) {
|
||||
return (
|
||||
<div className="flex items-center justify-center min-h-[60vh]">
|
||||
<RefreshCw size={32} className="animate-spin text-primary" />
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
// Check authentication
|
||||
if (!isSignedIn) {
|
||||
return <Navigate to="/login" replace />;
|
||||
}
|
||||
const { appUser, canEdit } = useAuth();
|
||||
|
||||
// Check role-based access
|
||||
if (allowedRoles && appUser && !allowedRoles.includes(appUser.role)) {
|
||||
|
||||
@@ -1,124 +1,57 @@
|
||||
import React, { useState, useEffect, useCallback, useMemo } from 'react';
|
||||
import { useLogto } from '@logto/react';
|
||||
import type { AppUser } from '../types';
|
||||
import type { AppUser, UserRole } from '../types';
|
||||
import { AuthContext } from './AuthContextType';
|
||||
import { setUser } from '../main';
|
||||
|
||||
const API_URL = import.meta.env.VITE_API_URL || '/api';
|
||||
import { getUser } from '../main';
|
||||
|
||||
interface AuthProviderProps {
|
||||
children: React.ReactNode;
|
||||
}
|
||||
|
||||
const defaultUser: AppUser = {
|
||||
id: 'guest-user',
|
||||
email: 'guest@gpi.app',
|
||||
name: 'Guest User',
|
||||
role: 'user',
|
||||
isBanned: false,
|
||||
createdAt: new Date().toISOString(),
|
||||
updatedAt: new Date().toISOString()
|
||||
};
|
||||
|
||||
export const AuthProvider: React.FC<AuthProviderProps> = ({ children }) => {
|
||||
const { isAuthenticated, getAccessToken, fetchUserInfo, isLoading: isLogtoLoading } = useLogto();
|
||||
const [appUser, setAppUser] = useState<AppUser | null>(null);
|
||||
const [isAppLoading, setIsAppLoading] = useState(true);
|
||||
const [error, setError] = useState<string | null>(null);
|
||||
|
||||
const syncUser = useCallback(async () => {
|
||||
if (!isAuthenticated) {
|
||||
setAppUser(null);
|
||||
setIsAppLoading(false);
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
setIsAppLoading(true);
|
||||
setError(null);
|
||||
|
||||
const token = await getAccessToken();
|
||||
if (!token) throw new Error('Token não disponível');
|
||||
|
||||
// Busca dados básicos do Logto se necessário
|
||||
const logtoUserInfo = await fetchUserInfo();
|
||||
|
||||
const response = await fetch(`${API_URL}/users/me`, {
|
||||
headers: {
|
||||
'Authorization': `Bearer ${token}`,
|
||||
},
|
||||
});
|
||||
|
||||
if (response.status === 404 && logtoUserInfo) {
|
||||
// Usuário não existe no banco (provavelmente redirecionamento pós-login)
|
||||
// Vamos tentar sincronizar/provisionar
|
||||
const syncResp = await fetch(`${API_URL}/users/sync`, {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify({
|
||||
email: logtoUserInfo.email,
|
||||
name: logtoUserInfo.name || logtoUserInfo.username || 'Usuário Logto',
|
||||
logto_id: logtoUserInfo.sub
|
||||
})
|
||||
});
|
||||
|
||||
if (!syncResp.ok) throw new Error('Falha ao sincronizar usuário');
|
||||
|
||||
// Tenta buscar novamente após o sync
|
||||
return syncUser();
|
||||
}
|
||||
|
||||
if (!response.ok) {
|
||||
throw new Error('Falha ao carregar usuário');
|
||||
}
|
||||
|
||||
const userData = await response.json();
|
||||
const effectiveRole = userData.role || 'guest';
|
||||
|
||||
const user = {
|
||||
...userData,
|
||||
id: userData._id || userData.id,
|
||||
role: effectiveRole,
|
||||
};
|
||||
|
||||
setUser(token, user);
|
||||
setAppUser(user);
|
||||
} catch (err) {
|
||||
console.error('Error loading user:', err);
|
||||
setError('Erro ao carregar dados do usuário');
|
||||
setAppUser(null);
|
||||
} finally {
|
||||
setIsAppLoading(false);
|
||||
}
|
||||
}, [isAuthenticated, getAccessToken, fetchUserInfo]);
|
||||
|
||||
useEffect(() => {
|
||||
if (!isLogtoLoading) {
|
||||
syncUser();
|
||||
const storedUser = getUser();
|
||||
if (storedUser) {
|
||||
setAppUser({ ...defaultUser, ...storedUser, role: storedUser.role as UserRole });
|
||||
} else {
|
||||
setAppUser(defaultUser);
|
||||
}
|
||||
}, [isLogtoLoading, syncUser]);
|
||||
}, []);
|
||||
|
||||
const refetchUser = useCallback(async () => {
|
||||
await syncUser();
|
||||
}, [syncUser]);
|
||||
|
||||
const isDeveloper = useCallback(() => {
|
||||
return appUser?.email === 'admtracksteel@gmail.com';
|
||||
}, [appUser]);
|
||||
|
||||
const isAdmin = useCallback(() => appUser?.role === 'admin' || isDeveloper(), [appUser, isDeveloper]);
|
||||
const isUser = useCallback(() => appUser?.role === 'user' || isAdmin(), [appUser, isAdmin]);
|
||||
const isGuest = useCallback(() => appUser?.role === 'guest' && !isDeveloper(), [appUser, isDeveloper]);
|
||||
const canEdit = useCallback(() => (appUser?.role !== 'guest' && appUser?.role !== undefined) || isDeveloper(), [appUser, isDeveloper]);
|
||||
|
||||
const isLoading = isLogtoLoading || isAppLoading;
|
||||
const isDeveloper = useCallback(() => false, []);
|
||||
const isAdmin = useCallback(() => true, []);
|
||||
const isUser = useCallback(() => true, []);
|
||||
const isGuest = useCallback(() => false, []);
|
||||
const canEdit = useCallback(() => true, []);
|
||||
const refetchUser = useCallback(async () => {}, []);
|
||||
|
||||
const value = useMemo(() => ({
|
||||
appUser,
|
||||
isLoading,
|
||||
isSignedIn: isAuthenticated,
|
||||
error,
|
||||
isLoading: false,
|
||||
isSignedIn: true,
|
||||
error: null,
|
||||
isAdmin,
|
||||
isUser,
|
||||
isGuest,
|
||||
isDeveloper,
|
||||
canEdit,
|
||||
refetchUser,
|
||||
}), [appUser, isLoading, isAuthenticated, error, isAdmin, isUser, isGuest, isDeveloper, canEdit, refetchUser]);
|
||||
}), [appUser, isAdmin, isUser, isGuest, isDeveloper, canEdit, refetchUser]);
|
||||
|
||||
return (
|
||||
<AuthContext.Provider value={value}>
|
||||
{children}
|
||||
</AuthContext.Provider>
|
||||
);
|
||||
};
|
||||
};
|
||||
@@ -1,34 +1,22 @@
|
||||
import { createRoot } from 'react-dom/client'
|
||||
import { LogtoProvider, type LogtoConfig } from '@logto/react';
|
||||
import './index.css'
|
||||
import App from './App.tsx'
|
||||
|
||||
const LOGTO_URL = import.meta.env.VITE_LOGTO_URL || 'https://logto-admin-bzlued1boxl3t8ewsyn99an9.187.77.227.172.sslip.io';
|
||||
const APP_ID = import.meta.env.VITE_LOGTO_APP_ID || 'gpi-app-final';
|
||||
|
||||
const config: LogtoConfig = {
|
||||
endpoint: LOGTO_URL,
|
||||
appId: APP_ID,
|
||||
scopes: ['openid', 'profile', 'email'],
|
||||
};
|
||||
|
||||
// Mantenha estas para compatibilidade temporária se necessário
|
||||
export function getToken() {
|
||||
return sessionStorage.getItem('logto_token');
|
||||
return 'guest-token';
|
||||
}
|
||||
|
||||
export function getUser() {
|
||||
const user = sessionStorage.getItem('logto_user');
|
||||
return user ? JSON.parse(user) : null;
|
||||
return {
|
||||
id: 'guest-user',
|
||||
email: 'guest@gpi.app',
|
||||
name: 'Guest User',
|
||||
role: 'user'
|
||||
};
|
||||
}
|
||||
|
||||
export function setUser(token: string, user: any) {
|
||||
sessionStorage.setItem('logto_token', token);
|
||||
sessionStorage.setItem('logto_user', JSON.stringify(user));
|
||||
console.log('User set (no auth):', user);
|
||||
}
|
||||
|
||||
createRoot(document.getElementById('root')!).render(
|
||||
<LogtoProvider config={config}>
|
||||
<App />
|
||||
</LogtoProvider>
|
||||
)
|
||||
createRoot(document.getElementById('root')!).render(<App />)
|
||||
Reference in New Issue
Block a user