Skip to content
Merged
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
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
3 changes: 1 addition & 2 deletions examples/.env.example
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
VITE_PUBLIC_ABLY_KEY=
NEXT_PUBLIC_ABLY_KEY=
VITE_ABLY_KEY=
4 changes: 2 additions & 2 deletions examples/auth-generate-jwt/javascript/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ cd /examples/
mv .env.example .env.local
```

7. In `.env.local` update the value of `VITE_PUBLIC_ABLY_KEY` to be your Ably API key.
7. In `.env.local` update the value of `VITE_ABLY_KEY` to be your Ably API key.

8. Install dependencies:

Expand All @@ -72,4 +72,4 @@ yarn run auth-generate-jwt-server

## Open in CodeSandbox

In CodeSandbox, rename the `.env.example` file to `.env.local` and update the value of your `VITE_PUBLIC_ABLY_KEY` variable to use your Ably API key.
In CodeSandbox, rename the `.env.example` file to `.env.local` and update the value of your `VITE_ABLY_KEY` variable to use your Ably API key.
7 changes: 0 additions & 7 deletions examples/auth-generate-jwt/javascript/postcss.config.ts

This file was deleted.

1 change: 1 addition & 0 deletions examples/auth-generate-jwt/javascript/tailwind.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import type { Config } from 'tailwindcss';

const config: Config = {
...baseConfig,
content: ['./src/**/*.{js,ts,tsx}', './index.html'],
};

export default config;
2 changes: 1 addition & 1 deletion examples/auth-generate-jwt/javascript/vite-env.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
interface ImportMetaEnv {
readonly VITE_PUBLIC_ABLY_KEY: string;
readonly VITE_ABLY_KEY: string;
// Add other environment variables here if needed
}

Expand Down
7 changes: 2 additions & 5 deletions examples/auth-generate-jwt/javascript/vite.config.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
import baseConfig from '../../vite.config';
import { defineConfig } from 'vite';
import dotenv from 'dotenv';
import path from 'path';

dotenv.config({ path: path.resolve(__dirname, '../../.env.local') });
import baseConfig from '../../vite.config';

export default defineConfig({
...baseConfig,
envDir: '../../',
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was a more vite-y approach than dotenv

});
4 changes: 2 additions & 2 deletions examples/auth-generate-jwt/react/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ cd /examples/
mv .env.example .env.local
```

7. In `.env.local` update the value of `VITE_PUBLIC_ABLY_KEY` to be your Ably API key.
7. In `.env.local` update the value of `VITE_ABLY_KEY` to be your Ably API key.

8. Install dependencies:

Expand All @@ -72,4 +72,4 @@ yarn run auth-generate-jwt-server

## Open in CodeSandbox

In CodeSandbox, rename the `.env.example` file to `.env.local` and update the value of your `VITE_PUBLIC_ABLY_KEY` variable to use your Ably API key.
In CodeSandbox, rename the `.env.example` file to `.env.local` and update the value of your `VITE_ABLY_KEY` variable to use your Ably API key.
12 changes: 12 additions & 0 deletions examples/auth-generate-jwt/react/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Ably Example</title>
</head>
<body>
<div id="root"></div>
<script type="module" src="/src/index.tsx"></script>
</body>
</html>
4 changes: 0 additions & 4 deletions examples/auth-generate-jwt/react/next.config.ts

This file was deleted.

6 changes: 3 additions & 3 deletions examples/auth-generate-jwt/react/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
"version": "0.1.0",
"private": true,
"scripts": {
"dev": "next dev",
"build": "next build",
"start": "next start",
"dev": "vite",
"build": "tsc && vite build",
"preview": "vite preview",
"lint": "next lint"
}
}
8 changes: 0 additions & 8 deletions examples/auth-generate-jwt/react/postcss.config.ts

This file was deleted.

84 changes: 84 additions & 0 deletions examples/auth-generate-jwt/react/src/App.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
import React, { useState, useEffect } from 'react';
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wouldn't look too closely at the auth-* projects. They're not in scope to support just yet.

import * as Ably from 'ably';
import { AblyProvider, useConnectionStateListener } from 'ably/react';
import './styles/styles.css';

interface StatusMessage {
id: number;
success: boolean;
message: string;
}

interface StatusMessagesProps {
messages: StatusMessage[];
setMessages: React.Dispatch<React.SetStateAction<StatusMessage[]>>;
}

const StatusMessages = ({ messages, setMessages }: StatusMessagesProps) => {
useConnectionStateListener((stateChange) => {
if (stateChange.current === 'connected') {
setMessages((prevMessages) => prevMessages.map((msg) => (msg.id === 3 ? { ...msg, success: true } : msg)));
}
});

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">
<div className="flex-grow text-center">
<h1 className="text-2xl font-bold mb-4">Authentication to Ably with a JWT</h1>
<p className="mb-8">The Ably client has been successfully initialized.</p>
</div>

<div className="mt-4 text-left text-sm h-40 overflow-y-auto">
{messages.map((msg) => (
<div key={msg.id} className="flex items-center gap-2">
<span>
{msg.success ? <span className="text-green-500">✓</span> : <span className="text-red-500">✗</span>}
</span>
{msg.message}
</div>
))}
</div>
</div>
</div>
);
};

export default function App() {
const [client, setClient] = useState<Ably.Realtime | null>(null);
const [messages, setMessages] = useState<StatusMessage[]>([
{ id: 1, success: false, message: 'Client requests JWT from server' },
{ id: 2, success: false, message: 'Client initializes connection to Ably with generated JWT' },
{ id: 3, success: false, message: 'Client is connected' },
]);

const handleConnect = async () => {
// Navigate to authenticated page
window.location.href = '/authenticated';
};

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">
<div className="flex-grow text-center">
<h1 className="text-2xl font-bold mb-4">Authentication to Ably with a JWT</h1>
<p>Press the Connect button to initialize the client:</p>
<button onClick={handleConnect} className="uk-btn uk-btn-md uk-btn-primary mb-4 rounded">
Connect
</button>
</div>

<div className="mt-4 text-left text-sm h-40 overflow-y-auto">
{messages.map((msg, index) => (
<div key={msg.id} className="flex items-center gap-2">
<span>
{msg.success ? <span className="text-green-500">✓</span> : <span className="text-red-500">✗</span>}
</span>
{msg.message}
</div>
))}
</div>
</div>
</div>
);
}
89 changes: 0 additions & 89 deletions examples/auth-generate-jwt/react/src/app/authenticated/page.tsx

This file was deleted.

31 changes: 0 additions & 31 deletions examples/auth-generate-jwt/react/src/app/layout.tsx

This file was deleted.

55 changes: 0 additions & 55 deletions examples/auth-generate-jwt/react/src/app/page.tsx

This file was deleted.

9 changes: 9 additions & 0 deletions examples/auth-generate-jwt/react/src/env.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/// <reference types="vite/client" />

interface ImportMetaEnv {
readonly VITE_ABLY_KEY: string;
}

interface ImportMeta {
readonly env: ImportMetaEnv;
}
9 changes: 9 additions & 0 deletions examples/auth-generate-jwt/react/src/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import React from 'react';
import ReactDOM from 'react-dom/client';
import App from './App';

ReactDOM.createRoot(document.getElementById('root')!).render(
<React.StrictMode>
<App />
</React.StrictMode>,
);
Loading