mirror of
https://github.com/dsoaress/link-free.git
synced 2026-08-18 23:46:04 +01:00
feat: add user and session
This commit is contained in:
@@ -11,3 +11,22 @@ model Data {
|
||||
id Int @id @default(autoincrement())
|
||||
data String
|
||||
}
|
||||
|
||||
model User {
|
||||
id Int @id @default(autoincrement())
|
||||
username String @unique
|
||||
password String
|
||||
role Role
|
||||
}
|
||||
|
||||
model Session {
|
||||
id String @id @default(cuid())
|
||||
userId Int
|
||||
expiresAt DateTime
|
||||
createdAt DateTime @default(now())
|
||||
}
|
||||
|
||||
enum Role {
|
||||
ADMIN
|
||||
EDITOR
|
||||
}
|
||||
|
||||
@@ -1,7 +1,13 @@
|
||||
import { PrismaClient } from '@prisma/client'
|
||||
import { hashSync } from 'bcryptjs'
|
||||
import dotenv from 'dotenv'
|
||||
|
||||
const prisma = new PrismaClient()
|
||||
|
||||
dotenv.config({
|
||||
path: './.env'
|
||||
})
|
||||
|
||||
async function main() {
|
||||
const data = {
|
||||
settings: {
|
||||
@@ -62,9 +68,7 @@ async function main() {
|
||||
}
|
||||
|
||||
const hasData = await prisma.data.findUnique({
|
||||
where: {
|
||||
id: 1
|
||||
}
|
||||
where: { id: 1 }
|
||||
})
|
||||
|
||||
if (!hasData) {
|
||||
@@ -77,6 +81,26 @@ async function main() {
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
const hasUser = await prisma.user.findFirst()
|
||||
|
||||
if (!hasUser) {
|
||||
console.log(`Start seeding user...`)
|
||||
|
||||
const { USERNAME, PASSWORD } = process.env
|
||||
|
||||
if (!USERNAME || !PASSWORD) {
|
||||
throw new Error('USERNAME and PASSWORD env variables are required')
|
||||
}
|
||||
|
||||
await prisma.user.create({
|
||||
data: {
|
||||
username: USERNAME,
|
||||
password: hashSync(PASSWORD, 10),
|
||||
role: 'ADMIN'
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
main()
|
||||
|
||||
Reference in New Issue
Block a user