Skip to content

Commit d53fd73

Browse files
committed
Update and enable Auth Token example
1 parent a91c12b commit d53fd73

File tree

6 files changed

+31
-3
lines changed

6 files changed

+31
-3
lines changed
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export const config = {
2+
AUTH_URL: import.meta.env.VITE_AUTH_TOKEN_URL as string,
3+
};

examples/auth-request-token/javascript/src/script.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import * as Ably from 'ably';
22
import './styles.css';
3+
import { config } from './config';
34

45
const connectButton = document.querySelector('button') as HTMLButtonElement;
56

@@ -14,7 +15,7 @@ function handleConnect() {
1415
messageOne.textContent = '✓';
1516

1617
const realtimeClient = new Ably.Realtime({
17-
authUrl: 'http://localhost:3001/request-token',
18+
authUrl: config.AUTH_URL || 'http://localhost:3001/request-token',
1819
});
1920

2021
const messageTwo = document.getElementById('message-2');

examples/auth-request-token/javascript/vite-env.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
interface ImportMetaEnv {
22
readonly VITE_ABLY_KEY: string;
3+
readonly VITE_AUTH_TOKEN_URL: string;
34
// Add other environment variables here if needed
45
}
56

examples/auth-request-token/react/src/App.tsx

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import React, { useState, useEffect } from 'react';
22
import * as Ably from 'ably';
33
import { AblyProvider, useConnectionStateListener } from 'ably/react';
44
import './styles/styles.css';
5+
import { config } from './config';
56

67
interface StatusMessage {
78
id: number;
@@ -53,10 +54,28 @@ export default function App() {
5354
]);
5455

5556
const handleConnect = async () => {
56-
// Navigate to authenticated page
57-
window.location.href = '/authenticated';
57+
// Update first message
58+
setMessages((prevMessages) => prevMessages.map((msg) => (msg.id === 1 ? { ...msg, success: true } : msg)));
59+
60+
// Initialize Ably client with token auth
61+
const realtimeClient = new Ably.Realtime({
62+
authUrl: config.AUTH_URL || 'http://localhost:3001/request-token',
63+
});
64+
65+
// Update second message
66+
setMessages((prevMessages) => prevMessages.map((msg) => (msg.id === 2 ? { ...msg, success: true } : msg)));
67+
68+
setClient(realtimeClient);
5869
};
5970

71+
if (client) {
72+
return (
73+
<AblyProvider client={client}>
74+
<StatusMessages messages={messages} setMessages={setMessages} />
75+
</AblyProvider>
76+
);
77+
}
78+
6079
return (
6180
<div className="flex items-center justify-center min-h-screen bg-gray-100">
6281
<div className="bg-white shadow-md rounded-md p-8 w-[50%] flex flex-col">
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export const config = {
2+
AUTH_URL: import.meta.env.VITE_AUTH_TOKEN_URL as string,
3+
};

examples/auth-request-token/react/src/env.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
interface ImportMetaEnv {
44
readonly VITE_ABLY_KEY: string;
5+
readonly VITE_AUTH_TOKEN_URL: string;
56
}
67

78
interface ImportMeta {

0 commit comments

Comments
 (0)