From 0dacce974c24f9b48ba2017c23b005bdc8112591 Mon Sep 17 00:00:00 2001 From: mwpn Date: Wed, 17 Dec 2025 13:57:11 +0700 Subject: [PATCH] docs: Tambahkan instruksi deployment CORS dan troubleshooting --- DEPLOYMENT.md | 74 ++++++++++++++++++++++++++++- DEPLOY_CORS.md | 123 +++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 196 insertions(+), 1 deletion(-) create mode 100644 DEPLOY_CORS.md diff --git a/DEPLOYMENT.md b/DEPLOYMENT.md index 3a0a307..166ccd6 100644 --- a/DEPLOYMENT.md +++ b/DEPLOYMENT.md @@ -58,7 +58,19 @@ composer install --no-dev --optimize-autoloader # 3. Regenerate autoloader composer dump-autoload --optimize -# 4. Clear cache (jika ada) +# 4. Update .env dengan konfigurasi CORS (jika belum ada) +# Edit .env dan tambahkan: +# CORS_ALLOWED_ORIGINS=* +# CORS_ALLOWED_METHODS=GET,POST,PUT,DELETE,OPTIONS +# CORS_ALLOWED_HEADERS=Content-Type,Authorization,X-API-KEY,Accept,Origin +# CORS_ALLOW_CREDENTIALS=true + +# 5. Restart PHP-FPM (opsional, untuk memastikan perubahan ter-load) +# Via aaPanel: Website -> PHP -> Service Management -> Reload +# Atau via command: +# systemctl reload php-fpm-83 # Sesuaikan dengan PHP version + +# 6. Clear cache (jika ada) # Tidak ada cache untuk project ini, skip ``` @@ -163,6 +175,14 @@ JWT_ISSUER=api-btekno # API Key RETRIBUSI_API_KEY=generate-secure-api-key-here + +# CORS (Cross-Origin Resource Sharing) +# Untuk development: gunakan '*' untuk allow semua origin +# Untuk production: list origin yang diizinkan dipisah koma +CORS_ALLOWED_ORIGINS=* +CORS_ALLOWED_METHODS=GET,POST,PUT,DELETE,OPTIONS +CORS_ALLOWED_HEADERS=Content-Type,Authorization,X-API-KEY,Accept,Origin +CORS_ALLOW_CREDENTIALS=true ``` **Generate secure keys:** @@ -236,6 +256,58 @@ chown -R www:www /www/wwwroot/api.btekno.cloud/api chmod -R 755 /www/wwwroot/api.btekno.cloud/api ``` +### Error: CORS belum dikonfigurasi / CORS error di browser + +**Cause**: CORS middleware belum ter-deploy atau konfigurasi `.env` belum ada +**Solution**: + +1. **Pastikan code terbaru sudah di-pull:** +```bash +cd /www/wwwroot/api.btekno.cloud/api +git pull origin main +composer dump-autoload --optimize +``` + +2. **Pastikan `.env` sudah ada konfigurasi CORS:** +```bash +nano /www/wwwroot/api.btekno.cloud/api/.env +``` + +Tambahkan (atau pastikan sudah ada): +```env +CORS_ALLOWED_ORIGINS=* +CORS_ALLOWED_METHODS=GET,POST,PUT,DELETE,OPTIONS +CORS_ALLOWED_HEADERS=Content-Type,Authorization,X-API-KEY,Accept,Origin +CORS_ALLOW_CREDENTIALS=true +``` + +3. **Restart PHP-FPM:** +```bash +# Via aaPanel: Website -> PHP -> Service Management -> Reload +# Atau via command (sesuaikan PHP version): +systemctl reload php-fpm-83 +``` + +4. **Test CORS dari browser console:** +```javascript +fetch('https://api.btekno.cloud/health', { + method: 'GET', + headers: { + 'Content-Type': 'application/json' + } +}) +.then(res => res.json()) +.then(data => console.log('CORS OK:', data)) +.catch(err => console.error('CORS Error:', err)); +``` + +5. **Cek response headers:** +```bash +curl -I -H "Origin: http://localhost:3000" https://api.btekno.cloud/health +``` + +Harus ada header `Access-Control-Allow-Origin` di response. + ## 📊 Monitoring - Check logs: `/www/wwwroot/api.btekno.cloud/api/logs/` (jika ada) diff --git a/DEPLOY_CORS.md b/DEPLOY_CORS.md new file mode 100644 index 0000000..c4f6d26 --- /dev/null +++ b/DEPLOY_CORS.md @@ -0,0 +1,123 @@ +# 🚀 Quick Deploy CORS Middleware + +## ⚡ Langkah Cepat (Production Server) + +```bash +# 1. Masuk ke folder project +cd /www/wwwroot/api.btekno.cloud/api + +# 2. Pull code terbaru (yang sudah include CORS middleware) +git pull origin main + +# 3. Regenerate autoloader (untuk load class CorsMiddleware) +composer dump-autoload --optimize + +# 4. Edit .env dan tambahkan konfigurasi CORS +nano .env +``` + +**Tambahkan ke file `.env`:** + +```env +# CORS Configuration +CORS_ALLOWED_ORIGINS=* +CORS_ALLOWED_METHODS=GET,POST,PUT,DELETE,OPTIONS +CORS_ALLOWED_HEADERS=Content-Type,Authorization,X-API-KEY,Accept,Origin +CORS_ALLOW_CREDENTIALS=true +``` + +**Untuk Production (lebih aman):** +```env +# Ganti * dengan domain yang diizinkan +CORS_ALLOWED_ORIGINS=https://app.example.com,https://dashboard.example.com +``` + +```bash +# 5. Restart PHP-FPM (via aaPanel atau command) +# Via aaPanel: Website -> PHP -> Service Management -> Reload +# Atau: +systemctl reload php-fpm-83 # Sesuaikan dengan PHP version +``` + +## ✅ Verifikasi CORS Aktif + +### Test 1: Cek Response Headers + +```bash +curl -I -H "Origin: http://localhost:3000" https://api.btekno.cloud/health +``` + +**Harus ada header:** +``` +Access-Control-Allow-Origin: * +Access-Control-Allow-Methods: GET, POST, PUT, DELETE, OPTIONS +Access-Control-Allow-Headers: Content-Type, Authorization, X-API-KEY, Accept, Origin +``` + +### Test 2: Test Preflight (OPTIONS) + +```bash +curl -X OPTIONS \ + -H "Origin: http://localhost:3000" \ + -H "Access-Control-Request-Method: POST" \ + -H "Access-Control-Request-Headers: Content-Type,Authorization" \ + https://api.btekno.cloud/auth/v1/login +``` + +**Harus return:** HTTP 204 No Content dengan CORS headers. + +### Test 3: Test dari Browser Console + +Buka browser console (F12) dan jalankan: + +```javascript +fetch('https://api.btekno.cloud/health', { + method: 'GET', + headers: { + 'Content-Type': 'application/json' + } +}) +.then(res => res.json()) +.then(data => { + console.log('✅ CORS OK:', data); +}) +.catch(err => { + console.error('❌ CORS Error:', err); +}); +``` + +## 🔍 Troubleshooting + +### Masalah: CORS headers tidak muncul + +**Solusi:** +1. Pastikan `CorsMiddleware` sudah di-load di `src/Bootstrap/app.php` ✅ +2. Pastikan `.env` sudah ada konfigurasi CORS ✅ +3. Restart PHP-FPM ✅ +4. Clear browser cache dan coba lagi + +### Masalah: Preflight OPTIONS return 404 + +**Solusi:** +- Pastikan routing middleware sudah di-load setelah CORS middleware ✅ +- Cek nginx config: pastikan `try_files` mengarah ke `index.php` + +### Masalah: CORS hanya work untuk localhost + +**Solusi:** +- Edit `.env` dan set `CORS_ALLOWED_ORIGINS` dengan domain yang benar +- Atau gunakan `*` untuk development (tidak recommended untuk production) + +## 📝 Catatan Penting + +1. **CORS middleware sudah otomatis handle:** + - Preflight OPTIONS request + - CORS headers di semua response + - Localhost detection (http://localhost, http://127.0.0.1 dengan port apapun) + +2. **Tidak perlu konfigurasi nginx untuk CORS** - semua di-handle di PHP middleware + +3. **Untuk production:** Ganti `CORS_ALLOWED_ORIGINS=*` dengan list domain yang spesifik + +4. **CORS headers akan muncul di semua endpoint** - termasuk `/health`, `/auth`, `/retribusi`, dll +