Protect backend routes with JWT auth middleware
This commit is contained in:
1 parent
b3a4eb9fde
commit
4313a794ce
3 files changed
+44
-8
No files matched your search
@@ -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;
|
||||
+2
-1
@@ -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);
|
||||
|
||||
@@ -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}`;
|
||||
Reference in new issue
Block a user