Files
drop/server/api/v1/client/auth/code/ws.get.ts
DecDuck b72e1ef7a4 Code-based authorization for Drop clients (#145)
* feat: code-based authorization

* fix: final touches

* fix: require session on code fetch endpoint

* feat: better error handling

* refactor: move auth send to client handler

* fix: lint
2025-08-01 13:11:56 +10:00

26 lines
701 B
TypeScript

import type { FetchError } from "ofetch";
import clientHandler from "~/server/internal/clients/handler";
export default defineWebSocketHandler({
async open(peer) {
try {
const h3 = { headers: peer.request?.headers ?? new Headers() };
const code = h3.headers.get("Authorization");
if (!code)
throw createError({
statusCode: 400,
statusMessage: "Code required in Authorization header.",
});
await clientHandler.connectCodeListener(code, peer);
} catch (e) {
peer.send(
JSON.stringify({
type: "error",
value: (e as FetchError)?.statusMessage,
}),
);
peer.close();
}
},
});