feat: add user and session

This commit is contained in:
Daniel Soares
2021-12-15 14:05:27 -03:00
parent 5df44ceddc
commit e071c0a19b
11 changed files with 577 additions and 9 deletions

View File

@@ -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
}

View File

@@ -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()