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);

View File

@@ -0,0 +1,54 @@
import { Context } from "hono";
import { authHandler, initAuthConfig, verifyAuth } from "@hono/auth-js";
import Credentials from "@auth/core/providers/credentials";
import { AuthConfig } from "@auth/core/types";
export const authConfig: AuthConfig = {
secret: process.env.AUTH_SECRET,
providers: [
Credentials({
name: "Credentials",
credentials: {
username: { label: "Username", type: "text" },
password: { label: "Password", type: "password" },
},
async authorize(credentials) {
// Add your authentication logic here
const user = { id: "1", name: "John Doe", email: "john@example.com" };
if (
credentials?.username === "john" &&
credentials?.password === "password"
) {
return user;
}
return null;
},
}),
],
session: {
strategy: "jwt",
},
callbacks: {
// async session({ session, token }) {
// session.user.id = token.sub;
// return session;
// },
async jwt({ token, user }) {
if (user) {
token.sub = user.id;
}
return token;
},
},
};
// auth.use("/api/auth/*", authHandler());
// auth.use("/api/*", verifyAuth());
// auth.get("/api/protected", (c) => {
// const auth = c.get("authUser");
// return c.json(auth);
// });
// export default auth;