feat: ShootTracker SQLite+JWT+YOLOv8
This commit is contained in:
@@ -0,0 +1,29 @@
|
||||
import { Request, Response, NextFunction } from 'express'
|
||||
import jwt from 'jsonwebtoken'
|
||||
|
||||
export interface AuthRequest extends Request {
|
||||
userId?: string
|
||||
userEmail?: string
|
||||
}
|
||||
|
||||
const JWT_SECRET = process.env.JWT_SECRET || 'shoottracker-dev-secret-change-in-prod'
|
||||
|
||||
export function requireAuth(req: AuthRequest, res: Response, next: NextFunction) {
|
||||
const authHeader = req.headers.authorization
|
||||
if (!authHeader?.startsWith('Bearer ')) {
|
||||
return res.status(401).json({ error: 'Token manquant' })
|
||||
}
|
||||
const token = authHeader.slice(7)
|
||||
try {
|
||||
const payload = jwt.verify(token, JWT_SECRET) as { userId: string; email: string }
|
||||
req.userId = payload.userId
|
||||
req.userEmail = payload.email
|
||||
next()
|
||||
} catch {
|
||||
return res.status(401).json({ error: 'Token invalide ou expiré' })
|
||||
}
|
||||
}
|
||||
|
||||
export function signToken(userId: string, email: string): string {
|
||||
return jwt.sign({ userId, email }, JWT_SECRET, { expiresIn: '30d' })
|
||||
}
|
||||
Reference in New Issue
Block a user