Files
Retribusi/api/INSTALASI_CORS.md

85 lines
2.4 KiB
Markdown
Raw Permalink Normal View History

# 🚨 INSTRUKSI PERBAIKAN CORS - WAJIB DILAKUKAN
## ⚠️ MASALAH SAAT INI
- Browser tidak bisa login karena CORS error
- Request OPTIONS (preflight) return 400 Bad Request
- Server API belum memiliki CORS handler
## ✅ SOLUSI: Upload File dengan CORS Handler
### LANGKAH 1: Buka File yang Sudah Diperbaiki
File berikut sudah diperbaiki dan siap digunakan:
- `api/auth/login.php`
- `api/dashboard/summary.php`
- `api/dashboard/chart.php`
- `api/dashboard/chart_monthly.php`
- `api/dashboard/events.php`
### LANGKAH 2: Copy Kode CORS Handler
Setiap file sudah memiliki CORS handler di baris paling atas:
```php
<?php
header("Access-Control-Allow-Origin: *");
header("Access-Control-Allow-Methods: GET, POST, OPTIONS");
header("Access-Control-Allow-Headers: Content-Type, Authorization, X-API-KEY");
header("Cache-Control: no-store, no-cache, must-revalidate, max-age=0");
if ($_SERVER['REQUEST_METHOD'] === 'OPTIONS') {
http_response_code(200);
exit;
}
```
### LANGKAH 3: Upload ke Server API
**OPSI A: Upload File Lengkap**
1. Buka file `api/auth/login.php` di folder lokal
2. Copy seluruh isinya
3. Upload/replace file di server: `/retribusi/v1/api/auth/login.php`
4. Ulangi untuk semua file endpoint lainnya
**OPSI B: Tambahkan CORS Handler ke File yang Sudah Ada**
1. Buka file API yang sudah ada di server
2. Tambahkan kode CORS handler di **BARIS PALING ATAS** (sebelum require/include apapun)
3. Pastikan kode CORS dieksekusi sebelum logic lainnya
### LANGKAH 4: Test
Setelah upload, test dengan:
```bash
# Test OPTIONS (harus return 200)
curl -X OPTIONS https://api.btekno.cloud/retribusi/v1/api/auth/login.php -i
# Test POST (harus berhasil)
curl -X POST https://api.btekno.cloud/retribusi/v1/api/auth/login.php \
-H "Content-Type: application/json" \
-H "X-API-KEY: RETRIBUSI-DASHBOARD-KEY" \
-d '{"username":"admin","password":"dodolgarut"}'
```
### ✅ HASIL YANG DIHARAPKAN
Setelah upload:
-`curl -X OPTIONS` → HTTP 200 OK
- ✅ Browser bisa login tanpa error CORS
- ✅ Frontend berfungsi normal
## 📋 CHECKLIST
- [ ] File `api/auth/login.php` sudah di-upload ke server
- [ ] CORS handler ada di baris paling atas
- [ ] OPTIONS request return HTTP 200
- [ ] Test login dari browser berhasil
## ⚠️ PENTING
- CORS handler HARUS di baris paling atas
- CORS handler HARUS dieksekusi sebelum require/include
- CORS handler HARUS dieksekusi sebelum logic auth
- Setelah upload, clear cache browser jika perlu