Spaces:
Sleeping

github-actions
Deploy clean workspace to Hugging Face Spaces
e8cd1ef
Raw History Blame Contribute Delete
1.57 kB
import { Request, Response, NextFunction } from 'express';
import jwt from 'jsonwebtoken';
import { PrismaClient } from '@prisma/client';
const prisma = new PrismaClient();
// Extend Express Request type to include user
declare global {
namespace Express {
interface Request {
user?: {
id: number;
username: string;
name: string;
};
}
}
}
export const authenticateToken = async (
req: Request,
res: Response,
next: NextFunction
) => {
try {
const authHeader = req.headers['authorization'];
const token = authHeader && authHeader.split(' ')[1];
if (!token) {
return res.status(401).json({ error: 'Access token required' });
}
const decoded = jwt.verify(
token,
process.env.JWT_SECRET || 'your-secret-key'
) as { userId: number; username: string };
// Verify user exists in database
const user = await prisma.customUser.findUnique({
where: { id: decoded.userId },
select: { id: true, username: true, name: true },
});
if (!user) {
return res.status(403).json({ error: 'User not found' });
}
req.user = {
id: user.id,
username: user.username,
name: user.name,
};
next();
} catch (error: any) {
if (error.name === 'TokenExpiredError') {
return res.status(401).json({ error: 'Token expired' });
}
if (error.name === 'JsonWebTokenError') {
return res.status(403).json({ error: 'Invalid token' });
}
return res.status(500).json({ error: 'Authentication error' });
}
};