diff --git a/examples/auth-generate-token/react/src/app/authenticated/page.tsx b/examples/auth-generate-token/react/src/app/authenticated/page.tsx new file mode 100644 index 0000000000..d0804f765a --- /dev/null +++ b/examples/auth-generate-token/react/src/app/authenticated/page.tsx @@ -0,0 +1,89 @@ +'use client'; + +import * as Ably from 'ably'; +import { AblyProvider, useConnectionStateListener } from 'ably/react'; +import { useState, useEffect } from 'react'; + +interface StatusMessage { + id: number; + success: boolean; + message: string; +} +interface StatusMessagesProps { + messages: StatusMessage[]; + setMessages: React.Dispatch>; +} + +const StatusMessages = ({ messages, setMessages }: StatusMessagesProps) => { + useConnectionStateListener((stateChange) => { + if (stateChange.current === 'connected') { + setMessages(prevMessages => + prevMessages.map(msg => + msg.id === 3 ? { ...msg, success: true } : msg + ) + ); + } + }); + + return ( +
+
+
+

Authentication with Ably

+

The Ably client has been successfully initialized.

+
+ +
+ {messages.map((msg) => ( +
+ + {msg.success ? + : + + } + + {msg.message} +
+ ))} +
+
+
+ ); +}; + +export default function Authenticated() { + const [messages, setMessages] = useState([ + { id: 1, success: false, message: "Client requests token from server" }, + { id: 2, success: false, message: "Client initializes connection to Ably with generated Token" }, + { id: 3, success: false, message: "Client is connected"} + ]); + const [client, setClient] = useState(null); + + useEffect(() => { + try { + setMessages(prevMessages => + prevMessages.map(msg => + msg.id === 1 ? { ...msg, success: true } : msg + ) + ); + + const newClient = new Ably.Realtime({ authUrl: 'http://localhost:3001/request-token' }); + setClient(newClient); + setMessages(prevMessages => + prevMessages.map(msg => + msg.id === 2 ? { ...msg, success: true } : msg + ) + ); + } catch (error) { + console.error('Failed to initialise client:', error); + } + }, []); + + if (!client) return null; + + return ( + + + + ); +} diff --git a/examples/auth-generate-token/react/src/app/page.tsx b/examples/auth-generate-token/react/src/app/page.tsx index 9cbc422f5d..39ecd1965f 100644 --- a/examples/auth-generate-token/react/src/app/page.tsx +++ b/examples/auth-generate-token/react/src/app/page.tsx @@ -2,6 +2,7 @@ import * as Ably from 'ably'; import { useState } from 'react'; +import { useRouter } from 'next/navigation'; interface StatusMessage { id: number; @@ -16,39 +17,9 @@ export default function Home() { { id: 2, success: false, message: "Client initializes connection to Ably with generated Token" }, { id: 3, success: false, message: "Client is connected"} ]); - const [isLoading, setIsLoading] = useState(false); - + const router = useRouter(); const handleConnect = async () => { - setIsLoading(true); - - setMessages(prevMessages => - prevMessages.map(msg => - msg.id === 1 ? { ...msg, success: true } : msg - ) - ); - - try { - const ablyClient = new Ably.Realtime({ authUrl: 'http://localhost:3001/request-token' }); - setClient(ablyClient); - - setMessages(prevMessages => - prevMessages.map(msg => - msg.id === 2 ? { ...msg, success: true } : msg - ) - ); - - ablyClient.connection.on('connected', (stateChange) => { - setMessages(prevMessages => - prevMessages.map(msg => - msg.id === 3 ? { ...msg, success: true } : msg - ) - ); - }); - } catch (error) { - console.error('Failed to initialise client:', error); - } finally { - setIsLoading(false); - } + router.push('/authenticated'); }; return ( @@ -56,17 +27,12 @@ export default function Home() {

Authentication with Ably

- {client ? ( -

The Ably client has been successfully initialized.

- ) : ( -

Press the Connect button to initialize the client:

- )} +

Press the Connect button to initialize the client: