Compare commits
2 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| db8fa8928d | |||
| 5777e4910e |
+1
-1
@@ -1,7 +1,7 @@
|
|||||||
{
|
{
|
||||||
"name": "clean",
|
"name": "clean",
|
||||||
"private": true,
|
"private": true,
|
||||||
"version": "0.6.4",
|
"version": "0.6.5",
|
||||||
"type": "module",
|
"type": "module",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"dev:types": "bun run type:generate && bun --bun vite dev --host",
|
"dev:types": "bun run type:generate && bun --bun vite dev --host",
|
||||||
|
|||||||
+31
-1
@@ -4,10 +4,12 @@ import { building, dev } from '$app/environment';
|
|||||||
import { getAuth } from '$lib/auth/server';
|
import { getAuth } from '$lib/auth/server';
|
||||||
import { getTextDirection } from '$lib/paraglide/runtime';
|
import { getTextDirection } from '$lib/paraglide/runtime';
|
||||||
import { paraglideMiddleware } from '$lib/paraglide/server';
|
import { paraglideMiddleware } from '$lib/paraglide/server';
|
||||||
|
import { db } from '$lib/server/db';
|
||||||
import { getConfig } from '$lib/server/config';
|
import { getConfig } from '$lib/server/config';
|
||||||
import { svelteKitHandler } from 'better-auth/svelte-kit';
|
import { svelteKitHandler } from 'better-auth/svelte-kit';
|
||||||
|
import { randomBytes } from 'node:crypto';
|
||||||
|
|
||||||
export const init: ServerInit = () => {
|
export const init: ServerInit = async () => {
|
||||||
const env = process.env;
|
const env = process.env;
|
||||||
const errors: string[] = [];
|
const errors: string[] = [];
|
||||||
|
|
||||||
@@ -20,6 +22,34 @@ export const init: ServerInit = () => {
|
|||||||
|
|
||||||
if (errors.length)
|
if (errors.length)
|
||||||
throw new Error(`Invalid environment:\n - ${errors.join('\n - ')}`);
|
throw new Error(`Invalid environment:\n - ${errors.join('\n - ')}`);
|
||||||
|
|
||||||
|
// Bootstrap an admin if the user table is empty.
|
||||||
|
const userCount = db.$client.prepare('SELECT COUNT(*) AS c FROM user').get() as { c: number };
|
||||||
|
if (userCount.c > 0) return;
|
||||||
|
|
||||||
|
const username = env.ADMIN_USERNAME?.trim() || 'admin';
|
||||||
|
const email = env.ADMIN_EMAIL?.trim() || `${username}@example.com`;
|
||||||
|
const provided = env.ADMIN_PASSWORD?.trim();
|
||||||
|
const password = provided || randomBytes(18).toString('base64url');
|
||||||
|
|
||||||
|
await getAuth().api.createUser({
|
||||||
|
body: {
|
||||||
|
email,
|
||||||
|
name: username,
|
||||||
|
password,
|
||||||
|
role: 'admin',
|
||||||
|
data: { username, displayUsername: username, emailVerified: true }
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
const line = '━'.repeat(64);
|
||||||
|
if (provided) {
|
||||||
|
console.log(`${line}\nBootstrap admin "${username}" created (ADMIN_PASSWORD).\n${line}`);
|
||||||
|
} else {
|
||||||
|
console.log(
|
||||||
|
`${line}\nBootstrap admin created — sign in with these and rotate immediately:\n username: ${username}\n password: ${password}\nSet ADMIN_PASSWORD env to pick your own.\n${line}`
|
||||||
|
);
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
const handleBetterAuth: Handle = async ({ event, resolve }) => {
|
const handleBetterAuth: Handle = async ({ event, resolve }) => {
|
||||||
|
|||||||
+4
-1
@@ -5,11 +5,14 @@ import adapter from 'svelte-adapter-bun';
|
|||||||
import { defineConfig } from 'vite';
|
import { defineConfig } from 'vite';
|
||||||
|
|
||||||
export default defineConfig({
|
export default defineConfig({
|
||||||
|
build: {
|
||||||
|
rollupOptions: { external: ['async_hooks'] }
|
||||||
|
},
|
||||||
plugins: [
|
plugins: [
|
||||||
tailwindcss(),
|
tailwindcss(),
|
||||||
sveltekit({
|
sveltekit({
|
||||||
adapter: adapter({
|
adapter: adapter({
|
||||||
precompress:true
|
precompress: true
|
||||||
}),
|
}),
|
||||||
compilerOptions: {
|
compilerOptions: {
|
||||||
experimental: { async: true },
|
experimental: { async: true },
|
||||||
|
|||||||
Reference in New Issue
Block a user