harmadik gyakorlat
This commit is contained in:
@@ -0,0 +1,160 @@
|
||||
/**
|
||||
* AuthController
|
||||
*
|
||||
* FELADAT: Implementáld az alábbi metódusokat!
|
||||
*
|
||||
* Ez a controller felelős az authentication és authorization kezeléséért.
|
||||
* A következő funkciók implementálása szükséges:
|
||||
*
|
||||
* 1. register() - Felhasználó regisztráció
|
||||
* - Email és username uniqueness ellenőrzés
|
||||
* - Jelszó hash-elés bcrypt-tel
|
||||
* - User létrehozása a repository-n keresztül
|
||||
* - JWT token generálás
|
||||
* - Cookie beállítás
|
||||
*
|
||||
* 2. login() - Bejelentkezés
|
||||
* - User keresése email vagy username alapján
|
||||
* - Jelszó ellenőrzés bcrypt.compare()-val
|
||||
* - JWT token generálás (access és refresh token)
|
||||
* - Cookie-k beállítása
|
||||
* - Redis-ben session tárolás (opcionális)
|
||||
*
|
||||
* 3. logout() - Kijelentkezés
|
||||
* - Cookie-k törlése
|
||||
* - Redis session törlése
|
||||
*
|
||||
* 4. refreshToken() - Token frissítés
|
||||
* - Refresh token validálás
|
||||
* - Új access token generálás
|
||||
*
|
||||
* 5. getCurrentUser() - Bejelentkezett user adatai
|
||||
* - req.user alapján user visszaadása
|
||||
*/
|
||||
export class AuthController {
|
||||
constructor(userRepository) {
|
||||
this.userRepository = userRepository;
|
||||
}
|
||||
|
||||
/**
|
||||
* POST /api/auth/register
|
||||
* Új felhasználó regisztrációja
|
||||
*/
|
||||
async register(req, res) {
|
||||
try {
|
||||
// TODO: Implementáld a regisztrációt
|
||||
// 1. Validáld az input adatokat (email, username, password)
|
||||
// 2. Ellenőrizd, hogy létezik-e már a user (email vagy username)
|
||||
// 3. Hash-eld a jelszót bcrypt-tel
|
||||
// 4. Hozd létre a user-t a repository-n keresztül
|
||||
// 5. Generálj JWT tokent
|
||||
// 6. Állítsd be a cookie-kat
|
||||
// 7. Küldd vissza a user adatokat (jelszó nélkül!)
|
||||
|
||||
res.status(501).json({
|
||||
success: false,
|
||||
error: 'Register not implemented yet - this is your task!'
|
||||
});
|
||||
} catch (error) {
|
||||
res.status(400).json({
|
||||
success: false,
|
||||
error: error.message
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* POST /api/auth/login
|
||||
* Bejelentkezés
|
||||
*/
|
||||
async login(req, res) {
|
||||
try {
|
||||
// TODO: Implementáld a login-t
|
||||
// 1. Keresd meg a user-t email vagy username alapján
|
||||
// 2. Ellenőrizd a jelszót bcrypt.compare()-val
|
||||
// 3. Generálj JWT access és refresh tokent
|
||||
// 4. Állítsd be a cookie-kat (httpOnly, secure)
|
||||
// 5. Opcionálisan tárolj session-t Redis-ben
|
||||
// 6. Küldd vissza a user adatokat és tokeneket
|
||||
|
||||
res.status(501).json({
|
||||
success: false,
|
||||
error: 'Login not implemented yet - this is your task!'
|
||||
});
|
||||
} catch (error) {
|
||||
res.status(401).json({
|
||||
success: false,
|
||||
error: error.message
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* POST /api/auth/logout
|
||||
* Kijelentkezés
|
||||
*/
|
||||
async logout(req, res) {
|
||||
try {
|
||||
// TODO: Implementáld a logout-ot
|
||||
// 1. Töröld a cookie-kat
|
||||
// 2. Töröld a Redis session-t (ha van)
|
||||
// 3. Invalidáld a refresh tokent
|
||||
|
||||
res.status(501).json({
|
||||
success: false,
|
||||
error: 'Logout not implemented yet - this is your task!'
|
||||
});
|
||||
} catch (error) {
|
||||
res.status(400).json({
|
||||
success: false,
|
||||
error: error.message
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* POST /api/auth/refresh
|
||||
* Access token frissítése refresh token alapján
|
||||
*/
|
||||
async refreshToken(req, res) {
|
||||
try {
|
||||
// TODO: Implementáld a token refresh-t
|
||||
// 1. Olvasd ki a refresh tokent a cookie-ból
|
||||
// 2. Validáld a refresh tokent
|
||||
// 3. Generálj új access tokent
|
||||
// 4. Állítsd be az új cookie-t
|
||||
|
||||
res.status(501).json({
|
||||
success: false,
|
||||
error: 'Refresh token not implemented yet - this is your task!'
|
||||
});
|
||||
} catch (error) {
|
||||
res.status(401).json({
|
||||
success: false,
|
||||
error: error.message
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* GET /api/auth/me
|
||||
* Bejelentkezett felhasználó adatai
|
||||
*/
|
||||
async getCurrentUser(req, res) {
|
||||
try {
|
||||
// TODO: Implementáld a current user lekérést
|
||||
// 1. Olvasd ki a user-t req.user-ből (amit az auth middleware állít be)
|
||||
// 2. Küldd vissza a user publikus adatait
|
||||
|
||||
res.status(501).json({
|
||||
success: false,
|
||||
error: 'Get current user not implemented yet - this is your task!'
|
||||
});
|
||||
} catch (error) {
|
||||
res.status(401).json({
|
||||
success: false,
|
||||
error: error.message
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,179 @@
|
||||
import { CreateBlogCommand } from '../../application/commands/CreateBlogCommand.js';
|
||||
import { UpdateBlogCommand } from '../../application/commands/UpdateBlogCommand.js';
|
||||
import { DeleteBlogCommand } from '../../application/commands/DeleteBlogCommand.js';
|
||||
import { GetBlogQuery, GetAllBlogsQuery, GetUserBlogsQuery } from '../../application/queries/BlogQueries.js';
|
||||
|
||||
/**
|
||||
* BlogController
|
||||
* Blog műveletek kezelése
|
||||
*/
|
||||
export class BlogController {
|
||||
constructor(createHandler, updateHandler, deleteHandler, queryHandler) {
|
||||
this.createHandler = createHandler;
|
||||
this.updateHandler = updateHandler;
|
||||
this.deleteHandler = deleteHandler;
|
||||
this.queryHandler = queryHandler;
|
||||
}
|
||||
|
||||
/**
|
||||
* Blog létrehozása
|
||||
* POST /api/blogs
|
||||
*/
|
||||
async createBlog(req, res) {
|
||||
try {
|
||||
// FIGYELEM: req.user-t az auth middleware-nek kell beállítania
|
||||
// Ez a feladat része - implementálandó!
|
||||
const authorId = req.user?.id;
|
||||
|
||||
if (!authorId) {
|
||||
return res.status(401).json({ error: 'Authentication required' });
|
||||
}
|
||||
|
||||
const command = new CreateBlogCommand({
|
||||
...req.body,
|
||||
authorId
|
||||
});
|
||||
|
||||
const blog = await this.createHandler.handle(command);
|
||||
|
||||
res.status(201).json({
|
||||
success: true,
|
||||
data: blog.toObject()
|
||||
});
|
||||
} catch (error) {
|
||||
res.status(400).json({
|
||||
success: false,
|
||||
error: error.message
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Blog módosítása
|
||||
* PUT /api/blogs/:id
|
||||
*/
|
||||
async updateBlog(req, res) {
|
||||
try {
|
||||
const userId = req.user?.id;
|
||||
|
||||
if (!userId) {
|
||||
return res.status(401).json({ error: 'Authentication required' });
|
||||
}
|
||||
|
||||
const command = new UpdateBlogCommand(req.params.id, req.body);
|
||||
|
||||
// FIGYELEM: Authorization ellenőrzés szükséges!
|
||||
// Csak a szerző vagy admin módosíthatja
|
||||
// Ez a feladat része - implementálandó!
|
||||
|
||||
const blog = await this.updateHandler.handle(command);
|
||||
|
||||
res.json({
|
||||
success: true,
|
||||
data: blog.toObject()
|
||||
});
|
||||
} catch (error) {
|
||||
res.status(400).json({
|
||||
success: false,
|
||||
error: error.message
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Blog törlése
|
||||
* DELETE /api/blogs/:id
|
||||
*/
|
||||
async deleteBlog(req, res) {
|
||||
try {
|
||||
const userId = req.user?.id;
|
||||
|
||||
if (!userId) {
|
||||
return res.status(401).json({ error: 'Authentication required' });
|
||||
}
|
||||
|
||||
const command = new DeleteBlogCommand(req.params.id, userId);
|
||||
const result = await this.deleteHandler.handle(command);
|
||||
|
||||
res.json({
|
||||
success: true,
|
||||
message: result.message
|
||||
});
|
||||
} catch (error) {
|
||||
res.status(400).json({
|
||||
success: false,
|
||||
error: error.message
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Egy blog lekérése
|
||||
* GET /api/blogs/:id
|
||||
*/
|
||||
async getBlog(req, res) {
|
||||
try {
|
||||
const query = new GetBlogQuery(req.params.id);
|
||||
const blog = await this.queryHandler.handleGetBlog(query);
|
||||
|
||||
res.json({
|
||||
success: true,
|
||||
data: blog.toObject()
|
||||
});
|
||||
} catch (error) {
|
||||
res.status(404).json({
|
||||
success: false,
|
||||
error: error.message
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Összes blog lekérése
|
||||
* GET /api/blogs
|
||||
*/
|
||||
async getAllBlogs(req, res) {
|
||||
try {
|
||||
const query = new GetAllBlogsQuery({
|
||||
publishedOnly: req.query.published === 'true',
|
||||
limit: req.query.limit ? parseInt(req.query.limit) : undefined,
|
||||
offset: req.query.offset ? parseInt(req.query.offset) : 0
|
||||
});
|
||||
|
||||
const blogs = await this.queryHandler.handleGetAllBlogs(query);
|
||||
|
||||
res.json({
|
||||
success: true,
|
||||
data: blogs.map(blog => blog.toObject()),
|
||||
count: blogs.length
|
||||
});
|
||||
} catch (error) {
|
||||
res.status(400).json({
|
||||
success: false,
|
||||
error: error.message
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* User blogjai
|
||||
* GET /api/users/:userId/blogs
|
||||
*/
|
||||
async getUserBlogs(req, res) {
|
||||
try {
|
||||
const query = new GetUserBlogsQuery(req.params.userId);
|
||||
const blogs = await this.queryHandler.handleGetUserBlogs(query);
|
||||
|
||||
res.json({
|
||||
success: true,
|
||||
data: blogs.map(blog => blog.toObject()),
|
||||
count: blogs.length
|
||||
});
|
||||
} catch (error) {
|
||||
res.status(400).json({
|
||||
success: false,
|
||||
error: error.message
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user