diff --git a/server/index.js b/server/index.js
index 7adac36f..9db2bdc1 100644
--- a/server/index.js
+++ b/server/index.js
@@ -29,6 +29,20 @@ app.get('/api/products', async (_, res) => {
res.json(result.rows);
});
+app.post('/api/login', (req, res) => {
+ const { username, password } = req.body || {};
+
+ const validUser = process.env.ADMIN_USER || process.env.PGUSER || 'postgres';
+ const validPassword = process.env.ADMIN_PASSWORD || process.env.PGPASSWORD || 'postgres';
+
+ if (username === validUser && password === validPassword) {
+ res.json({ token: 'ok', name: 'Administrador' });
+ return;
+ }
+
+ res.status(401).json({ message: 'Credenciais inválidas' });
+});
+
app.post('/api/products', async (req, res) => {
const { name, price, category, description, active = true } = req.body;
const query =
diff --git a/src/App.js b/src/App.js
index 089f2e72..cd94412e 100644
--- a/src/App.js
+++ b/src/App.js
@@ -10,10 +10,9 @@ import {
FileText,
LogOut
} from 'lucide-react';
-import { signInWithPopup, onAuthStateChanged, signOut } from 'firebase/auth';
-import { auth, googleProvider } from './config/firebase';
import { productService } from './services/productService';
import { orderService } from './services/orderService';
+import { authService } from './services/authService';
import { MenuView } from './components/Client/MenuView';
import { CartView } from './components/Client/CartView';
import { SuccessView } from './components/Client/SuccessView';
@@ -33,13 +32,20 @@ function App() {
const [adminTab, setAdminTab] = useState('dashboard');
const [cart, setCart] = useState({});
const [customer, setCustomer] = useState(initialCustomer);
+ const [loginForm, setLoginForm] = useState({ username: '', password: '' });
+ const [loginError, setLoginError] = useState('');
useEffect(() => {
- const unsubAuth = onAuthStateChanged(auth, setUser);
+ const savedSession = localStorage.getItem('adminSession');
+ if (savedSession) {
+ const parsedSession = JSON.parse(savedSession);
+ setUser(parsedSession);
+ setView('admin');
+ }
+
const unsubProd = productService.subscribe(setProducts);
const unsubOrders = orderService.subscribeAll(setOrders);
return () => {
- unsubAuth();
unsubProd();
unsubOrders();
};
@@ -91,17 +97,25 @@ function App() {
setView('success');
};
- const handleLogin = async () => {
+ const handleLogin = async (event) => {
+ event?.preventDefault();
+ setLoginError('');
+
try {
- await signInWithPopup(auth, googleProvider);
+ const session = await authService.login(loginForm.username, loginForm.password);
+ const sessionData = { ...session, username: loginForm.username };
+ localStorage.setItem('adminSession', JSON.stringify(sessionData));
+ setUser(sessionData);
setView('admin');
} catch (error) {
- alert('Erro ao logar: ' + error.message);
+ setLoginError(error.message || 'Falha ao autenticar');
}
};
const logout = () => {
- signOut(auth);
+ localStorage.removeItem('adminSession');
+ setUser(null);
+ setLoginForm({ username: '', password: '' });
setView('menu');
};
@@ -152,11 +166,11 @@ function App() {
- {user.displayName?.[0] || 'U'}
+ {user.name?.[0]?.toUpperCase() || user.username?.[0]?.toUpperCase() || 'U'}
-
{user.displayName}
-
{user.email}
+
{user.name || 'Administrador'}
+
{user.username || 'admin'}