40 lines
1006 B
TypeScript
40 lines
1006 B
TypeScript
import { db } from '$lib/server/db';
|
|
import { Guard } from '$lib/server/guard';
|
|
import { getUserFromSession } from '$lib/server/sessions';
|
|
import type { ServerInit } from '@sveltejs/kit';
|
|
import bcrypt from 'bcryptjs';
|
|
import { writeFileSync } from 'fs';
|
|
import { nanoid } from 'nanoid';
|
|
|
|
export const init: ServerInit = async () => {
|
|
const anyUser = db.data.users[0];
|
|
|
|
if (!anyUser) {
|
|
const pass = nanoid();
|
|
|
|
await db.update(({ users }) => {
|
|
users.push({
|
|
id: nanoid(),
|
|
name: 'admin',
|
|
password: bcrypt.hashSync(pass, 10),
|
|
admin: true,
|
|
groups: [],
|
|
devices: [],
|
|
});
|
|
});
|
|
|
|
console.log(`default admin password: ${pass}`);
|
|
console.log("saved to ./default_admin_pass.txt, don't share it and change it asap");
|
|
|
|
writeFileSync('./data/default_admin_pass.txt', pass);
|
|
}
|
|
};
|
|
|
|
export async function handle({ event, resolve }) {
|
|
const { cookies, locals } = event;
|
|
|
|
locals.guard = new Guard(await getUserFromSession(cookies.get('session')));
|
|
|
|
return await resolve(event);
|
|
}
|