feat(lst): added in basic authentication

This commit is contained in:
2025-02-17 20:01:04 -06:00
parent ca27264bb0
commit 5f7a3dd182
25 changed files with 810 additions and 154 deletions

View File

@@ -1,60 +1,49 @@
import { Hono } from "hono";
import { serveStatic } from "hono/bun";
import { logger } from "hono/logger";
import { ocmeService } from "./services/ocmeServer";
import { AuthConfig } from "@auth/core/types";
import { authHandler, initAuthConfig, verifyAuth } from "@hono/auth-js";
import Credentials from "@auth/core/providers/credentials";
import { authConfig } from "./auth/auth";
import {Hono} from "hono";
import {serveStatic} from "hono/bun";
import {logger} from "hono/logger";
import {ocmeService} from "./services/ocmeServer";
import {authMiddleware} from "lst-auth";
import {cors} from "hono/cors";
//import { expensesRoute } from "./routes/expenses";
import login from "./route/auth/login";
import session from "./route/auth/session";
const app = new Hono();
app.use("*", logger());
app.use(
"*",
cors({
origin: "http://localhost:5173",
allowHeaders: ["X-Custom-Header", "Upgrade-Insecure-Requests"],
allowMethods: ["POST", "GET", "OPTIONS"],
exposeHeaders: ["Content-Length", "X-Kuma-Revision"],
maxAge: 600,
credentials: true,
})
);
// as we dont want to change ocme again well use a proxy to this
app.all("/ocme/*", async (c) => {
return ocmeService(c);
return ocmeService(c);
});
app.basePath("/api/auth").route("/login", login).route("/session", session);
//auth stuff
app.use("*", initAuthConfig(authConfig));
app.use("/api/auth/*", async (c, next) => {
const response = await authHandler()(c, next);
if (c.req.path === "/api/auth/callback/credentials") {
const setCookieHeader = response.headers.get("Set-Cookie");
if (setCookieHeader) {
const tokenMatch = setCookieHeader.match(/authjs\.session-token=([^;]+)/);
const jwt = tokenMatch ? tokenMatch[1] : null;
if (jwt) {
console.log("Extracted JWT:", jwt);
return c.json({ token: jwt });
}
}
}
return response;
});
app.get("/api/protected", verifyAuth(), (c) => {
const auth = c.get("authUser");
return c.json(auth);
app.get("/api/protected", authMiddleware, (c) => {
return c.json({success: true, message: "is authenticated"});
});
app.get("/api/test", (c) => {
const auth = c.get("authUser");
return c.json({ success: true, message: "hello from bun" });
return c.json({success: true, message: "hello from bun"});
});
// const authRoute = app.basePath("/api/auth").route("*", )
//const apiRoute = app.basePath("/api").route("/expenses", expensesRoute);
app.get("*", serveStatic({ root: "../frontend/dist" }));
app.get("*", serveStatic({ path: "../frontend/dist/index.html" }));
app.get("*", serveStatic({root: "../frontend/dist"}));
app.get("*", serveStatic({path: "../frontend/dist/index.html"}));
export default app;