From 1c11bf9cf290e4cb31120fedfd35308176bd20f4 Mon Sep 17 00:00:00 2001 From: adueck Date: Tue, 26 Nov 2024 16:59:16 +0500 Subject: [PATCH] start on auth middleware --- new-functions/src/index.ts | 28 +++++++++++++++------------- 1 file changed, 15 insertions(+), 13 deletions(-) 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;