mirror of https://github.com/AlphaNecron/Void.git
fix(prisma): moved default user creation to seed.ts
This commit is contained in:
parent
9fb221fdc2
commit
eae855fb32
|
@ -16,6 +16,9 @@
|
|||
"prepare": "husky install",
|
||||
"lint": "next lint --fix"
|
||||
},
|
||||
"prisma": {
|
||||
"seed": "ts-node --compiler-options {\"module\":\"CommonJS\"} prisma/seed.ts"
|
||||
},
|
||||
"dependencies": {
|
||||
"@chakra-ui/react": "^1.6.8",
|
||||
"@emotion/react": "^11",
|
||||
|
|
|
@ -0,0 +1,29 @@
|
|||
import { PrismaClient } from '@prisma/client';
|
||||
import { hashPassword, generateToken } from '../src/lib/utils';
|
||||
import { info } from '../src/lib/logger';
|
||||
|
||||
const prisma = new PrismaClient();
|
||||
|
||||
async function main() {
|
||||
const count = await prisma.user.count();
|
||||
if (count === 0) {
|
||||
const user = await prisma.user.create({
|
||||
data: {
|
||||
username: 'admin',
|
||||
password: await hashPassword('voiduser'),
|
||||
token: generateToken(),
|
||||
isAdmin: true
|
||||
}
|
||||
});
|
||||
info('SEED', `Created default user with username "${user.username}" and password "voiduser"`);
|
||||
}
|
||||
}
|
||||
|
||||
main()
|
||||
.catch(e => {
|
||||
console.error(e);
|
||||
process.exit(1);
|
||||
})
|
||||
.finally(async () => {
|
||||
await prisma.$disconnect();
|
||||
});
|
|
@ -19,10 +19,11 @@ const dev = process.env.NODE_ENV === 'development';
|
|||
try {
|
||||
const config = await validateConfig(configReader());
|
||||
const data = await prismaRun(config.core.database_url, ['migrate', 'status'], true);
|
||||
if (data.match(/Following migration[s]? have not yet been applied/)) {
|
||||
if (data.match(/Following migrations? have not yet been applied/)) {
|
||||
info('DB', 'Some migrations are not applied, applying them now...');
|
||||
await deployDb(config);
|
||||
info('DB', 'Finished applying migrations');
|
||||
await prismaRun(config.core.database_url, ['db', 'seed'], false)
|
||||
}
|
||||
process.env.DATABASE_URL = config.core.database_url;
|
||||
await stat('./.next');
|
||||
|
|
|
@ -10,11 +10,11 @@ const validator = yup.object({
|
|||
}).required(),
|
||||
bot: yup.object({
|
||||
enabled: yup.bool().default(false),
|
||||
prefix: yup.string().min(1).required(),
|
||||
prefix: yup.string().min(1),
|
||||
token: yup.string(),
|
||||
admins: yup.array().default([]),
|
||||
log_channel: yup.string(),
|
||||
hostname: yup.string().required()
|
||||
hostname: yup.string()
|
||||
}),
|
||||
shortener: yup.object({
|
||||
allow_vanity: yup.bool().default(false),
|
||||
|
|
|
@ -32,6 +32,7 @@ module.exports = () => {
|
|||
error('CONFIG', 'Config file not found, please create one.');
|
||||
return tryReadEnv();
|
||||
} else {
|
||||
if (process.env.JEST_WORKER_ID) return;
|
||||
info('CONFIG', 'Reading config file');
|
||||
const str = readFileSync(join(process.cwd(), 'config.toml'), 'utf8');
|
||||
const parsed = require('@iarna/toml/parse-string')(str);
|
||||
|
|
|
@ -12,10 +12,17 @@ export function verifyPassword(s: string, hash: string): Promise<boolean> {
|
|||
return verify(hash, s);
|
||||
}
|
||||
|
||||
export function createToken() {
|
||||
export function generateToken() {
|
||||
return generate(24) + '.' + Buffer.from(Date.now().toString()).toString('base64').replace(/=+$/, '');
|
||||
}
|
||||
|
||||
export function generateUuid() {
|
||||
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
|
||||
var r = Math.random() * 16 | 0, v = c == 'x' ? r : (r & 0x3 | 0x8);
|
||||
return v.toString(16);
|
||||
});
|
||||
}
|
||||
|
||||
export function sign(value: string, secret: string): string {
|
||||
const signed = value + ':' + createHmac('sha256', secret)
|
||||
.update(value)
|
||||
|
|
|
@ -1,23 +1,11 @@
|
|||
import { info } from 'lib/logger';
|
||||
import prisma from 'lib/prisma';
|
||||
import { verifyPassword, createToken, hashPassword } from 'lib/utils';
|
||||
import { verifyPassword } from 'lib/utils';
|
||||
import { NextApiReq, NextApiRes, withVoid } from 'middleware/withVoid';
|
||||
|
||||
async function handler(req: NextApiReq, res: NextApiRes) {
|
||||
if (req.method !== 'POST') return res.status(405).end();
|
||||
const { username, password } = req.body as { username: string, password: string };
|
||||
const users = await prisma.user.findMany();
|
||||
if (users.length === 0) {
|
||||
const user = await prisma.user.create({
|
||||
data: {
|
||||
username: 'admin',
|
||||
password: await hashPassword('voiduser'),
|
||||
token: createToken(),
|
||||
isAdmin: true
|
||||
}
|
||||
});
|
||||
info('SEED', `Created default user with username "${user.username}" and password "voiduser"`);
|
||||
}
|
||||
const user = await prisma.user.findFirst({
|
||||
where: {
|
||||
username
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
import { info } from 'lib/logger';
|
||||
import prisma from 'lib/prisma';
|
||||
import { createToken } from 'lib/utils';
|
||||
import { generateToken } from 'lib/utils';
|
||||
import { NextApiReq, NextApiRes, withVoid } from 'middleware/withVoid';
|
||||
|
||||
async function handler(req: NextApiReq, res: NextApiRes) {
|
||||
|
@ -12,7 +12,7 @@ async function handler(req: NextApiReq, res: NextApiRes) {
|
|||
id: user.id
|
||||
},
|
||||
data: {
|
||||
token: createToken()
|
||||
token: generateToken()
|
||||
}
|
||||
});
|
||||
info('USER', `User ${user.username} (${user.id}) reset their token`);
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
import { info } from 'lib/logger';
|
||||
import prisma from 'lib/prisma';
|
||||
import { createToken, hashPassword } from 'lib/utils';
|
||||
import { generateToken, hashPassword } from 'lib/utils';
|
||||
import { NextApiReq, NextApiRes, withVoid } from 'middleware/withVoid';
|
||||
|
||||
async function handler(req: NextApiReq, res: NextApiRes) {
|
||||
|
@ -37,7 +37,7 @@ async function handler(req: NextApiReq, res: NextApiRes) {
|
|||
data: {
|
||||
password: hashed,
|
||||
username,
|
||||
token: createToken(),
|
||||
token: generateToken(),
|
||||
isAdmin
|
||||
}
|
||||
});
|
||||
|
|
|
@ -44,5 +44,8 @@
|
|||
"exclude": [
|
||||
"node_modules",
|
||||
"uploads"
|
||||
],
|
||||
"collectCoverageFrom": [
|
||||
"!src/lib/config.ts"
|
||||
]
|
||||
}
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
import { Message, MessageEmbed } from 'discord.js';
|
||||
import { info } from '../../src/lib/logger';
|
||||
import prisma from '../../src/lib/prisma';
|
||||
import { createToken, hashPassword } from '../../src/lib/utils';
|
||||
import { generateToken, hashPassword } from '../../src/lib/utils';
|
||||
import { logger } from '../index';
|
||||
|
||||
const user = {
|
||||
|
@ -30,7 +30,7 @@ const user = {
|
|||
const newUser = await prisma.user.create({
|
||||
data: {
|
||||
username,
|
||||
token: createToken(),
|
||||
token: generateToken(),
|
||||
password: hashed,
|
||||
}
|
||||
});
|
||||
|
|
Loading…
Reference in New Issue