mirror of
https://github.com/Drop-OSS/drop.git
synced 2025-11-09 20:12:10 +10:00
* 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
26 lines
701 B
TypeScript
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();
|
|
}
|
|
},
|
|
});
|