added default admin logic

This commit is contained in:
Laurin
2022-10-20 16:08:10 +02:00
committed by La_Felx
parent 82ecca0173
commit ff13a95eac
2 changed files with 29 additions and 0 deletions
+25
View File
@@ -0,0 +1,25 @@
import prisma from "../../lib/prisma";
import crypto from "crypto";
import argon2 from "argon2";
import logger from "../error/logger";
require("express-async-errors");
export const generateDefaultCredentials = async () => {
const name: string = "Detleph_" + crypto.randomBytes(3).toString("base64url");
const pw: string = crypto.randomBytes(8).toString("base64url");
await prisma.admin.upsert({
where: { id: 1 },
create: {
name,
password: await argon2.hash(pw, { type: argon2.argon2id }),
permission_level: "ELEVATED",
},
update: {},
});
logger.notice("Default admin name is " + name);
logger.notice("Confidential password: " + pw);
};
+4
View File
@@ -19,6 +19,7 @@ import roleRouter from "./Routes/role.routes";
import participantRouter from "./Routes/participant.routes"; import participantRouter from "./Routes/participant.routes";
import { notFoundHandler, rootHandler } from "./Middleware/error/defaultRoutes"; import { notFoundHandler, rootHandler } from "./Middleware/error/defaultRoutes";
import { env } from "process"; import { env } from "process";
import { generateDefaultCredentials } from "./Middleware/auth/defaultAdmin";
// Set up async error handling // Set up async error handling
require("express-async-errors"); require("express-async-errors");
@@ -34,6 +35,7 @@ async function main() {
"This mode should not be used in any production-near environment as it is significantly less secure than the production mode" "This mode should not be used in any production-near environment as it is significantly less secure than the production mode"
); );
generateDefaultCredentials();
// TODO: How should you login to the prod server by default? Maybe random password? // TODO: How should you login to the prod server by default? Maybe random password?
await prisma.admin.upsert({ await prisma.admin.upsert({
where: { id: 1 }, where: { id: 1 },
@@ -50,6 +52,8 @@ async function main() {
} else { } else {
logger.info("Using production mode"); logger.info("Using production mode");
generateDefaultCredentials();
// Configure cors // Configure cors
/*app.use( /*app.use(
cors({ cors({