Added Auth

This commit is contained in:
2025-12-22 10:47:15 +07:00
parent 2c244b77fb
commit fa029365d0
79 changed files with 19643 additions and 2830 deletions

View File

@@ -0,0 +1,149 @@
-- CreateTable
CREATE TABLE "user" (
"id" TEXT NOT NULL,
"name" TEXT NOT NULL,
"email" TEXT NOT NULL,
"emailVerified" BOOLEAN NOT NULL DEFAULT false,
"image" TEXT,
"createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
"updatedAt" TIMESTAMP(3) NOT NULL,
"role" TEXT,
"banned" BOOLEAN DEFAULT false,
"banReason" TEXT,
"banExpires" TIMESTAMP(3),
CONSTRAINT "user_pkey" PRIMARY KEY ("id")
);
-- CreateTable
CREATE TABLE "session" (
"id" TEXT NOT NULL,
"expiresAt" TIMESTAMP(3) NOT NULL,
"token" TEXT NOT NULL,
"createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
"updatedAt" TIMESTAMP(3) NOT NULL,
"ipAddress" TEXT,
"userAgent" TEXT,
"userId" TEXT NOT NULL,
"impersonatedBy" TEXT,
"activeOrganizationId" TEXT,
CONSTRAINT "session_pkey" PRIMARY KEY ("id")
);
-- CreateTable
CREATE TABLE "account" (
"id" TEXT NOT NULL,
"accountId" TEXT NOT NULL,
"providerId" TEXT NOT NULL,
"userId" TEXT NOT NULL,
"accessToken" TEXT,
"refreshToken" TEXT,
"idToken" TEXT,
"accessTokenExpiresAt" TIMESTAMP(3),
"refreshTokenExpiresAt" TIMESTAMP(3),
"scope" TEXT,
"password" TEXT,
"createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
"updatedAt" TIMESTAMP(3) NOT NULL,
CONSTRAINT "account_pkey" PRIMARY KEY ("id")
);
-- CreateTable
CREATE TABLE "verification" (
"id" TEXT NOT NULL,
"identifier" TEXT NOT NULL,
"value" TEXT NOT NULL,
"expiresAt" TIMESTAMP(3) NOT NULL,
"createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
"updatedAt" TIMESTAMP(3) NOT NULL,
CONSTRAINT "verification_pkey" PRIMARY KEY ("id")
);
-- CreateTable
CREATE TABLE "organization" (
"id" TEXT NOT NULL,
"name" TEXT NOT NULL,
"slug" TEXT NOT NULL,
"logo" TEXT,
"createdAt" TIMESTAMP(3) NOT NULL,
"metadata" TEXT,
"color" TEXT DEFAULT '#000000',
CONSTRAINT "organization_pkey" PRIMARY KEY ("id")
);
-- CreateTable
CREATE TABLE "member" (
"id" TEXT NOT NULL,
"organizationId" TEXT NOT NULL,
"userId" TEXT NOT NULL,
"role" TEXT NOT NULL DEFAULT 'member',
"createdAt" TIMESTAMP(3) NOT NULL,
CONSTRAINT "member_pkey" PRIMARY KEY ("id")
);
-- CreateTable
CREATE TABLE "invitation" (
"id" TEXT NOT NULL,
"organizationId" TEXT NOT NULL,
"email" TEXT NOT NULL,
"role" TEXT,
"status" TEXT NOT NULL DEFAULT 'pending',
"expiresAt" TIMESTAMP(3) NOT NULL,
"createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
"inviterId" TEXT NOT NULL,
CONSTRAINT "invitation_pkey" PRIMARY KEY ("id")
);
-- CreateIndex
CREATE UNIQUE INDEX "user_email_key" ON "user"("email");
-- CreateIndex
CREATE INDEX "session_userId_idx" ON "session"("userId");
-- CreateIndex
CREATE UNIQUE INDEX "session_token_key" ON "session"("token");
-- CreateIndex
CREATE INDEX "account_userId_idx" ON "account"("userId");
-- CreateIndex
CREATE INDEX "verification_identifier_idx" ON "verification"("identifier");
-- CreateIndex
CREATE UNIQUE INDEX "organization_slug_key" ON "organization"("slug");
-- CreateIndex
CREATE INDEX "member_organizationId_idx" ON "member"("organizationId");
-- CreateIndex
CREATE INDEX "member_userId_idx" ON "member"("userId");
-- CreateIndex
CREATE INDEX "invitation_organizationId_idx" ON "invitation"("organizationId");
-- CreateIndex
CREATE INDEX "invitation_email_idx" ON "invitation"("email");
-- AddForeignKey
ALTER TABLE "session" ADD CONSTRAINT "session_userId_fkey" FOREIGN KEY ("userId") REFERENCES "user"("id") ON DELETE CASCADE ON UPDATE CASCADE;
-- AddForeignKey
ALTER TABLE "account" ADD CONSTRAINT "account_userId_fkey" FOREIGN KEY ("userId") REFERENCES "user"("id") ON DELETE CASCADE ON UPDATE CASCADE;
-- AddForeignKey
ALTER TABLE "member" ADD CONSTRAINT "member_organizationId_fkey" FOREIGN KEY ("organizationId") REFERENCES "organization"("id") ON DELETE CASCADE ON UPDATE CASCADE;
-- AddForeignKey
ALTER TABLE "member" ADD CONSTRAINT "member_userId_fkey" FOREIGN KEY ("userId") REFERENCES "user"("id") ON DELETE CASCADE ON UPDATE CASCADE;
-- AddForeignKey
ALTER TABLE "invitation" ADD CONSTRAINT "invitation_organizationId_fkey" FOREIGN KEY ("organizationId") REFERENCES "organization"("id") ON DELETE CASCADE ON UPDATE CASCADE;
-- AddForeignKey
ALTER TABLE "invitation" ADD CONSTRAINT "invitation_inviterId_fkey" FOREIGN KEY ("inviterId") REFERENCES "user"("id") ON DELETE CASCADE ON UPDATE CASCADE;

