-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathroute.ts
More file actions
38 lines (32 loc) · 976 Bytes
/
route.ts
File metadata and controls
38 lines (32 loc) · 976 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
/**
* Ably JWT token endpoint.
*
* Issues short-lived JWTs signed with the Ably API key secret.
* The client connects to Ably with `authUrl` pointing here.
*/
import jwt from 'jsonwebtoken';
import { NextResponse } from 'next/server';
export async function GET(req: Request) {
const apiKey = process.env.ABLY_API_KEY;
if (!apiKey) {
return NextResponse.json({ error: 'ABLY_API_KEY not set' }, { status: 500 });
}
const [keyName, keySecret] = apiKey.split(':');
const url = new URL(req.url);
const clientId = url.searchParams.get('clientId') ?? `user-${crypto.randomUUID().slice(0, 8)}`;
const token = jwt.sign(
{
'x-ably-clientId': clientId,
'x-ably-capability': JSON.stringify({ '*': ['publish', 'subscribe', 'history'] }),
},
keySecret,
{
algorithm: 'HS256',
keyid: keyName,
expiresIn: '1h',
},
);
return new NextResponse(token, {
headers: { 'Content-Type': 'application/jwt' },
});
}