From d2e7088146fbbc6ae86fffe36fb248fc61987545 Mon Sep 17 00:00:00 2001 From: Webklar Deploy Date: Fri, 22 May 2026 18:23:37 +0000 Subject: [PATCH] =?UTF-8?q?Vite-Proxy=20f=C3=BCr=20lokale=20Appwrite-Entwi?= =?UTF-8?q?cklung?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Leitet /v1 an ticket.webklar.com weiter und dokumentiert .env.local, damit Session-Cookies auf localhost wie auf dem Server funktionieren. Co-authored-by: Cursor --- .env.example | 14 +++++++++++++ LOCAL_DEV.md | 55 ++++++++++++++++++++++++++++++++++++++++++++++++++ vite.config.js | 22 +++++++++++++++++--- 3 files changed, 88 insertions(+), 3 deletions(-) create mode 100644 .env.example create mode 100644 LOCAL_DEV.md diff --git a/.env.example b/.env.example new file mode 100644 index 0000000..b5a032b --- /dev/null +++ b/.env.example @@ -0,0 +1,14 @@ +# Kopieren nach .env.local (wird nicht von Git versioniert): +# cp .env.example .env.local +# +# Lokal mit Vite-Proxy (empfohlen — Session-Cookies wie auf dem Server): +VITE_APPWRITE_ENDPOINT=http://localhost:5173/v1 +VITE_APPWRITE_PROJECT_ID=6a1058610003c5a13a05 +VITE_APPWRITE_DATABASE_ID=woms-database +VITE_APPWRITE_BUCKET_ID=woms-attachments + +# Optional: anderes Proxy-Ziel (Standard: https://ticket.webklar.com) +# VITE_APPWRITE_PROXY_TARGET=https://ticket.webklar.com + +# Produktion / Build (ohne Vite-Proxy): +# VITE_APPWRITE_ENDPOINT=https://ticket.webklar.com/v1 diff --git a/LOCAL_DEV.md b/LOCAL_DEV.md new file mode 100644 index 0000000..b570a1e --- /dev/null +++ b/LOCAL_DEV.md @@ -0,0 +1,55 @@ +# Lokale Entwicklung (Appwrite) + +## Warum funktioniert es auf dem Server, aber nicht mit `npm run dev`? + +| | Server (`ticket.webklar.com`) | Lokal (`localhost:5173`) | +|---|---|---| +| App-URL | `https://ticket.webklar.com` | `http://localhost:5173` | +| API-URL | `https://ticket.webklar.com/v1` (nginx-Proxy) | oft `https://ticket.webklar.com/v1` direkt | +| **Gleiche Origin?** | Ja | **Nein** (Cross-Origin) | +| Session-Cookie | `domain=.ticket.webklar.com` | Browser speichert Cookie **nicht** für localhost | + +Auf dem Server leitet nginx `/v1/` an Appwrite weiter und schreibt Cookies auf `ticket.webklar.com` um. Die React-App und die API sind **dieselbe Site** — Login funktioniert. + +Lokal ruft der Browser die API auf einer **anderen Domain** auf. Appwrite setzt Cookies für `.ticket.webklar.com`. Die werden bei Requests von `localhost:5173` **nicht mitgeschickt** ? `guests missing scopes`, Login scheint tot. + +CORS für `http://localhost:5173` ist auf dem Server erlaubt — das reicht allein nicht für HttpOnly-Session-Cookies. + +## Lösung: Vite-Proxy + `.env.local` + +1. Datei anlegen (nicht in Git): + + ```bash + cp .env.example .env.local + ``` + +2. In `.env.local` muss stehen: + + ```env + VITE_APPWRITE_ENDPOINT=http://localhost:5173/v1 + VITE_APPWRITE_PROJECT_ID=6a1058610003c5a13a05 + VITE_APPWRITE_DATABASE_ID=woms-database + VITE_APPWRITE_BUCKET_ID=woms-attachments + ``` + +3. Dev-Server starten: + + ```bash + npm install + npm run dev + ``` + +`vite.config.js` leitet `/v1` an `https://ticket.webklar.com` weiter und schreibt Cookie-Domains auf `localhost` um — Verhalten wie nginx auf dem Server. + +## Checkliste bei Problemen + +- [ ] `.env.local` existiert (`.env` allein reicht; Vite lädt `.env.local` mit höherer Priorität) +- [ ] Endpoint ist `http://localhost:5173/v1`, **nicht** `https://appwrite.webklar.com/v1` (altes Projekt) +- [ ] Nach Login: DevTools ? Application ? Cookies ? `localhost` ? `a_session_6a1058610003c5a13a05` +- [ ] Appwrite Console ? Projekt **Ticket-System** ? Platforms ? `localhost` eingetragen + +## Git / Deploy + +- `.env` und `.env.local` sind in `.gitignore` — jeder Entwickler braucht eigene `.env.local` +- Server-Build nutzt Container-Env (`VITE_APPWRITE_ENDPOINT=https://ticket.webklar.com/v1`) +- Push nach Gitea deployt nur den **Frontend-Code**, nicht deine lokale `.env.local` diff --git a/vite.config.js b/vite.config.js index 9ffcc67..3c6c1a4 100644 --- a/vite.config.js +++ b/vite.config.js @@ -1,6 +1,22 @@ -import { defineConfig } from 'vite' +import { defineConfig, loadEnv } from 'vite' import react from '@vitejs/plugin-react' -export default defineConfig({ - plugins: [react()], +export default defineConfig(({ mode }) => { + const env = loadEnv(mode, process.cwd(), '') + const apiTarget = env.VITE_APPWRITE_PROXY_TARGET || 'https://ticket.webklar.com' + + return { + plugins: [react()], + server: { + proxy: { + // Lokal: Appwrite über gleiche Origin wie die App (wie nginx auf dem Server) + '/v1': { + target: apiTarget, + changeOrigin: true, + secure: true, + cookieDomainRewrite: 'localhost', + }, + }, + }, + } })