feat: implement secure admin authentication system with JWT sessions, middleware protection, and Prisma schema initialization

This commit is contained in:
Yolando
2026-03-28 20:38:47 +07:00
parent 53da46def1
commit 0549f12a97
13 changed files with 1100 additions and 6 deletions

68
prisma/schema.prisma Normal file
View File

@@ -0,0 +1,68 @@
generator client {
provider = "prisma-client-js"
}
datasource db {
provider = "postgresql"
url = env("DATABASE_URL")
}
model User {
id String @id @default(uuid()) @db.Uuid
email String @unique
passwordHash String @map("password_hash")
name String
@@map("users")
}
model Project {
id String @id @default(uuid()) @db.Uuid
title String
slug String @unique
description String @db.Text
imageUrl String? @map("image_url")
repoUrl String? @map("repo_url")
liveUrl String? @map("live_url")
category String
isPublished Boolean @default(false) @map("is_published")
createdAt DateTime @default(now()) @map("created_at")
skills ProjectSkill[]
@@map("projects")
}
model Skill {
id String @id @default(uuid()) @db.Uuid
name String
iconName String? @map("icon_name")
category String
projects ProjectSkill[]
@@map("skills")
}
// Explicit Join Table based on ERD
model ProjectSkill {
projectId String @map("project_id") @db.Uuid
skillId String @map("skill_id") @db.Uuid
project Project @relation(fields: [projectId], references: [id], onDelete: Cascade)
skill Skill @relation(fields: [skillId], references: [id], onDelete: Cascade)
@@id([projectId, skillId])
@@map("project_skills")
}
model Message {
id String @id @default(uuid()) @db.Uuid
senderName String @map("sender_name")
senderEmail String @map("sender_email")
content String @db.Text
isRead Boolean @default(false) @map("is_read")
createdAt DateTime @default(now()) @map("created_at")
@@map("messages")
}

34
prisma/seed.ts Normal file
View File

@@ -0,0 +1,34 @@
import { PrismaClient } from "@prisma/client";
import { hash } from "bcryptjs";
const prisma = new PrismaClient();
async function main() {
console.log("Seeding database...");
const adminEmail = process.env.ADMIN_EMAIL || "admin@ando.dev";
const rawPassword = process.env.ADMIN_PASSWORD || "admin123";
const passwordHash = await hash(rawPassword, 12);
// Upsert ensures we don't insert duplicate admins if the seeder runs twice
const user = await prisma.user.upsert({
where: { email: adminEmail },
update: {},
create: {
email: adminEmail,
name: "Yolando Admin",
passwordHash,
},
});
console.log(`Admin user created: ${user.email}`);
}
main()
.catch((e) => {
console.error(e);
process.exit(1);
})
.finally(async () => {
await prisma.$disconnect();
});