View File

@@ -0,0 +1,3 @@
# Please do not edit this file manually
# It should be added in your version-control system (e.g., Git)
provider = "postgresql"

View File

@@ -1,3 +1,4 @@
generator client {
provider = "prisma-client"
output = "../src/generated/prisma"
@@ -6,3 +7,125 @@ generator client {
datasource db {
provider = "postgresql"
}
model User {
id String @id
name String
email String
emailVerified Boolean @default(false)
image String?
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
sessions Session[]
accounts Account[]
role String?
banned Boolean? @default(false)
banReason String?
banExpires DateTime?
members Member[]
invitations Invitation[]
@@unique([email])
@@map("user")
}
model Session {
id String @id
expiresAt DateTime
token String
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
ipAddress String?
userAgent String?
userId String
user User @relation(fields: [userId], references: [id], onDelete: Cascade)
impersonatedBy String?
activeOrganizationId String?
@@unique([token])
@@index([userId])
@@map("session")
}
model Account {
id String @id
accountId String
providerId String
userId String
user User @relation(fields: [userId], references: [id], onDelete: Cascade)
accessToken String?
refreshToken String?
idToken String?
accessTokenExpiresAt DateTime?
refreshTokenExpiresAt DateTime?
scope String?
password String?
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
@@index([userId])
@@map("account")
}
model Verification {
id String @id
identifier String
value String
expiresAt DateTime
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
@@index([identifier])
@@map("verification")
}
model Organization {
id String @id
name String
slug String
logo String?
createdAt DateTime
metadata String?
members Member[]
invitations Invitation[]
color String? @default("#000000")
@@unique([slug])
@@map("organization")
}
model Member {
id String @id
organizationId String
organization Organization @relation(fields: [organizationId], references: [id], onDelete: Cascade)
userId String
user User @relation(fields: [userId], references: [id], onDelete: Cascade)
role String @default("member")
createdAt DateTime
@@index([organizationId])
@@index([userId])
@@map("member")
}
model Invitation {
id String @id
organizationId String
organization Organization @relation(fields: [organizationId], references: [id], onDelete: Cascade)
email String
role String?
status String @default("pending")
expiresAt DateTime
createdAt DateTime @default(now())
inviterId String
user User @relation(fields: [inviterId], references: [id], onDelete: Cascade)
@@index([organizationId])
@@index([email])
@@map("invitation")
}

View File

@@ -1,6 +1,6 @@
import { PrismaClient } from '../src/generated/prisma/client.js'
import { PrismaPg } from '@prisma/adapter-pg'
import { PrismaClient } from '../src/generated/prisma/client.js'
import { auth } from '@/lib/auth'
const adapter = new PrismaPg({
connectionString: process.env.DATABASE_URL!,
@@ -11,19 +11,29 @@ const prisma = new PrismaClient({ adapter })
async function main() {
console.log('🌱 Seeding database...')
// Clear existing todos
await prisma.todo.deleteMany()
// Create example todos
const todos = await prisma.todo.createMany({
data: [
{ title: 'Buy groceries' },
{ title: 'Read a book' },
{ title: 'Workout' },
],
// add admin user
await auth.api.createUser({
body: {
email: 'luu.dat.tham@gmail.com',
password: 'Th@m!S@m!040390',
name: 'Sam',
role: 'admin',
},
})
console.log(` Created ${todos.count} todos`)
// // Clear existing todos
// await prisma.todo.deleteMany()
// // Create example todos
// const todos = await prisma.todo.createMany({
// data: [
// { title: 'Buy groceries' },
// { title: 'Read a book' },
// { title: 'Workout' },
// ],
// })
// console.log(`✅ Created ${todos.count} todos`)
}
main()