Commit f7cfa8a
authored
chore(demo): support api_key query param in vite example (#3210)
### 🎯 Goal
The vite example could only take the Stream API key from the
`VITE_STREAM_API_KEY` env var, while the user `token` was already
overridable via a query param. This makes it awkward to point the
running example at a different Stream app without editing `.env` and
restarting. This lets you pass `?api_key=…` (and combine it with
`?token=…`) directly in the URL, matching how `token` already works.
### 🛠 Implementation details
- `apiKey` now reads from the `api_key` query param first, falling back
to `VITE_STREAM_API_KEY` — mirroring the existing `token` resolution.
- `userId` now derives from the provided `token` (via
`parseUserIdFromToken`) when present, slotted just below the explicit
`user_id` query param and above the env/localStorage/random fallbacks.
This makes `?api_key=…&token=…` self-sufficient: previously the pronto
token generator was only skipped when the resolved `userId` matched the
token's embedded `user_id`, so omitting `user_id` silently fell back to
pronto with the wrong environment/app.
- `parseUserIdFromToken` now returns `undefined` instead of throwing on
a malformed/empty token, since it now runs during render; this also
hardens the existing `tokenProvider` call site.
User-id precedence: `?user_id=` → token-derived → `VITE_USER_ID` →
`localStorage` → random. The explicit `user_id` param still wins,
preserving the "connect as someone else, regenerate via pronto" path.
### 🎨 UI Changes
No visual changes — this only affects how the example app sources its
credentials. Verified by driving the running example with Playwright:
- `?api_key=test_key_AAA&token=<jwt for "bob">` → Stream WS connects
with `api_key=test_key_AAA` and `user_id=bob`; pronto skipped.
- `?api_key=test_key_BBB` (no token) → pronto `create-token` called
(fallback intact).
- `?api_key=xzwhhgtazy6h` (public key) → real connection succeeds, chat
UI renders, zero console errors.
<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->
## Summary by CodeRabbit
* **New Features**
* API key can now be optionally specified via query parameter, with
fallback to environment configuration
* Enhanced token parsing with improved error handling that gracefully
manages malformed tokens
<!-- end of auto-generated comment: release notes by coderabbit.ai -->1 parent 7ad98fa commit f7cfa8a
1 file changed
Lines changed: 12 additions & 5 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
74 | 74 | | |
75 | 75 | | |
76 | 76 | | |
77 | | - | |
78 | | - | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
79 | 80 | | |
80 | | - | |
| 81 | + | |
81 | 82 | | |
82 | | - | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
83 | 87 | | |
84 | 88 | | |
85 | | - | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
86 | 92 | | |
87 | 93 | | |
88 | 94 | | |
| |||
107 | 113 | | |
108 | 114 | | |
109 | 115 | | |
| 116 | + | |
110 | 117 | | |
111 | 118 | | |
112 | 119 | | |
| |||
0 commit comments