feat(apps\server): auth stuff

This commit is contained in:
2025-02-17 06:05:12 -06:00
parent aa2ba8d79a
commit ca27264bb0
9 changed files with 295 additions and 5 deletions

View File

@@ -2,6 +2,10 @@ 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 { expensesRoute } from "./routes/expenses";
const app = new Hono();
@@ -13,9 +17,39 @@ app.all("/ocme/*", async (c) => {
return ocmeService(c);
});
app.get("/test", (c) => {
//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/test", (c) => {
const auth = c.get("authUser");
return c.json({ success: true, message: "hello from bun" });
});
// const authRoute = app.basePath("/api/auth").route("*", )
//const apiRoute = app.basePath("/api").route("/expenses", expensesRoute);