-
Notifications
You must be signed in to change notification settings - Fork 41
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add generate JWT authentication example
- Loading branch information
1 parent
fb70c3c
commit 8bed8ce
Showing
31 changed files
with
6,515 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
VITE_PUBLIC_ABLY_KEY= |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files. | ||
|
||
# dependencies | ||
/node_modules | ||
/.pnp | ||
.pnp.js | ||
.yarn/install-state.gz | ||
|
||
# testing | ||
/coverage | ||
|
||
# next.js | ||
/.next/ | ||
/out/ | ||
|
||
# production | ||
/build | ||
|
||
# misc | ||
.DS_Store | ||
*.pem | ||
|
||
# debug | ||
npm-debug.log* | ||
yarn-debug.log* | ||
yarn-error.log* | ||
|
||
# local env files | ||
.env*.local | ||
|
||
# vercel | ||
.vercel | ||
|
||
# typescript | ||
*.tsbuildinfo | ||
next-env.d.ts |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
# Generate a JWT token to authenticate clients | ||
|
||
This folder contains the code for the authentication (Typescript) - a demo of how you can authenticate with [Ably](https://ably.com/docs/auth) to use any of the products. | ||
|
||
## Getting started | ||
|
||
1. Clone the [Ably docs](https://github.com/ably/docs) repository where this example can be found: | ||
|
||
```sh | ||
git clone [email protected]:ably/docs.git | ||
``` | ||
|
||
2. Change directory: | ||
|
||
```sh | ||
cd /examples/auth-generate-token/javascript/ | ||
``` | ||
|
||
3. Install dependencies: | ||
|
||
```sh | ||
yarn install | ||
``` | ||
|
||
4. Run the frontend client: | ||
|
||
```sh | ||
yarn run dev | ||
``` | ||
|
||
5. In a new tab, change directory: | ||
|
||
```sh | ||
cd /examples/auth-generate-token/server/ | ||
``` | ||
|
||
6. Rename the environment file: | ||
|
||
```sh | ||
mv .env.example .env.local | ||
``` | ||
|
||
7. In `.env.local` update the value of `VITE_PUBLIC_ABLY_KEY` to be your Ably API key. | ||
|
||
8. Install dependencies: | ||
|
||
```sh | ||
yarn install | ||
``` | ||
|
||
9. Run the backend server: | ||
|
||
```sh | ||
yarn run dev | ||
``` | ||
|
||
10. Try it out by opening two tabs to [http://localhost:5173/](http://localhost:5173/) with your browser to see the result. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
<head> | ||
<meta charset="UTF-8"> | ||
<meta name="viewport" content="width=device-width, initial-scale=1.0"> | ||
<link href='https://fonts.googleapis.com/css?family=Inter' rel='stylesheet'> | ||
<link rel="stylesheet" href="src/styles.css" /> | ||
<title>Generate JWT</title> | ||
</head> | ||
<body class="font-inter"> | ||
<div class="flex items-center justify-center min-h-screen bg-gray-100"> | ||
<div class="bg-white shadow-md rounded-md p-8 w-[50%] flex flex-col"> | ||
<div class="flex-grow text-center"> | ||
<h1 class="text-2xl font-bold mb-4"> | ||
Welcome to Ably Token Manager | ||
</h1> | ||
<p class="mb-8"> | ||
Press the Connect button to initialize the client: | ||
</p> | ||
<button class="px-4 py-2 bg-blue-500 text-white rounded hover:bg-blue-600"> | ||
Connect | ||
</button> | ||
</div> | ||
<div class="mt-4 text-left text-sm h-40 overflow-y-auto"> | ||
<ul id="messages"></ul> | ||
</div> | ||
<div class="mt-4 flex flex-col items-center justify-center h-16"> | ||
<h2 id="header-initialized" class="text-lg font-bold" style="display: none;">Client Initialized</h2> | ||
<p id="paragraph-initialized" style="display: none;">The Ably client has been successfully initialized.</p> | ||
</div> | ||
</div> | ||
</div> | ||
<script type="module" src="src/script.ts"></script> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
{ | ||
"name": "auth-generate-token", | ||
"version": "1.0.0", | ||
"main": "index.js", | ||
"license": "MIT", | ||
"scripts": { | ||
"dev": "vite", | ||
"build": "tsc && vite build", | ||
"preview": "vite preview" | ||
}, | ||
"devDependencies": { | ||
"autoprefixer": "^10.4.20", | ||
"dotenv": "^16.4.5", | ||
"postcss": "^8.4.47", | ||
"tailwindcss": "^3.4.13", | ||
"typescript": "^5.6.3" | ||
}, | ||
"dependencies": { | ||
"ably": "^2.3.1", | ||
"nanoid": "^5.0.7", | ||
"vite": "^5.4.2" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import * as Ably from 'ably'; | ||
import './styles.css'; | ||
|
||
const connectButton = document.querySelector('button') as HTMLButtonElement; | ||
|
||
connectButton.addEventListener('click', () => { | ||
handleConnect(); | ||
}); | ||
|
||
function handleConnect() { | ||
// Add your connection logic here | ||
console.log('Connect button clicked'); | ||
|
||
const messagesUl = document.getElementById('messages'); | ||
const newMessage = document.createElement('li'); | ||
newMessage.className = 'border-b py-1'; | ||
newMessage.textContent = '1. Client requests token from server'; | ||
messagesUl.appendChild(newMessage); | ||
|
||
try { | ||
const realtimeClient = new Ably.Realtime({ | ||
authUrl: 'http://localhost:3001/auth-url', | ||
}); | ||
|
||
const clientInializedMessage = document.createElement('li'); | ||
clientInializedMessage.className = 'border-b py-1'; | ||
clientInializedMessage.textContent = '2. Client initializes connection to Ably with generated Token'; | ||
messagesUl.appendChild(clientInializedMessage); | ||
|
||
const headerInitialized = document.getElementById('header-initialized'); | ||
const paragraphInitialized = document.getElementById('paragraph-initialized'); | ||
|
||
headerInitialized.style.display = 'block'; | ||
paragraphInitialized.style.display = 'block'; | ||
} catch (error) { | ||
const clientFailedMessage = document.createElement('li'); | ||
clientFailedMessage.className = 'border-b py-1'; | ||
clientFailedMessage.textContent = '3. Failed to initialise client. Please try again.'; | ||
messagesUl.appendChild(clientFailedMessage); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
@tailwind base; | ||
@tailwind components; | ||
@tailwind utilities; | ||
|
||
.container { | ||
width: 100%; | ||
display: flex; | ||
justify-content: center; | ||
align-items: center; | ||
position: relative; | ||
background-color: #f4f8fb; | ||
height: 100vh; | ||
} | ||
|
||
input { | ||
padding: 0.5rem; | ||
width: 100%; | ||
height: 2.5rem; | ||
font-size: 0.875rem; | ||
border-radius: 0.375rem; | ||
outline: none; | ||
transition: all 0.2s ease-in-out; | ||
} |
12 changes: 12 additions & 0 deletions
12
examples/auth-generate-token/javascript/tailwind.config.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
/** @type {import('tailwindcss').Config} */ | ||
module.exports = { | ||
content: [ | ||
"./index.html", | ||
"./src/**/*.{js,jsx,ts,tsx}", | ||
], | ||
theme: { | ||
extend: {}, | ||
}, | ||
plugins: [], | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
{ | ||
"compilerOptions": { | ||
"lib": ["dom", "es2015"], | ||
"outDir": "./lib/cjs/", | ||
"sourceMap": true, | ||
"declaration": true, | ||
"noImplicitAny": true, | ||
"module": "commonjs", | ||
"target": "es2017", | ||
"allowJs": true, | ||
"moduleResolution": "node" | ||
}, | ||
"include": ["src/**/*.ts*"], | ||
"exclude": ["node_modules", "dist", "lib"] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
interface ImportMetaEnv { | ||
readonly VITE_PUBLIC_ABLY_KEY: string; | ||
// Add other environment variables here if needed | ||
} | ||
|
||
interface ImportMeta { | ||
readonly env: ImportMetaEnv; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import { defineConfig } from 'vite' | ||
|
||
export default defineConfig({ | ||
css: { | ||
postcss: { | ||
plugins: [ | ||
require('tailwindcss'), | ||
require('autoprefixer'), | ||
], | ||
} | ||
} | ||
}) |
Oops, something went wrong.