negyedik gyakorlat + megoldasok
This commit is contained in:
@@ -0,0 +1,44 @@
|
||||
const JwtService = require('../../application/services/JwtService');
|
||||
|
||||
const jwtService = new JwtService();
|
||||
|
||||
/**
|
||||
* Authentication Middleware - JWT token ellenőrzés (Cookie-based)
|
||||
*
|
||||
* Ezt a middleware-t használd protected route-okon!
|
||||
*
|
||||
* Példa használat:
|
||||
* router.get('/me', authMiddleware, userController.getMe);
|
||||
*/
|
||||
function authMiddleware(req, res, next) {
|
||||
try {
|
||||
// 1. Token kinyerése cookieból
|
||||
const token = jwtService.extractTokenFromCookies(req.cookies);
|
||||
|
||||
if (!token) {
|
||||
return res.status(401).json({
|
||||
error: 'Authentication required',
|
||||
message: 'No token provided in cookies'
|
||||
});
|
||||
}
|
||||
|
||||
// 2. Token verifikálása
|
||||
const decoded = jwtService.verifyToken(token);
|
||||
|
||||
// 3. User adatok elhelyezése req.user-ben (controller-ek használhatják)
|
||||
req.user = {
|
||||
userId: decoded.userId,
|
||||
email: decoded.email
|
||||
};
|
||||
|
||||
// 4. Folytatás
|
||||
next();
|
||||
} catch (error) {
|
||||
return res.status(401).json({
|
||||
error: 'Authentication failed',
|
||||
message: error.message
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = authMiddleware;
|
||||
Reference in New Issue
Block a user