Skip to content

Commit 6d6988d

Browse files
committed
docs(examples/tutorial): fetch tokens from pronto.getstream.io
Mirror the pattern used by examples/vite: credentials.ts now exports apiKey, userId, userName, and a tokenProvider function that mints a fresh JWT from pronto for whichever user_id is active. The app stays runnable without pasting a token that expires, and users can switch identities at runtime via ?user_id=... / ?user_name=... URL params. - credentials.ts: drop the static userToken export; add a tokenProvider() that hits VITE_TOKEN_ENDPOINT (default https://pronto.getstream.io/api/auth/create-token) with VITE_TOKEN_ENVIRONMENT (default "demo") and the active user_id. userId/userName now derive from URL params first, then env, then sensible defaults. - All 7 step App.tsx files: rename the userToken import and tokenOrProvider arg to tokenProvider. - .env.example: drop VITE_USER_TOKEN; document the new optional VITE_TOKEN_ENDPOINT / VITE_TOKEN_ENVIRONMENT overrides and the URL-param fallbacks.
1 parent 756ac6d commit 6d6988d

9 files changed

Lines changed: 71 additions & 21 deletions

File tree

examples/tutorial/.env.example

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,14 @@
1+
# Required: your Stream app's public key.
12
VITE_API_KEY=REPLACE_WITH_API_KEY
2-
VITE_USER_ID=REPLACE_WITH_USER_ID
3-
VITE_USER_NAME=REPLACE_WITH_USER_NAME
4-
VITE_USER_TOKEN=REPLACE_WITH_USER_TOKEN
3+
4+
# Optional. If unset, the app defaults to user_id "react-tutorial" and
5+
# derives user_name from it. You can also override either value per-run
6+
# via URL params: ?user_id=alice&user_name=Alice
7+
# VITE_USER_ID=react-tutorial
8+
# VITE_USER_NAME=React Tutorial
9+
10+
# Optional. Token endpoint used to mint a fresh JWT for the active
11+
# user_id. Defaults to Stream's demo endpoint; override if you're
12+
# pointing at a different Stream app / token service.
13+
# VITE_TOKEN_ENDPOINT=https://pronto.getstream.io/api/auth/create-token
14+
# VITE_TOKEN_ENVIRONMENT=demo

examples/tutorial/src/1-client-setup/App.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import { Chat, useCreateChatClient } from 'stream-chat-react';
2-
import { apiKey, userId, userName, userToken } from './credentials';
2+
import { apiKey, userId, userName, tokenProvider } from './credentials';
33

44
const App = () => {
55
const client = useCreateChatClient({
66
apiKey,
7-
tokenOrProvider: userToken,
7+
tokenOrProvider: tokenProvider,
88
userData: { id: userId, name: userName },
99
});
1010

Lines changed: 44 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,45 @@
1-
// your Stream app information
1+
// Stream Chat credentials for the tutorial example.
2+
//
3+
// The example fetches a fresh JWT from pronto.getstream.io for whichever
4+
// user_id is active, so the app stays runnable without pasting a token
5+
// that expires, and you can switch users via URL params at runtime:
6+
//
7+
// ?user_id=alice // different user
8+
// ?user_id=alice&user_name=Alice // + display name override
9+
//
10+
// Notes:
11+
// - apiKey is the one thing you still need to set (via VITE_API_KEY).
12+
// - The token endpoint and environment default to the values shared with
13+
// the other example apps in this repo; override with VITE_TOKEN_ENDPOINT
14+
// and VITE_TOKEN_ENVIRONMENT if you're pointing at a different Stream
15+
// app.
16+
17+
const searchParams = new URLSearchParams(window.location.search);
18+
219
export const apiKey = import.meta.env.VITE_API_KEY;
3-
export const userId = import.meta.env.VITE_USER_ID;
4-
export const userName = import.meta.env.VITE_USER_NAME;
5-
export const userToken = import.meta.env.VITE_USER_TOKEN;
20+
21+
export const userId =
22+
searchParams.get('user_id') || import.meta.env.VITE_USER_ID || 'react-tutorial';
23+
24+
export const userName =
25+
searchParams.get('user_name') || import.meta.env.VITE_USER_NAME || userId;
26+
27+
const tokenEndpoint =
28+
import.meta.env.VITE_TOKEN_ENDPOINT ||
29+
'https://pronto.getstream.io/api/auth/create-token';
30+
const tokenEnvironment = import.meta.env.VITE_TOKEN_ENVIRONMENT || 'demo';
31+
32+
// Stream's `useCreateChatClient` accepts either a token string or a provider
33+
// function. A provider lets the SDK refresh the token on reconnect, which is
34+
// what we want for a long-running example session.
35+
export const tokenProvider = async (): Promise<string> => {
36+
const url = `${tokenEndpoint}?environment=${encodeURIComponent(
37+
tokenEnvironment,
38+
)}&user_id=${encodeURIComponent(userId)}`;
39+
const response = await fetch(url);
40+
if (!response.ok) {
41+
throw new Error(`Failed to mint token from ${tokenEndpoint} (${response.status})`);
42+
}
43+
const data = (await response.json()) as { token: string };
44+
return data.token;
45+
};

examples/tutorial/src/2-core-component-setup/App.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import {
1313

1414
import 'stream-chat-react/dist/css/index.css';
1515
import './layout.css';
16-
import { apiKey, userId, userName, userToken } from '../1-client-setup/credentials';
16+
import { apiKey, userId, userName, tokenProvider } from '../1-client-setup/credentials';
1717

1818
const user: User = {
1919
id: userId,
@@ -25,7 +25,7 @@ const App = () => {
2525
const [channel, setChannel] = useState<StreamChannel>();
2626
const client = useCreateChatClient({
2727
apiKey,
28-
tokenOrProvider: userToken,
28+
tokenOrProvider: tokenProvider,
2929
userData: user,
3030
});
3131

examples/tutorial/src/3-channel-list/App.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import {
1212
} from 'stream-chat-react';
1313

1414
import './layout.css';
15-
import { apiKey, userId, userName, userToken } from '../1-client-setup/credentials';
15+
import { apiKey, userId, userName, tokenProvider } from '../1-client-setup/credentials';
1616

1717
const user: User = {
1818
id: userId,
@@ -32,7 +32,7 @@ const options: ChannelOptions = {
3232
const App = () => {
3333
const client = useCreateChatClient({
3434
apiKey,
35-
tokenOrProvider: userToken,
35+
tokenOrProvider: tokenProvider,
3636
userData: user,
3737
});
3838

examples/tutorial/src/4-custom-ui-components/App.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import {
1717
} from 'stream-chat-react';
1818

1919
import './layout.css';
20-
import { apiKey, userId, userName, userToken } from '../1-client-setup/credentials';
20+
import { apiKey, userId, userName, tokenProvider } from '../1-client-setup/credentials';
2121

2222
const user: User = {
2323
id: userId,
@@ -118,7 +118,7 @@ const App = () => {
118118
const [isReady, setIsReady] = useState(false);
119119
const client = useCreateChatClient({
120120
apiKey,
121-
tokenOrProvider: userToken,
121+
tokenOrProvider: tokenProvider,
122122
userData: user,
123123
});
124124

examples/tutorial/src/5-custom-attachment-type/App.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import {
1919
} from 'stream-chat-react';
2020

2121
import './layout.css';
22-
import { apiKey, userId, userName, userToken } from '../1-client-setup/credentials';
22+
import { apiKey, userId, userName, tokenProvider } from '../1-client-setup/credentials';
2323

2424
const user: User = {
2525
id: userId,
@@ -76,7 +76,7 @@ const App = () => {
7676
const [channel, setChannel] = useState<StreamChannel>();
7777
const client = useCreateChatClient({
7878
apiKey,
79-
tokenOrProvider: userToken,
79+
tokenOrProvider: tokenProvider,
8080
userData: user,
8181
});
8282

examples/tutorial/src/6-emoji-picker/App.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import { init, SearchIndex } from 'emoji-mart';
1818
import data from '@emoji-mart/data';
1919

2020
import './layout.css';
21-
import { apiKey, userId, userName, userToken } from '../1-client-setup/credentials';
21+
import { apiKey, userId, userName, tokenProvider } from '../1-client-setup/credentials';
2222

2323
const user: User = {
2424
id: userId,
@@ -38,7 +38,7 @@ const App = () => {
3838
const [isReady, setIsReady] = useState(false);
3939
const client = useCreateChatClient({
4040
apiKey,
41-
tokenOrProvider: userToken,
41+
tokenOrProvider: tokenProvider,
4242
userData: user,
4343
});
4444

examples/tutorial/src/7-livestream/App.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import {
1111
} from 'stream-chat-react';
1212

1313
import './layout.css';
14-
import { apiKey, userId, userName, userToken } from '../1-client-setup/credentials';
14+
import { apiKey, userId, userName, tokenProvider } from '../1-client-setup/credentials';
1515

1616
const user: User = {
1717
id: userId,
@@ -23,7 +23,7 @@ const App = () => {
2323
const [channel, setChannel] = useState<StreamChannel>();
2424
const chatClient = useCreateChatClient({
2525
apiKey,
26-
tokenOrProvider: userToken,
26+
tokenOrProvider: tokenProvider,
2727
userData: user,
2828
});
2929

0 commit comments

Comments
 (0)