# Fix CORS localhost:8000 Error

Your frontend is calling `http://localhost:8000` instead of your production server!

---

## The Problem

You uploaded an **old build** that was built with `localhost:8000` API URL.

The error shows:
```
Cross-Origin Request Blocked: ... http://localhost:8000/api/user
```

But it should be calling:
```
https://herataria.nova.af/backend/public/api/user
```

---

## The Solution: Rebuild and Re-upload

### Step 1: Clean Old Build

On your computer:

```bash
cd frontend
```

**Windows:**
```cmd
rmdir /s /q dist
rmdir /s /q .quasar
```

**Mac/Linux:**
```bash
rm -rf dist .quasar
```

### Step 2: Verify .env.production

Make sure `frontend/.env.production` contains:

```env
VITE_API_URL=https://herataria.nova.af/backend/public
```

✅ This file is already correct!

### Step 3: Rebuild

```bash
npm run build
```

Wait for "Build successful" message.

### Step 4: Verify Build

Open `frontend/dist/spa/assets/` folder and check one of the `.js` files.

Search for "localhost:8000" - **it should NOT appear!**

It should have "herataria.nova.af" instead.

### Step 5: Delete Old Files on Server

In cPanel File Manager:
1. Go to `public_html/frontend/`
2. **Select ALL files** (Ctrl+A)
3. Click **Delete**
4. Confirm deletion

### Step 6: Upload New Build

1. On your computer, go to `frontend\dist\spa\`
2. Select ALL files (index.html, assets, icons, etc.)
3. Compress to ZIP: `new-build.zip`
4. Upload to `public_html/frontend/`
5. Extract in cPanel
6. Delete the ZIP file

### Step 7: Test

1. Visit: `https://herataria.nova.af/`
2. Open Browser Console (F12)
3. Try to login
4. Check Network tab - should now call `herataria.nova.af/backend/public/api/login`

---

## Quick Commands

On your computer:

```bash
cd frontend
rm -rf dist .quasar
npm run build
```

Then upload `frontend/dist/spa/*` to server's `public_html/frontend/`

---

## Why This Happened

When you run `npm run build`, Vite reads `.env.production` and **bakes** the API URL into the JavaScript files.

If you:
1. Built before creating `.env.production`
2. Or built while `.env.production` had wrong URL
3. Or uploaded an old build folder

Then the frontend will have the wrong API URL hardcoded.

**Solution:** Always rebuild after changing `.env.production`!

---

## Alternative: Check What's Actually Built

To see what API URL is in your current build:

**Windows:**
```cmd
cd frontend\dist\spa\assets
findstr /S /I "localhost" *.js
```

**Mac/Linux:**
```bash
cd frontend/dist/spa/assets
grep -r "localhost" *.js
```

If you see "localhost:8000" appear, you need to rebuild!

---

## Backend CORS Config (After Frontend is Fixed)

Once frontend is calling the correct URL, make sure backend CORS is configured.

Edit `backend/config/cors.php`:

```php
<?php

return [
    'paths' => ['api/*', 'sanctum/csrf-cookie'],

    'allowed_methods' => ['*'],

    'allowed_origins' => [
        'https://herataria.nova.af',
        'https://www.herataria.nova.af',
    ],

    'allowed_origins_patterns' => [],

    'allowed_headers' => ['*'],

    'exposed_headers' => [],

    'max_age' => 0,

    'supports_credentials' => true,
];
```

Then run:
```bash
cd public_html/backend
php artisan config:cache
```

---

## Checklist

- [ ] Deleted `frontend/dist` and `frontend/.quasar` folders
- [ ] Verified `frontend/.env.production` has correct URL
- [ ] Ran `npm run build` successfully
- [ ] Verified built files don't contain "localhost:8000"
- [ ] Deleted old files on server
- [ ] Uploaded new build to server
- [ ] Tested - no more localhost:8000 errors
- [ ] Updated backend CORS config
- [ ] Ran `php artisan config:cache` on server

---

## Summary

The issue is simple: **You uploaded a build that was created before setting the production API URL.**

Fix: **Clean rebuild with correct `.env.production`, then re-upload.**

This will fix all CORS errors! ✅
