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:
58
apps/backend/prisma/seed.ts
Normal file
58
apps/backend/prisma/seed.ts
Normal file
@@ -0,0 +1,58 @@
|
||||
import "dotenv/config";
|
||||
import { betterAuth } from "better-auth";
|
||||
import { prismaAdapter } from "better-auth/adapters/prisma";
|
||||
import { PrismaClient } from "../src/generated/prisma/client";
|
||||
import { PrismaPg } from "@prisma/adapter-pg";
|
||||
|
||||
const adapter = new PrismaPg({ connectionString: process.env.DATABASE_URL! });
|
||||
const prisma = new PrismaClient({ adapter });
|
||||
|
||||
async function main() {
|
||||
const existingUser = await prisma.user.findUnique({
|
||||
where: { email: "jselesan@gmail.com" },
|
||||
});
|
||||
|
||||
if (existingUser) {
|
||||
console.log("Admin user already exists, skipping seed.");
|
||||
return;
|
||||
}
|
||||
|
||||
const auth = betterAuth({
|
||||
database: prismaAdapter(prisma, {
|
||||
provider: "postgresql",
|
||||
}),
|
||||
emailAndPassword: {
|
||||
enabled: true,
|
||||
disableSignUp: false,
|
||||
minPasswordLength: 4,
|
||||
},
|
||||
secret: process.env.BETTER_AUTH_SECRET!,
|
||||
baseURL: process.env.BETTER_AUTH_URL ?? "http://localhost:3000",
|
||||
});
|
||||
|
||||
const password = process.env.ADMIN_PASSWORD;
|
||||
|
||||
if (!password) {
|
||||
console.error("ADMIN_PASSWORD environment variable is not set.");
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
await auth.api.signUpEmail({
|
||||
body: {
|
||||
email: "jselesan@gmail.com",
|
||||
password,
|
||||
name: "Admin",
|
||||
},
|
||||
});
|
||||
|
||||
console.log("Admin user created successfully.");
|
||||
}
|
||||
|
||||
main()
|
||||
.catch((e) => {
|
||||
console.error(e);
|
||||
process.exit(1);
|
||||
})
|
||||
.finally(async () => {
|
||||
await prisma.$disconnect();
|
||||
});
|
||||
Reference in New Issue
Block a user