cleaner
This commit is contained in:
parent
2efa3086f2
commit
a0704e7808
|
@ -1,35 +1,16 @@
|
||||||
import { Hono } from "hono";
|
import { Hono } from "hono";
|
||||||
import { cors } from "hono/cors";
|
import { cors } from "hono/cors";
|
||||||
import { createMiddleware } from "hono/factory";
|
import { authMiddleware } from "./middleware/lingdocs-auth";
|
||||||
import type { LingdocsUser } from "../../website/src/types/account-types";
|
|
||||||
|
|
||||||
const app = new Hono();
|
const app = new Hono();
|
||||||
app.use(cors());
|
app.use(cors());
|
||||||
|
|
||||||
const authMiddleware = createMiddleware<{
|
|
||||||
Variables: {
|
|
||||||
user: LingdocsUser;
|
|
||||||
};
|
|
||||||
}>(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()) as { ok: boolean; user: LingdocsUser };
|
|
||||||
if (res.ok) {
|
|
||||||
c.set("user", res.user);
|
|
||||||
}
|
|
||||||
await next();
|
|
||||||
});
|
|
||||||
|
|
||||||
app.use(authMiddleware);
|
|
||||||
|
|
||||||
app.get("/", (c) => {
|
app.get("/", (c) => {
|
||||||
// c.env.LINGDOCS_COUCHDB
|
// c.env.LINGDOCS_COUCHDB
|
||||||
return c.text("Hi from hono updated");
|
return c.text("Hi from hono updated");
|
||||||
});
|
});
|
||||||
|
|
||||||
app.get("/wa", async (c) => {
|
app.get("/wa", authMiddleware, async (c) => {
|
||||||
return c.json({ user: c.var.user });
|
return c.json({ user: c.var.user });
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
|
@ -0,0 +1,20 @@
|
||||||
|
import { createMiddleware } from "hono/factory";
|
||||||
|
import type { LingdocsUser } from "../../../website/src/types/account-types";
|
||||||
|
|
||||||
|
export const authMiddleware = createMiddleware<{
|
||||||
|
Variables: {
|
||||||
|
user: LingdocsUser | undefined;
|
||||||
|
};
|
||||||
|
}>(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()) as { ok: boolean; user: LingdocsUser };
|
||||||
|
if (res.ok) {
|
||||||
|
c.set("user", res.user);
|
||||||
|
} else {
|
||||||
|
c.set("user", undefined);
|
||||||
|
}
|
||||||
|
await next();
|
||||||
|
});
|
Loading…
Reference in New Issue