-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Seed admin user on server start. * Optionally disable public user registration.
- Loading branch information
1 parent
ac6e9ad
commit 73de7ef
Showing
5 changed files
with
101 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
//@ts-check | ||
import { PrismaClient } from "@prisma/client"; | ||
import bycrypt from "bcryptjs"; | ||
|
||
const db = new PrismaClient(); | ||
|
||
const log = (/** @type {string} */ message) => | ||
console.log(`ADMIN SEED: ${message}`); | ||
|
||
async function main() { | ||
const { ADMIN_USERNAME, ADMIN_PASSWORD } = process.env; | ||
|
||
if (!ADMIN_USERNAME || !ADMIN_PASSWORD) { | ||
return; | ||
} | ||
|
||
const admin = await db.user.findUnique({ | ||
where: { | ||
username: ADMIN_USERNAME, | ||
}, | ||
}); | ||
|
||
if (admin) { | ||
log(`Admin user '${ADMIN_USERNAME}' already exists`); | ||
return; | ||
} | ||
|
||
log(`Seeding admin user '${ADMIN_USERNAME}'`); | ||
|
||
await db.user.create({ | ||
data: { | ||
username: ADMIN_USERNAME, | ||
passwordHash: await bycrypt.hash(ADMIN_PASSWORD, 10), | ||
isAdmin: true, | ||
}, | ||
}); | ||
|
||
log(`Admin user '${ADMIN_USERNAME}' created`); | ||
} | ||
|
||
main().catch((e) => { | ||
console.error(e); | ||
process.exit(1); | ||
}); |