Skip to content

Commit e67b6d9

Browse files
committed
Chore: Update package deps and correct token generation
- Update TypeScript configuration to support `react-jsx`. - Add `"use client"` directive to React pages for client-side rendering. - Update dependencies in `package-lock.json`, including `ably`, `eslint`, and `next.js` versions. - Refactor Next.js configuration: replace `domains` with `remotePatterns` for image handling and adjust server package settings. -Updated token generation to use the preferred JWT method.
1 parent 8f7fead commit e67b6d9

File tree

9 files changed

+3202
-1544
lines changed

9 files changed

+3202
-1544
lines changed

app/authentication/page.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
* Warning: Opening too many live preview tabs will slow down performance.
33
* We recommend closing them after you're done.
44
*/
5+
'use client'
6+
57
import React from "react";
68
import "../global.css";
79
import dynamic from 'next/dynamic';

app/history/page.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
* Warning: Opening too many live preview tabs will slow down performance.
33
* We recommend closing them after you're done.
44
*/
5+
'use client'
6+
57
import React from "react";
68
import "../global.css";
79
import dynamic from 'next/dynamic';

app/presence/page.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
* Warning: Opening too many live preview tabs will slow down performance.
33
* We recommend closing them after you're done.
44
*/
5+
'use client'
6+
57
import React from "react";
68
import "../global.css";
79
import dynamic from 'next/dynamic';

app/pub-sub/page.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
* Warning: Opening too many live preview tabs will slow down performance.
33
* We recommend closing them after you're done.
44
*/
5+
'use client'
6+
57
import React from "react";
68
import "../global.css";
79
import dynamic from 'next/dynamic';

app/token/route.ts

Lines changed: 37 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
import { NextRequest, NextResponse } from 'next/server'
2-
import * as Ably from "ably";
2+
import jwt from 'jsonwebtoken';
33

44
export async function POST(req: Request) {
55
if (!process.env.ABLY_API_KEY) {
66
return NextResponse.json({ errorMessage: `Missing ABLY_API_KEY environment variable.
77
If you're running locally, please ensure you have a ./.env file with a value for ABLY_API_KEY=your-key.
8-
If you're running in Netlify, make sure you've configured env variable ABLY_API_KEY.
8+
If you're running in Netlify, make sure you've configured env variable ABLY_API_KEY.
99
Please see README.md for more details on configuring your Ably API Key.`,
10-
},{
10+
},{
1111
status: 500,
1212
headers: new Headers({
1313
"content-type": "application/json"
@@ -16,8 +16,38 @@ export async function POST(req: Request) {
1616
}
1717

1818
const clientId = ( (await req.formData()).get('clientId')?.toString() ) || process.env.DEFAULT_CLIENT_ID || "NO_CLIENT_ID";
19-
const client = new Ably.Rest(process.env.ABLY_API_KEY);
20-
const tokenRequestData = await client.auth.createTokenRequest({ clientId: clientId });
21-
console.log(tokenRequestData)
22-
return NextResponse.json(tokenRequestData)
19+
20+
// Parse the API key to extract keyName and keySecret
21+
// Ably API key format: appId.keyId:keySecret
22+
const apiKey = process.env.ABLY_API_KEY;
23+
const [keyName, keySecret] = apiKey.split(':');
24+
25+
if (!keyName || !keySecret) {
26+
return NextResponse.json({
27+
errorMessage: 'Invalid ABLY_API_KEY format. Expected format: appId.keyId:keySecret'
28+
}, {
29+
status: 500,
30+
headers: new Headers({
31+
"content-type": "application/json"
32+
})
33+
});
34+
}
35+
36+
// Create JWT claims
37+
const currentTime = Math.floor(Date.now() / 1000);
38+
const claims = {
39+
'x-ably-capability': JSON.stringify({ '*': ['*'] }), // Full capabilities - adjust as needed
40+
'x-ably-clientId': clientId,
41+
'iat': currentTime,
42+
'exp': currentTime + 3600 // Token expires in 1 hour
43+
};
44+
45+
// Sign the JWT with the key secret
46+
const token = jwt.sign(claims, keySecret, {
47+
keyid: keyName,
48+
algorithm: 'HS256'
49+
});
50+
51+
console.log('Generated JWT token for client:', clientId);
52+
return NextResponse.json(token);
2353
}

next.config.js

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,16 @@ const nextConfig = {
44
// useEffect with no dependencies to ensure the function
55
// is only run once and only run on the client.
66
reactStrictMode: false,
7-
swcMinify: true,
87
images: {
98
dangerouslyAllowSVG: true,
10-
domains: ['static.ably.dev'],
11-
},
12-
experimental: {
13-
serverComponentsExternalPackages: ['ably'],
9+
remotePatterns: [
10+
{
11+
protocol: 'https',
12+
hostname: 'static.ably.dev',
13+
},
14+
],
1415
},
16+
serverExternalPackages: ['ably'],
1517
};
1618

1719
module.exports = nextConfig;

0 commit comments

Comments
 (0)