pollo/app/api/internal/ably/route.ts

53 lines
1.7 KiB
TypeScript
Raw Normal View History

2023-09-24 23:49:24 -06:00
import { NextResponse } from "next/server";
import { env } from "env.mjs";
2023-10-02 19:29:35 -06:00
import { currentUser } from "@clerk/nextjs/server";
import type { AblyTokenResponse } from "@/_utils/types";
export const runtime = "edge";
2023-09-24 23:49:24 -06:00
async function handler() {
const user = await currentUser();
if (!env.ABLY_API_KEY) {
return new Response(
`Missing ABLY_API_KEY environment variable.
If you're running locally, please ensure you have a ./.env file with a value for ABLY_API_KEY=your-key.
If you're running in Netlify, make sure you've configured env variable ABLY_API_KEY.
Please see README.md for more details on configuring your Ably API Key.`,
{
status: 500,
statusText: `Missing ABLY_API_KEY environment variable.
If you're running locally, please ensure you have a ./.env file with a value for ABLY_API_KEY=your-key.
If you're running in Netlify, make sure you've configured env variable ABLY_API_KEY.
Please see README.md for more details on configuring your Ably API Key.`,
}
);
}
2023-10-02 19:29:35 -06:00
const keyName = env.ABLY_API_KEY.split(":")[0];
const tokenResponse = await fetch(
`https://rest.ably.io/keys/${keyName}/requestToken`,
{
method: "POST",
headers: {
"Content-Type": "application/json",
Authorization: `Basic ${btoa(env.ABLY_API_KEY)}`,
},
body: JSON.stringify({
keyName,
clientId: user?.id,
timestamp: Date.now(),
}),
}
);
const tokenResponseData = (await tokenResponse.json()) as AblyTokenResponse;
2023-09-24 23:49:24 -06:00
2023-10-02 19:29:35 -06:00
return NextResponse.json(tokenResponseData, {
2023-09-24 23:49:24 -06:00
status: 200,
statusText: "SUCCESS",
});
}
export { handler as POST, handler as GET };