feat(auth): implement better authentication system with user sessions and password management
- Added user, session, account, and verification models to Prisma schema. - Integrated better-auth for user authentication with email and password. - Created auth middleware for session validation. - Implemented auth context and client in frontend for managing user sessions. - Added login and settings pages for user authentication and password management. - Updated routes to include authentication checks and user-specific content. - Enhanced UI components to reflect authentication state.
This commit is contained in:
@@ -1,5 +1,7 @@
|
||||
import { Hono } from "hono";
|
||||
import { serveStatic } from "hono/bun";
|
||||
import { auth } from "./lib/auth";
|
||||
import { authMiddleware } from "./lib/auth-middleware";
|
||||
import { startQuoteJob } from "./modules/quotes/quote.job";
|
||||
import quotesRouter from "./modules/quotes/quotes.routes";
|
||||
import expensesRouter from "./modules/expenses/expenses.routes";
|
||||
@@ -12,6 +14,15 @@ app.get("/api/health", (c) => {
|
||||
return c.json({ status: "ok", timestamp: new Date().toISOString() });
|
||||
});
|
||||
|
||||
app.on(["POST", "GET"], "/api/auth/*", (c) => auth.handler(c.req.raw));
|
||||
|
||||
app.use("/api/*", async (c, next) => {
|
||||
if (c.req.path === "/api/health" || c.req.path.startsWith("/api/auth")) {
|
||||
return next();
|
||||
}
|
||||
return authMiddleware(c, next);
|
||||
});
|
||||
|
||||
app.route("/api/quotes", quotesRouter);
|
||||
app.route("/api", expensesRouter);
|
||||
|
||||
|
||||
14
apps/backend/src/lib/auth-middleware.ts
Normal file
14
apps/backend/src/lib/auth-middleware.ts
Normal file
@@ -0,0 +1,14 @@
|
||||
import type { Context, Next } from "hono";
|
||||
import { auth } from "./auth";
|
||||
|
||||
export async function authMiddleware(c: Context, next: Next) {
|
||||
const session = await auth.api.getSession({ headers: c.req.raw.headers });
|
||||
|
||||
if (!session) {
|
||||
return c.json({ error: "Unauthorized" }, 401);
|
||||
}
|
||||
|
||||
c.set("user", session.user);
|
||||
c.set("session", session.session);
|
||||
await next();
|
||||
}
|
||||
20
apps/backend/src/lib/auth.ts
Normal file
20
apps/backend/src/lib/auth.ts
Normal file
@@ -0,0 +1,20 @@
|
||||
import { betterAuth } from "better-auth";
|
||||
import { prismaAdapter } from "better-auth/adapters/prisma";
|
||||
import { prisma } from "./prisma";
|
||||
|
||||
export const auth = betterAuth({
|
||||
database: prismaAdapter(prisma, {
|
||||
provider: "postgresql",
|
||||
}),
|
||||
emailAndPassword: {
|
||||
enabled: true,
|
||||
disableSignUp: true,
|
||||
minPasswordLength: 4,
|
||||
},
|
||||
secret: process.env.BETTER_AUTH_SECRET!,
|
||||
baseURL: process.env.BETTER_AUTH_URL!,
|
||||
trustedOrigins: [
|
||||
process.env.BETTER_AUTH_URL!,
|
||||
"http://localhost:5173",
|
||||
],
|
||||
});
|
||||
Reference in New Issue
Block a user