-
Notifications
You must be signed in to change notification settings - Fork 1
feat: add admin seed script with default admin account #5
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,50 @@ | ||
| import { PrismaClient } from '@prisma/client'; | ||
| import bcrypt from 'bcryptjs'; | ||
|
|
||
| const prisma = new PrismaClient(); | ||
|
Comment on lines
+1
to
+4
|
||
|
|
||
| const ADMIN_EMAIL = process.env.ADMIN_EMAIL; | ||
| const ADMIN_PASSWORD = process.env.ADMIN_PASSWORD; | ||
|
|
||
| async function main() { | ||
| if (!ADMIN_EMAIL || !ADMIN_PASSWORD) { | ||
| console.log('ADMIN_EMAIL or ADMIN_PASSWORD not set, skipping admin seed.'); | ||
| return; | ||
| } | ||
|
|
||
| const existing = await prisma.user.findUnique({ | ||
| where: { email: ADMIN_EMAIL.toLowerCase() }, | ||
| }); | ||
|
|
||
| if (existing) { | ||
| if (existing.role !== 'ADMIN') { | ||
| await prisma.user.update({ | ||
| where: { id: existing.id }, | ||
| data: { role: 'ADMIN' }, | ||
| }); | ||
| console.log(`Promoted ${ADMIN_EMAIL} to ADMIN.`); | ||
| } else { | ||
| console.log(`${ADMIN_EMAIL} is already ADMIN.`); | ||
| } | ||
| return; | ||
| } | ||
|
Comment on lines
+19
to
+30
|
||
|
|
||
| const hashedPassword = await bcrypt.hash(ADMIN_PASSWORD, 12); | ||
|
|
||
|
Comment on lines
+32
to
+33
|
||
| await prisma.user.create({ | ||
| data: { | ||
| email: ADMIN_EMAIL.toLowerCase(), | ||
| password: hashedPassword, | ||
| role: 'ADMIN', | ||
| }, | ||
| }); | ||
|
Comment on lines
+34
to
+40
|
||
|
|
||
| console.log(`Created admin user: ${ADMIN_EMAIL}`); | ||
| } | ||
|
|
||
| main() | ||
| .catch((e) => { | ||
| console.error('Seed error:', e); | ||
| process.exit(1); | ||
| }) | ||
| .finally(() => prisma.$disconnect()); | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The production env example includes a specific personal email address. Prefer using a placeholder value (e.g., admin@example.com) so documentation/examples don’t embed personal data and don’t encourage copying a potentially unsafe default into production.