diff --git a/backend/middleware/authMiddleware.js b/backend/middleware/authMiddleware.js new file mode 100644 index 0000000..87c0d83 --- /dev/null +++ b/backend/middleware/authMiddleware.js @@ -0,0 +1,26 @@ +const jwt = require('jsonwebtoken'); +const configService = require('../services/configService'); + +function authMiddleware(req, res, next) { + // Skip auth for login/verify endpoints + if (req.path === '/auth/login' || req.path === '/auth/verify') { + return next(); + } + + const auth = req.headers.authorization; + if (!auth || !auth.startsWith('Bearer ')) { + return res.status(401).json({ error: 'Authentication required' }); + } + + const settings = configService.getSettings(); + const jwtSecret = settings.jwtSecret || 'showy-default-secret'; + + try { + jwt.verify(auth.slice(7), jwtSecret); + next(); + } catch { + return res.status(401).json({ error: 'Invalid or expired token' }); + } +} + +module.exports = authMiddleware; diff --git a/backend/server.js b/backend/server.js index 1bd0725..fb611d8 100644 --- a/backend/server.js +++ b/backend/server.js @@ -23,6 +23,7 @@ const sequelizeHooks = require('./config/sequelizeHooks.js'); const { initializeData } = require('./config/dataInitializer.js'); const killerRouter = require('./routes/killer.routes.js'); const errorHandler = require('./middleware/errorHandler'); +const authMiddleware = require('./middleware/authMiddleware'); const configService = require('./services/configService.js'); const { logToFile } = require('./services/logService.js'); const { generatePreview } = require('./services/mediaService.js'); @@ -51,7 +52,7 @@ app.use((req, res, next) => { }); // ? other api routes -app.use('/api/v1', routes); +app.use('/api/v1', authMiddleware, routes); // ? Error handling middleware app.use(errorHandler); diff --git a/react_frontend/src/lib/api.ts b/react_frontend/src/lib/api.ts index 736e8ff..7489526 100644 --- a/react_frontend/src/lib/api.ts +++ b/react_frontend/src/lib/api.ts @@ -1,23 +1,32 @@ -import axios from "axios"; +import axios from 'axios'; +import { useAuthStore } from '@/stores/useAuthStore'; const API_BASE_URL = - import.meta.env.VITE_ELECTRON === "true" - ? "http://localhost:4000/api/v1" - : import.meta.env.VITE_API_URL || "/api/v1"; + import.meta.env.VITE_ELECTRON === 'true' + ? 'http://localhost:4000/api/v1' + : import.meta.env.VITE_API_URL || '/api/v1'; export const api = axios.create({ baseURL: API_BASE_URL, headers: { - "Content-Type": "application/json", + 'Content-Type': 'application/json', }, }); +api.interceptors.request.use((config) => { + const token = useAuthStore.getState().token; + if (token) { + config.headers.Authorization = `Bearer ${token}`; + } + return config; +}); + export const getMediaUrl = ( category: string, type: string, - size: "preview" | "full", + size: 'preview' | 'full', file: string, ) => `${API_BASE_URL}/media/${category}/${type}/${size}/${file}`; -export const getTmdbImageUrl = (path: string, size = "w342") => +export const getTmdbImageUrl = (path: string, size = 'w342') => `https://image.tmdb.org/t/p/${size}${path}`;