Initial commit: Centralized auth service with Discord OAuth

This commit is contained in:
2026-06-30 17:16:08 +02:00
commit aa5168b201
7 changed files with 1619 additions and 0 deletions
+18
View File
@@ -0,0 +1,18 @@
import jwt from 'jsonwebtoken';
const JWT_SECRET = process.env.JWT_SECRET || 'sandbox-secret-key-change-in-production';
export function generateToken(userId) {
return jwt.sign({ userId }, JWT_SECRET, { expiresIn: '7d' });
}
export function authenticateToken(req, res, next) {
const authHeader = req.headers['authorization'];
const token = authHeader && authHeader.split(' ')[1];
if (!token) return res.status(401).json({ error: 'Authentication required' });
jwt.verify(token, JWT_SECRET, (err, decoded) => {
if (err) return res.status(403).json({ error: 'Invalid or expired token' });
req.userId = decoded.userId;
next();
});
}