fetch_jwt.sh 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. #!/usr/bin/env bash
  2. set -euo pipefail
  3. BASE_URL="${BASE_URL:-http://localhost:8000}"
  4. LOGIN_ENDPOINT="$BASE_URL/login"
  5. EMAIL="${LOGIN_EMAIL:-tester@example.com}"
  6. PASSWORD="${LOGIN_PASSWORD:-Password123!}"
  7. usage() {
  8. cat <<'USAGE'
  9. Usage: LOGIN_EMAIL=<email> LOGIN_PASSWORD=<password> ./fetch_jwt.sh
  10. Environment variables:
  11. BASE_URL Base URL of the API (default: http://localhost:8000)
  12. LOGIN_EMAIL Email used for authentication (default: tester@example.com)
  13. LOGIN_PASSWORD Password used for authentication (default: Password123!)
  14. USAGE
  15. }
  16. if [[ -z "$EMAIL" || -z "$PASSWORD" ]]; then
  17. echo "[error] LOGIN_EMAIL and LOGIN_PASSWORD must be provided" >&2
  18. usage
  19. exit 1
  20. fi
  21. PAYLOAD=$(jq -n --arg email "$EMAIL" --arg password "$PASSWORD" '{email: $email, password: $password}')
  22. RESPONSE=$(curl --fail --show-error --silent \
  23. -X POST "$LOGIN_ENDPOINT" \
  24. -H "Content-Type: application/json" \
  25. --data "$PAYLOAD")
  26. TOKEN=$(echo "$RESPONSE" | jq -r '.data.token // empty')
  27. if [[ -z "$TOKEN" ]]; then
  28. echo "[error] Unable to extract token from response" >&2
  29. echo "$RESPONSE" >&2
  30. exit 1
  31. fi
  32. echo "$TOKEN"