Skip to content

Commit 651466a

Browse files
committed
Security changes
1 parent 9771c28 commit 651466a

8 files changed

Lines changed: 33 additions & 148 deletions

File tree

backend/auth.js

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
import passport from "passport";
22
import { Strategy as GoogleStrategy } from "passport-google-oauth20";
3-
import { PrismaClient } from "@prisma/client";
4-
5-
const prisma = new PrismaClient();
3+
import prisma from "./prismaClient.js";
64

75
passport.serializeUser((user, done) => {
86
done(null, user.id);
@@ -80,6 +78,4 @@ passport.use(
8078
}
8179
}
8280
)
83-
);
84-
85-
export default prisma;
81+
);

backend/index.js

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ import session from "express-session";
55
import passport from "passport";
66
import pg from "pg";
77
import { createRequire } from "module";
8-
import prisma from "./auth.js";
8+
import prisma from "./prismaClient.js";
9+
import "./auth.js";
910
import requireAuth from "./middleware/requireAuth.js";
1011
import { handleOrderFiscalization } from "./fira.js";
1112
import { getSessionRange } from './utils/sessionHelper.js';
@@ -37,7 +38,11 @@ app.use(
3738
secret: process.env.SESSION_SECRET || "dev_secret",
3839
resave: false,
3940
saveUninitialized: false,
40-
cookie: { secure: false }, // dev only
41+
cookie: {
42+
secure: process.env.NODE_ENV === "production",
43+
httpOnly: true,
44+
sameSite: "lax"
45+
},
4146
})
4247
);
4348

@@ -53,10 +58,10 @@ app.get("/api/health", async (req, res) => {
5358
const transactionCount = await prisma.transaction.count();
5459
const receiptCount = await prisma.receipt.count();
5560

56-
console.log(` ✅ Database OK!`);
57-
console.log(` Users: ${userCount}`);
58-
console.log(` Transactions: ${transactionCount}`);
59-
console.log(` Receipts: ${receiptCount}`);
61+
console.log(`✅ Database OK!`);
62+
console.log(`Users: ${userCount}`);
63+
console.log(`Transactions: ${transactionCount}`);
64+
console.log(`Receipts: ${receiptCount}`);
6065

6166
res.json({
6267
status: "OK",
@@ -250,7 +255,7 @@ app.get("/api/receipts", requireAuth, async (req, res) => {
250255
}
251256
});
252257

253-
app.get('/api/receipts/current-session', async (req, res) => {
258+
app.get('/api/receipts/current-session', requireAuth, async (req, res) => {
254259
try {
255260
const now = new Date();
256261
const start = new Date(now);
@@ -286,7 +291,7 @@ app.get('/api/receipts/current-session', async (req, res) => {
286291

287292
// ========== RECEIPTS API UPDATES ==========
288293

289-
app.get('/api/receipts/active-dates', async (req, res) => {
294+
app.get('/api/receipts/active-dates', requireAuth, async (req, res) => {
290295
try {
291296
const receipts = await prisma.receipt.findMany({
292297
select: { createdAt: true }
@@ -306,7 +311,7 @@ app.get('/api/receipts/active-dates', async (req, res) => {
306311
}
307312
});
308313

309-
app.get('/api/receipts/range', async (req, res) => {
314+
app.get('/api/receipts/range', requireAuth, async (req, res) => {
310315
const { from, to } = req.query;
311316

312317
try {
@@ -910,7 +915,7 @@ app.put('/api/settings/active-location', requireAuth, async (req, res) => {
910915
});
911916

912917
// GET all prodajna mjesta
913-
app.get('/api/prodajna-mjesta', async (req, res) => {
918+
app.get('/api/prodajna-mjesta', requireAuth, async (req, res) => {
914919
try {
915920
const locations = await prisma.prodajnoMjesto.findMany();
916921
const safeLocations = locations.map(loc => {
@@ -928,7 +933,7 @@ app.get('/api/prodajna-mjesta', async (req, res) => {
928933
});
929934

930935
// POST new prodajno mjesto
931-
app.post('/api/prodajna-mjesta', async (req, res) => {
936+
app.post('/api/prodajna-mjesta', requireAuth, async (req, res) => {
932937
try {
933938
const { name, businessSpace, paymentDevice, firaApiKey, active } = req.body;
934939

@@ -960,7 +965,7 @@ app.post('/api/prodajna-mjesta', async (req, res) => {
960965
});
961966

962967
// PUT (update) prodajno mjesto
963-
app.put('/api/prodajna-mjesta/:id', async (req, res) => {
968+
app.put('/api/prodajna-mjesta/:id', requireAuth, async (req, res) => {
964969
const { id } = req.params;
965970
const { name, businessSpace, paymentDevice, firaApiKey, active } = req.body;
966971
try {
@@ -979,7 +984,7 @@ app.put('/api/prodajna-mjesta/:id', async (req, res) => {
979984
});
980985

981986
// DELETE prodajno mjesto
982-
app.delete('/api/prodajna-mjesta/:id', async (req, res) => {
987+
app.delete('/api/prodajna-mjesta/:id', requireAuth, async (req, res) => {
983988
const { id } = req.params;
984989
try {
985990
await prisma.prodajnoMjesto.delete({

backend/prismaClient.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import { PrismaClient } from "@prisma/client";
2+
3+
const prisma = new PrismaClient();
4+
5+
export default prisma;

frontend/src/ProtectedRoute.jsx

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,11 @@ export default function ProtectedRoute({ children }) {
99
credentials: "include"
1010
})
1111
.then(res => res.json())
12-
.then(data => setUser(data));
12+
.then(data => setUser(data))
13+
.catch(error => {
14+
console.error("Auth check failed:", error);
15+
setUser(null);
16+
});
1317
}, []);
1418

1519
if (user === undefined) return <div>Loading...</div>;

frontend/src/pages/Home.jsx

Lines changed: 0 additions & 104 deletions
This file was deleted.

frontend/src/pages/Prodaja.jsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ export default function Prodaja() {
5959

6060
fetchLocations();
6161
fetchActiveLocation();
62-
}, [selectedLocationId]);
62+
}, []);
6363

6464
const fetchArticles = async () => {
6565
try {

frontend/src/pages/admin/Kategorije.jsx

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -29,18 +29,7 @@ export default function Kategorije() {
2929
};
3030

3131
useEffect(() => {
32-
(async () => {
33-
try {
34-
const response = await fetch(`${import.meta.env.VITE_API_URL}/api/categories`, { credentials: "include" });
35-
const data = await response.json();
36-
setCategories(Array.isArray(data) ? data : []);
37-
setLoading(false);
38-
} catch (error) {
39-
console.error("Error fetching categories:", error);
40-
setCategories([]);
41-
setLoading(false);
42-
}
43-
})();
32+
fetchCategories();
4433
}, []);
4534

4635
const resetForm = () => {

frontend/src/pages/admin/Korisnici.jsx

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -24,17 +24,7 @@ export default function Korisnici() {
2424
};
2525

2626
useEffect(() => {
27-
(async () => {
28-
try {
29-
const response = await fetch(`${import.meta.env.VITE_API_URL}/api/users`, { credentials: "include" });
30-
const data = await response.json();
31-
setUsers(data);
32-
setLoading(false);
33-
} catch (error) {
34-
console.error("Error fetching users:", error);
35-
setLoading(false);
36-
}
37-
})();
27+
fetchUsers();
3828
}, []);
3929

4030
const handleEdit = (user) => {

0 commit comments

Comments
 (0)