Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions examples/auth-generate-jwt/javascript/src/config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export const config = {
AUTH_URL: import.meta.env.VITE_AUTH_JWT_URL as string,
};
3 changes: 2 additions & 1 deletion examples/auth-generate-jwt/javascript/src/script.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import * as Ably from 'ably';
import './styles.css';
import { config } from './config';

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

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

const realtimeClient = new Ably.Realtime({
authUrl: 'http://localhost:3001/generate-jwt',
authUrl: config.AUTH_URL || 'http://localhost:3001/generate-jwt',
});

const messageTwo = document.getElementById('message-2');
Expand Down
1 change: 1 addition & 0 deletions examples/auth-generate-jwt/javascript/vite-env.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
interface ImportMetaEnv {
readonly VITE_ABLY_KEY: string;
readonly VITE_AUTH_URL: string;
// Add other environment variables here if needed
}

Expand Down
23 changes: 21 additions & 2 deletions examples/auth-generate-jwt/react/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import React, { useState, useEffect } from 'react';
import * as Ably from 'ably';
import { AblyProvider, useConnectionStateListener } from 'ably/react';
import './styles/styles.css';
import { config } from './config';

interface StatusMessage {
id: number;
Expand Down Expand Up @@ -53,10 +54,28 @@ export default function App() {
]);

const handleConnect = async () => {
// Navigate to authenticated page
window.location.href = '/authenticated';
// Update first message
setMessages((prevMessages) => prevMessages.map((msg) => (msg.id === 1 ? { ...msg, success: true } : msg)));

// Initialize Ably client with JWT auth
const realtimeClient = new Ably.Realtime({
authUrl: config.AUTH_URL || 'http://localhost:3001/generate-jwt',
});

// Update second message
setMessages((prevMessages) => prevMessages.map((msg) => (msg.id === 2 ? { ...msg, success: true } : msg)));

setClient(realtimeClient);
};

if (client) {
return (
<AblyProvider client={client}>
<StatusMessages messages={messages} setMessages={setMessages} />
</AblyProvider>
);
}

return (
<div className="flex items-center justify-center min-h-screen bg-gray-100">
<div className="bg-white shadow-md rounded-md p-8 w-[50%] flex flex-col">
Expand Down
3 changes: 3 additions & 0 deletions examples/auth-generate-jwt/react/src/config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export const config = {
AUTH_URL: import.meta.env.VITE_AUTH_JWT_URL as string,
};
1 change: 1 addition & 0 deletions examples/auth-generate-jwt/react/src/env.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

interface ImportMetaEnv {
readonly VITE_ABLY_KEY: string;
readonly VITE_AUTH_JWT_URL: string;
}

interface ImportMeta {
Expand Down
3 changes: 3 additions & 0 deletions examples/auth-request-token/javascript/src/config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export const config = {
AUTH_URL: import.meta.env.VITE_AUTH_TOKEN_URL as string,
};
3 changes: 2 additions & 1 deletion examples/auth-request-token/javascript/src/script.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import * as Ably from 'ably';
import './styles.css';
import { config } from './config';

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

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

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

const messageTwo = document.getElementById('message-2');
Expand Down
1 change: 1 addition & 0 deletions examples/auth-request-token/javascript/vite-env.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
interface ImportMetaEnv {
readonly VITE_ABLY_KEY: string;
readonly VITE_AUTH_TOKEN_URL: string;
// Add other environment variables here if needed
}

Expand Down
23 changes: 21 additions & 2 deletions examples/auth-request-token/react/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import React, { useState, useEffect } from 'react';
import * as Ably from 'ably';
import { AblyProvider, useConnectionStateListener } from 'ably/react';
import './styles/styles.css';
import { config } from './config';

interface StatusMessage {
id: number;
Expand Down Expand Up @@ -53,10 +54,28 @@ export default function App() {
]);

const handleConnect = async () => {
// Navigate to authenticated page
window.location.href = '/authenticated';
// Update first message
setMessages((prevMessages) => prevMessages.map((msg) => (msg.id === 1 ? { ...msg, success: true } : msg)));

// Initialize Ably client with token auth
const realtimeClient = new Ably.Realtime({
authUrl: config.AUTH_URL || 'http://localhost:3001/request-token',
});

// Update second message
setMessages((prevMessages) => prevMessages.map((msg) => (msg.id === 2 ? { ...msg, success: true } : msg)));

setClient(realtimeClient);
};

if (client) {
return (
<AblyProvider client={client}>
<StatusMessages messages={messages} setMessages={setMessages} />
</AblyProvider>
);
}

return (
<div className="flex items-center justify-center min-h-screen bg-gray-100">
<div className="bg-white shadow-md rounded-md p-8 w-[50%] flex flex-col">
Expand Down
3 changes: 3 additions & 0 deletions examples/auth-request-token/react/src/config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export const config = {
AUTH_URL: import.meta.env.VITE_AUTH_TOKEN_URL as string,
};
1 change: 1 addition & 0 deletions examples/auth-request-token/react/src/env.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

interface ImportMetaEnv {
readonly VITE_ABLY_KEY: string;
readonly VITE_AUTH_TOKEN_URL: string;
}

interface ImportMeta {
Expand Down
2 changes: 2 additions & 0 deletions src/components/Examples/ExamplesRenderer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@ const ExamplesRenderer = ({
Object.entries(files).forEach(([languageKey, languageFiles]) => {
result[languageKey as LanguageKey] = updateAblyConnectionKey(languageFiles, apiKey, {
NAME: getRandomChannelName(), // Use CHANNEL_NAME as env var key
AUTH_TOKEN_URL: process.env.VITE_AUTH_TOKEN_URL || '',
AUTH_JWT_URL: process.env.VITE_AUTH_JWT_URL || '',
});
});
return result;
Expand Down
20 changes: 20 additions & 0 deletions src/data/examples/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,26 @@ import { Example } from './types';
export const DEFAULT_EXAMPLE_LANGUAGES = ['javascript', 'react'];

export const examples: Example[] = [
{
id: 'auth-generate-jwt',
name: 'Generate JWT',
description: 'Generate a JSON Web Token (JWT) for authenticating users.',
products: ['auth'],
layout: 'single-horizontal',
visibleFiles: ['src/script.ts', 'App.tsx', 'Chat.tsx', 'Home.tsx', 'index.tsx'],
metaTitle: `Build chat history with Ably's Chat SDK`,
metaDescription: `Implement chat room history with message persistence and replay. Learn how to store, retrieve, and sync past messages in realtime apps.`,
},
{
id: 'auth-request-token',
name: 'Request Token',
description: 'Request an Ably Token for authenticating users.',
products: ['auth'],
layout: 'single-horizontal',
visibleFiles: ['src/script.ts', 'App.tsx', 'Chat.tsx', 'Home.tsx', 'index.tsx'],
metaTitle: `Authenticate with Ably Token`,
metaDescription: `Learn how to request and use Ably Tokens for client authentication. Secure token-based auth for realtime applications.`,
},
{
id: 'chat-presence',
name: 'Chat presence',
Expand Down