Refactor Hono API handler, fix missing headers
This commit is contained in:
@@ -33,17 +33,16 @@ const app = new Hono()
|
||||
// Callback URL with query response type
|
||||
if (code !== undefined || state !== undefined) {
|
||||
yield* Authentication.getAndProcessIdToken(code ?? null, state ?? null);
|
||||
return Response.redirect(config.NODE_ENV === "production" ? `https://${config.HOSTNAME}/` : "http://localhost:5173/", 303);
|
||||
return ctx.redirect(config.NODE_ENV === "production" ? `https://${config.HOSTNAME}/` : "http://localhost:5173/", 303);
|
||||
}
|
||||
|
||||
// Initial login request; redirect to identity provider
|
||||
const res = yield* pipe(
|
||||
const url = yield* pipe(
|
||||
Authentication.Authentication,
|
||||
Effect.flatMap(({ sessionId }) => Authentication.makeAuthorizationUrl(sessionId)),
|
||||
Effect.map((url) => Response.redirect(url)),
|
||||
);
|
||||
|
||||
return res;
|
||||
return ctx.redirect(url);
|
||||
}).pipe(
|
||||
Effect.provide(Layer.provideMerge(Authentication.Live(ctx), databaseLayer)),
|
||||
Effect.runPromise,
|
||||
@@ -58,7 +57,7 @@ const app = new Hono()
|
||||
|
||||
yield* Authentication.getAndProcessIdToken(code, state);
|
||||
|
||||
return Response.redirect(config.NODE_ENV === "production" ? `https://${config.HOSTNAME}/` : "http://localhost:5173/", 303);
|
||||
return ctx.redirect(config.NODE_ENV === "production" ? `https://${config.HOSTNAME}/` : "http://localhost:5173/", 303);
|
||||
}).pipe(
|
||||
Effect.provide(Layer.provideMerge(Authentication.Live(ctx), databaseLayer)),
|
||||
Effect.runPromise,
|
||||
@@ -87,17 +86,22 @@ const app = new Hono()
|
||||
const authenticationLayer = Authentication.Live(ctx);
|
||||
const layers = Layer.provideMerge(authenticationLayer, databaseLayer);
|
||||
|
||||
const response = await pipe(
|
||||
const { body, status } = await pipe(
|
||||
handle(req),
|
||||
Effect.provide(layers),
|
||||
Effect.runPromise,
|
||||
);
|
||||
|
||||
for (const [name, value] of CORS_HEADERS) {
|
||||
response.headers.set(name, value);
|
||||
ctx.header(name, value);
|
||||
}
|
||||
|
||||
return response;
|
||||
if (body !== null) {
|
||||
ctx.header("Content-Type", "application/cbor");
|
||||
return ctx.body(body, status);
|
||||
} else {
|
||||
return ctx.body(body, status);
|
||||
}
|
||||
})
|
||||
.use("/assets/*", serveStatic({ root: FRONTEND_ROOT }))
|
||||
.use("*", serveStatic({ path: path.join(FRONTEND_ROOT, "index.html") }));
|
||||
|
||||
Reference in New Issue
Block a user