add: webseite mit jwt
This commit is contained in:
47
webseite-react-php-jwt/slim-php/README.md
Normal file
47
webseite-react-php-jwt/slim-php/README.md
Normal file
@@ -0,0 +1,47 @@
|
||||
Die MySQL-Datenbank und die Slim-REST-API sind eingerichtet und getestet.
|
||||
|
||||
**Datenbank `nerdshop`** (MAMP, Port 8889) enthält die Tabelle `products` mit den 13 Einträgen aus der JSON-Datei. Die Mongo-IDs bleiben als `id` erhalten; die API liefert sie als `_id`, damit die React-App kompatibel bleibt.
|
||||
|
||||
**API läuft auf** [http://localhost:8080](http://localhost:8080)
|
||||
|
||||
Authentifizierung erfolgt per **JWT** (`firebase/php-jwt`, HS256). Login liefert ein Bearer-Token; geschützte Routen erwarten `Authorization: Bearer <token>`.
|
||||
|
||||
| Methode | URL | Auth | Status |
|
||||
| -------- | ---------------- | ---- | ------------- |
|
||||
| `POST` | `/login` | nein | JWT + User |
|
||||
| `POST` | `/logout` | ja | Session Ende |
|
||||
| `GET` | `/me` | ja | aktueller User|
|
||||
| `GET` | `/users` | ja | alle User |
|
||||
| `GET` | `/users/{id}` | ja | ein User |
|
||||
| `GET` | `/products` | nein | alle Produkte |
|
||||
| `GET` | `/products/{id}` | nein | ein Produkt |
|
||||
| `POST` | `/products` | ja | anlegen |
|
||||
| `PUT` | `/products/{id}` | ja | aktualisieren |
|
||||
| `PATCH` | `/products/{id}` | ja | teilweise |
|
||||
| `DELETE` | `/products/{id}` | ja | löschen |
|
||||
|
||||
Login-Beispiel:
|
||||
|
||||
```bash
|
||||
curl -s -X POST http://localhost:8080/login \
|
||||
-H 'Content-Type: application/json' \
|
||||
-d '{"username":"megaman","password":"<passwort>"}'
|
||||
```
|
||||
|
||||
Antwort: `{ "user": { ... }, "token": "<jwt>" }`. Anschließend:
|
||||
|
||||
```bash
|
||||
curl -s http://localhost:8080/users \
|
||||
-H "Authorization: Bearer <jwt>"
|
||||
```
|
||||
|
||||
`JWT_SECRET` und `JWT_TTL` (Sekunden, Standard 86400) stehen in `.env`. CORS ist für `http://localhost:5173` (Vite) gesetzt, inklusive Header `Authorization`.
|
||||
|
||||
Server neu starten:
|
||||
|
||||
```bash
|
||||
cd slim-php-framework
|
||||
php -S localhost:8080 -t public public/router.php
|
||||
```
|
||||
|
||||
Falls MAMP andere Zugangsdaten nutzt, stehen sie in `slim-php-framework/.env`.
|
||||
Reference in New Issue
Block a user