diff --git a/new-functions/src/index.ts b/new-functions/src/index.ts index 61f1cc7..af72fe9 100644 --- a/new-functions/src/index.ts +++ b/new-functions/src/index.ts @@ -1,6 +1,20 @@ import { Hono } from "hono"; +import { cors } from "hono/cors"; const app = new Hono(); +app.use(cors()); + +app.use(async (c, next) => { + const cookie = c.req.header("Cookie") || ""; + const r = await fetch("https://account.lingdocs.com/api/user", { + headers: { cookie }, + }); + const res = await r.json(); + if (res.ok) { + c.set("user", res.user); + } + await next(); +}); app.get("/", (c) => { // c.env.LINGDOCS_COUCHDB @@ -8,19 +22,7 @@ app.get("/", (c) => { }); app.get("/wa", async (c) => { - const cookie = c.req.header("Cookie") || ""; - const r = await fetch("https://account.lingdocs.com/api/user", { - headers: { cookie }, - }); - const { ok, user } = await r.json(); - // const { - // headers: { cookie }, - // } = c. req; - // if (!cookie) { - // return { req: null, res }; - // } - // c.env.LINGDOCS_COUCHDB - return c.json({ ok, user }); + return c.json({ user: c.var.user }); }); export default app;