99 lines
2.7 KiB
Markdown
99 lines
2.7 KiB
Markdown
# Negyedik Gyakorlat - MINTA Megoldás
|
|
|
|
Ez a mappa tartalmazza a negyedik gyakorlat **teljes, működő megoldását**.
|
|
|
|
## Amit implementáltunk:
|
|
|
|
### 1. **Dependency Injection Container**
|
|
- Singleton lifecycle - egy példány az egész alkalmazásban
|
|
- Transient lifecycle - minden híváskor új példány
|
|
- Scoped lifecycle - request szintű scope
|
|
- Service regisztráció és feloldás
|
|
- Circular dependency kezelés
|
|
|
|
### 2. **CORS Konfigurás**
|
|
- Whitelist-based origin ellenőrzés
|
|
- Credentials support (cookie-k)
|
|
- Preflight request kezelés
|
|
- Custom CORS middleware
|
|
|
|
### 3. **Email Service (Nodemailer + Handlebars)**
|
|
- Ethereal test SMTP
|
|
- HTML email templatek (Handlebars)
|
|
- Welcome email új usereknek
|
|
- Password reset email
|
|
- Template rendering
|
|
|
|
### 4. **Cookie-based JWT Auth**
|
|
- Tokenek csak cookie-ban (nem response body-ban!)
|
|
- HttpOnly, Secure, SameSite cookie-k
|
|
- Automatikus token ellenőrzés middleware-ből
|
|
|
|
## Indítás
|
|
|
|
```bash
|
|
npm install
|
|
npm run docker:up
|
|
npx prisma generate
|
|
npx prisma migrate dev
|
|
npm run dev
|
|
```
|
|
|
|
## Környezeti Változók
|
|
|
|
```env
|
|
DATABASE_URL="postgresql://postgres:postgres@localhost:5432/cors_di_app?schema=public"
|
|
JWT_SECRET="your-secret-key"
|
|
JWT_EXPIRES_IN="7d"
|
|
PORT=3000
|
|
NODE_ENV=development
|
|
|
|
# Ethereal Email
|
|
ETHEREAL_USER=your-ethereal-user@ethereal.email
|
|
ETHEREAL_PASS=your-ethereal-password
|
|
|
|
# CORS
|
|
ALLOWED_ORIGINS=http://localhost:3000,http://localhost:5173
|
|
```
|
|
|
|
## DI Container Példa
|
|
|
|
```javascript
|
|
// Service regisztráció
|
|
container.register('database', () => new DatabaseConnection(), 'singleton');
|
|
container.register('userRepo', () => new UserRepository(), 'singleton');
|
|
container.register('emailService', () => new EmailService(), 'singleton');
|
|
container.register('requestId', () => crypto.randomUUID(), 'transient');
|
|
container.register('jwtService', () => new JwtService(), 'scoped');
|
|
|
|
// Service feloldás
|
|
const userRepo = container.resolve('userRepo');
|
|
const emailService = container.resolve('emailService');
|
|
|
|
// Scoped resolution (request szintű)
|
|
const scope = container.createScope();
|
|
const jwtService = scope.resolve('jwtService');
|
|
```
|
|
|
|
## Email Service Példa
|
|
|
|
```javascript
|
|
// Welcome email küldése
|
|
await emailService.sendWelcomeEmail(user.email, user.name);
|
|
|
|
// Password reset email
|
|
await emailService.sendPasswordResetEmail(user.email, resetToken);
|
|
```
|
|
|
|
## CORS Engedélyezett Eredethelyekről
|
|
|
|
Csak az `ALLOWED_ORIGINS` környezeti változóban megadott origin-ek férhetnek hozzá az API-hoz.
|
|
|
|
## Tanulási Pontok
|
|
|
|
1. **Dependency Injection**: Service lifecycle management
|
|
2. **CORS**: Biztonságos cross-origin konfigurás
|
|
3. **Email Templates**: Handlebars template rendering
|
|
4. **Nodemailer**: Email küldés SMTP-vel
|
|
5. **Cookie-based Auth**: Token kezelés csak cookie-kban
|