From c3808792bbe5dcc7504423af9d15df4e4464e871 Mon Sep 17 00:00:00 2001 From: SeanCassiere <33615041+SeanCassiere@users.noreply.github.com> Date: Tue, 2 Jul 2024 08:19:24 +1200 Subject: [PATCH 1/2] refactor(ui): split the ui out of the auth routing --- src/routers/app/auth/index.mts | 25 ---------- .../app/{ui => }/components/button.tsx | 0 src/routers/app/{ui => }/components/card.tsx | 0 src/routers/app/{ui => }/components/icons.tsx | 0 src/routers/app/{ui => }/index.tsx | 3 ++ .../app/{ui => }/layouts/app-container.tsx | 2 +- .../app/{ui => }/layouts/root-document.tsx | 0 .../pages/app.$workspace.$serviceId.edit.tsx | 0 .../pages/app.$workspace.$serviceId.index.tsx | 0 .../{ui => }/pages/app.$workspace.edit.tsx | 0 .../{ui => }/pages/app.$workspace.index.tsx | 0 src/routers/app/{ui => }/pages/app.index.tsx | 0 src/routers/app/{ui => }/pages/app.login.tsx | 2 +- src/routers/app/{ui => }/utils/date.mts | 0 src/routers/app/{ui => }/utils/middleware.mts | 10 ++++ .../{app/auth/github.mts => auth/index.mts} | 47 ++++++++++++++----- .../{app/index.mts => auth/middleware.mts} | 15 ++---- src/server.mts | 4 +- 18 files changed, 55 insertions(+), 53 deletions(-) delete mode 100644 src/routers/app/auth/index.mts rename src/routers/app/{ui => }/components/button.tsx (100%) rename src/routers/app/{ui => }/components/card.tsx (100%) rename src/routers/app/{ui => }/components/icons.tsx (100%) rename src/routers/app/{ui => }/index.tsx (97%) rename src/routers/app/{ui => }/layouts/app-container.tsx (96%) rename src/routers/app/{ui => }/layouts/root-document.tsx (100%) rename src/routers/app/{ui => }/pages/app.$workspace.$serviceId.edit.tsx (100%) rename src/routers/app/{ui => }/pages/app.$workspace.$serviceId.index.tsx (100%) rename src/routers/app/{ui => }/pages/app.$workspace.edit.tsx (100%) rename src/routers/app/{ui => }/pages/app.$workspace.index.tsx (100%) rename src/routers/app/{ui => }/pages/app.index.tsx (100%) rename src/routers/app/{ui => }/pages/app.login.tsx (96%) rename src/routers/app/{ui => }/utils/date.mts (100%) rename src/routers/app/{ui => }/utils/middleware.mts (84%) rename src/routers/{app/auth/github.mts => auth/index.mts} (77%) rename src/routers/{app/index.mts => auth/middleware.mts} (72%) diff --git a/src/routers/app/auth/index.mts b/src/routers/app/auth/index.mts deleted file mode 100644 index 2190021..0000000 --- a/src/routers/app/auth/index.mts +++ /dev/null @@ -1,25 +0,0 @@ -import type { ServerContext } from "@/types/hono.mjs"; -import { Hono } from "hono"; - -import { lucia } from "@/config/lucia/index.mjs"; - -import githubAuthRouter from "./github.mjs"; - -const app = new Hono(); - -app.route("/login/github", githubAuthRouter); - -app.get("/logout", async (c) => { - const session = c.get("session"); - if (!session) { - return c.body(null, 401); - } - - await lucia.invalidateSession(session.id); - - c.header("Set-Cookie", lucia.createBlankSessionCookie().serialize(), { append: true }); - - return c.redirect("/app"); -}); - -export default app; diff --git a/src/routers/app/ui/components/button.tsx b/src/routers/app/components/button.tsx similarity index 100% rename from src/routers/app/ui/components/button.tsx rename to src/routers/app/components/button.tsx diff --git a/src/routers/app/ui/components/card.tsx b/src/routers/app/components/card.tsx similarity index 100% rename from src/routers/app/ui/components/card.tsx rename to src/routers/app/components/card.tsx diff --git a/src/routers/app/ui/components/icons.tsx b/src/routers/app/components/icons.tsx similarity index 100% rename from src/routers/app/ui/components/icons.tsx rename to src/routers/app/components/icons.tsx diff --git a/src/routers/app/ui/index.tsx b/src/routers/app/index.tsx similarity index 97% rename from src/routers/app/ui/index.tsx rename to src/routers/app/index.tsx index 0d0d9c0..f8bd65b 100644 --- a/src/routers/app/ui/index.tsx +++ b/src/routers/app/index.tsx @@ -1,5 +1,6 @@ import { Hono } from "hono"; +import { sessionMiddleware } from "@/routers/auth/middleware.mjs"; import { db } from "@/config/db/index.mjs"; import { NoOrganizationPage } from "./pages/app.index.js"; @@ -15,6 +16,8 @@ import type { ServerContext } from "@/types/hono.mjs"; const app = new Hono(); +app.use("*", sessionMiddleware); + app.get("/", checkUserAuthed, async (c) => { const user = c.var.user!; const view_all = c.req.query("view_all") || "false"; diff --git a/src/routers/app/ui/layouts/app-container.tsx b/src/routers/app/layouts/app-container.tsx similarity index 96% rename from src/routers/app/ui/layouts/app-container.tsx rename to src/routers/app/layouts/app-container.tsx index 0faaf8f..3aae1a8 100644 --- a/src/routers/app/ui/layouts/app-container.tsx +++ b/src/routers/app/layouts/app-container.tsx @@ -43,7 +43,7 @@ export const AppContainer: FC> = ({
{children}
diff --git a/src/routers/app/ui/layouts/root-document.tsx b/src/routers/app/layouts/root-document.tsx similarity index 100% rename from src/routers/app/ui/layouts/root-document.tsx rename to src/routers/app/layouts/root-document.tsx diff --git a/src/routers/app/ui/pages/app.$workspace.$serviceId.edit.tsx b/src/routers/app/pages/app.$workspace.$serviceId.edit.tsx similarity index 100% rename from src/routers/app/ui/pages/app.$workspace.$serviceId.edit.tsx rename to src/routers/app/pages/app.$workspace.$serviceId.edit.tsx diff --git a/src/routers/app/ui/pages/app.$workspace.$serviceId.index.tsx b/src/routers/app/pages/app.$workspace.$serviceId.index.tsx similarity index 100% rename from src/routers/app/ui/pages/app.$workspace.$serviceId.index.tsx rename to src/routers/app/pages/app.$workspace.$serviceId.index.tsx diff --git a/src/routers/app/ui/pages/app.$workspace.edit.tsx b/src/routers/app/pages/app.$workspace.edit.tsx similarity index 100% rename from src/routers/app/ui/pages/app.$workspace.edit.tsx rename to src/routers/app/pages/app.$workspace.edit.tsx diff --git a/src/routers/app/ui/pages/app.$workspace.index.tsx b/src/routers/app/pages/app.$workspace.index.tsx similarity index 100% rename from src/routers/app/ui/pages/app.$workspace.index.tsx rename to src/routers/app/pages/app.$workspace.index.tsx diff --git a/src/routers/app/ui/pages/app.index.tsx b/src/routers/app/pages/app.index.tsx similarity index 100% rename from src/routers/app/ui/pages/app.index.tsx rename to src/routers/app/pages/app.index.tsx diff --git a/src/routers/app/ui/pages/app.login.tsx b/src/routers/app/pages/app.login.tsx similarity index 96% rename from src/routers/app/ui/pages/app.login.tsx rename to src/routers/app/pages/app.login.tsx index b9cd5fb..25685b2 100644 --- a/src/routers/app/ui/pages/app.login.tsx +++ b/src/routers/app/pages/app.login.tsx @@ -15,7 +15,7 @@ export const LoginPage: FC = () => {

You must first sign in with GitHub to access the application.

- + Login with GitHub diff --git a/src/routers/app/ui/utils/date.mts b/src/routers/app/utils/date.mts similarity index 100% rename from src/routers/app/ui/utils/date.mts rename to src/routers/app/utils/date.mts diff --git a/src/routers/app/ui/utils/middleware.mts b/src/routers/app/utils/middleware.mts similarity index 84% rename from src/routers/app/ui/utils/middleware.mts rename to src/routers/app/utils/middleware.mts index 5e56944..4d61d71 100644 --- a/src/routers/app/ui/utils/middleware.mts +++ b/src/routers/app/utils/middleware.mts @@ -1,7 +1,9 @@ +import { setCookie } from "hono/cookie"; import { createMiddleware } from "hono/factory"; import { HTTPException } from "hono/http-exception"; import { db } from "@/config/db/index.mjs"; +import { env } from "@/config/env.mjs"; import type { ServerContext } from "@/types/hono.mjs"; @@ -9,6 +11,14 @@ export const checkUserAuthed = createMiddleware(async (c, next) = const user = c.var.user; if (!user) { + setCookie(c, "post_login_redirect", c.req.url, { + path: "/", + secure: env.NODE_ENV === "production", + httpOnly: true, + maxAge: 60 * 10, + sameSite: "Lax", + }); + return c.redirect("/app/login"); } diff --git a/src/routers/app/auth/github.mts b/src/routers/auth/index.mts similarity index 77% rename from src/routers/app/auth/github.mts rename to src/routers/auth/index.mts index c5eac73..3577bf3 100644 --- a/src/routers/app/auth/github.mts +++ b/src/routers/auth/index.mts @@ -1,5 +1,7 @@ -import { OAuth2RequestError, generateState } from "arctic"; +import type { ServerContext } from "@/types/hono.mjs"; import { Hono } from "hono"; + +import { OAuth2RequestError, generateState } from "arctic"; import { getCookie, setCookie } from "hono/cookie"; import { z } from "zod"; @@ -13,11 +15,25 @@ import { env } from "@/config/env.mjs"; import { github, lucia } from "@/config/lucia/index.mjs"; import { createDbId } from "@/utils/db.mjs"; -import type { ServerContext } from "@/types/hono.mjs"; +import { sessionMiddleware } from "./middleware.mjs"; const app = new Hono(); -app.get("/", async (c) => { +app.use("*", sessionMiddleware); + +app.get("/login/github", async (c) => { + const post_login_redirect = getCookie(c).post_login_redirect || "/app"; + + if (post_login_redirect.length === 0 || post_login_redirect === "/app") { + setCookie(c, "post_login_redirect", "/app", { + path: "/", + secure: env.NODE_ENV === "production", + httpOnly: true, + maxAge: 60 * 10, + sameSite: "Lax", + }); + } + const state = generateState(); const url = await github.createAuthorizationURL(state); @@ -29,14 +45,6 @@ app.get("/", async (c) => { sameSite: "Lax", }); - setCookie(c, "post_login_redirect", "/app", { - path: "/", - secure: env.NODE_ENV === "production", - httpOnly: true, - maxAge: 60 * 10, - sameSite: "Lax", - }); - return c.redirect(url.toString()); }); @@ -45,7 +53,7 @@ const githubUserSchema = z.object({ login: z.string(), }); -app.get("/callback", async (c) => { +app.get("/login/github/callback", async (c) => { const code = c.req.query("code")?.toString() ?? null; const state = c.req.query("state")?.toString() ?? null; const storedState = getCookie(c).github_oauth_state ?? null; @@ -54,7 +62,7 @@ app.get("/callback", async (c) => { return c.body(null, 400); } - const postLoginRedirect = getCookie(c).post_login_redirect ?? "/app"; + const postLoginRedirect = getCookie(c).post_login_redirect || "/app"; try { const tokens = await github.validateAuthorizationCode(code); @@ -105,4 +113,17 @@ app.get("/callback", async (c) => { } }); +app.get("/logout", async (c) => { + const session = c.get("session"); + if (!session) { + return c.body(null, 401); + } + + await lucia.invalidateSession(session.id); + + c.header("Set-Cookie", lucia.createBlankSessionCookie().serialize(), { append: true }); + + return c.redirect("/app"); +}); + export default app; diff --git a/src/routers/app/index.mts b/src/routers/auth/middleware.mts similarity index 72% rename from src/routers/app/index.mts rename to src/routers/auth/middleware.mts index f364c4e..256bbd1 100644 --- a/src/routers/app/index.mts +++ b/src/routers/auth/middleware.mts @@ -1,14 +1,10 @@ -import type { ServerContext } from "@/types/hono.mjs"; -import { Hono } from "hono"; +import { createMiddleware } from "hono/factory"; import { lucia } from "@/config/lucia/index.mjs"; -import authRouter from "./auth/index.mjs"; -import uiRouter from "./ui/index.js"; - -const app = new Hono(); +import type { ServerContext } from "@/types/hono.mjs"; -app.use("*", async (c, next) => { +export const sessionMiddleware = createMiddleware(async (c, next) => { const sessionId = lucia.readSessionCookie(c.req.header("Cookie") ?? ""); if (!sessionId) { c.set("user", null); @@ -30,8 +26,3 @@ app.use("*", async (c, next) => { return await next(); }); - -app.route("", authRouter); -app.route("", uiRouter); - -export default app; diff --git a/src/server.mts b/src/server.mts index 776f3b6..3a69cd5 100644 --- a/src/server.mts +++ b/src/server.mts @@ -12,7 +12,8 @@ import { secureHeaders } from "hono/secure-headers"; import { timeout } from "hono/timeout"; import { trimTrailingSlash } from "hono/trailing-slash"; -import appRouter from "@/routers/app/index.mjs"; +import appRouter from "@/routers/app/index.js"; +import authRouter from "@/routers/auth/index.mjs"; import docsRouter from "@/routers/docs/index.mjs"; import v2Router from "@/routers/v2/index.mjs"; @@ -43,6 +44,7 @@ app.use("/api/", timeout(5000)); app.route("/api/v2", v2Router); app.use(limiter); +app.route("/auth", authRouter); app.route("/docs", docsRouter); app.route("/app", appRouter); From ab2855b38075df2cd0babcb68d47e2aa7d276a3d Mon Sep 17 00:00:00 2001 From: SeanCassiere <33615041+SeanCassiere@users.noreply.github.com> Date: Tue, 2 Jul 2024 08:20:08 +1200 Subject: [PATCH 2/2] chore: bump package to 2.3.4 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index b88f918..feb5bfc 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "simple-logging-server", "license": "MIT", - "version": "2.3.3", + "version": "2.3.4", "description": "This is a simple API for logging messages", "author": "Sean Cassiere", "keywords": [],