+
🔐 Admin-Passwort ändern
+
+
+
+ {#if error}
+
{error}
+ {/if}
+ {#if message}
+
{message}
+ {/if}
+
+
+
+
+
\ No newline at end of file
diff --git a/src/routes/api/admin/change-password/+server.ts b/src/routes/api/admin/change-password/+server.ts
new file mode 100644
index 0000000..996a501
--- /dev/null
+++ b/src/routes/api/admin/change-password/+server.ts
@@ -0,0 +1,30 @@
+import { json } from '@sveltejs/kit';
+import type { RequestHandler } from './$types';
+import { PrismaClient } from '@prisma/client';
+import bcrypt from 'bcryptjs';
+
+const prisma = new PrismaClient();
+
+function checkAuth(cookies: any) {
+ return cookies.get('admin_session') === 'true';
+}
+
+export const POST: RequestHandler = async ({ request, cookies }) => {
+ if (!checkAuth(cookies)) return new Response('Nicht erlaubt', { status: 401 });
+
+ const { oldPassword, newPassword } = await request.json();
+
+ const admin = await prisma.admin.findUnique({ where: { id: 1 } });
+ if (!admin) return json({ error: 'Admin nicht gefunden' }, { status: 500 });
+
+ const isValid = await bcrypt.compare(oldPassword, admin.password);
+ if (!isValid) return json({ error: 'Falsches Passwort' }, { status: 401 });
+
+ const newHashed = await bcrypt.hash(newPassword, 10);
+ await prisma.admin.update({
+ where: { id: 1 },
+ data: { password: newHashed }
+ });
+
+ return json({ success: true });
+};
\ No newline at end of file
diff --git a/src/routes/api/admin/login/+server.ts b/src/routes/api/admin/login/+server.ts
index a792187..f7ac3de 100644
--- a/src/routes/api/admin/login/+server.ts
+++ b/src/routes/api/admin/login/+server.ts
@@ -1,20 +1,29 @@
import { json } from '@sveltejs/kit';
import type { RequestHandler } from './$types';
+import { PrismaClient } from '@prisma/client';
+import bcrypt from 'bcryptjs';
-const ADMIN_PASS = import.meta.env.VITE_ADMIN_PASS;
+const prisma = new PrismaClient();
export const POST: RequestHandler = async ({ request, cookies }) => {
const { passwort } = await request.json();
- if (passwort === ADMIN_PASS) {
- cookies.set('admin_session', 'true', {
- path: '/',
- httpOnly: true,
- sameSite: 'strict',
- maxAge: 60 * 60 * 4 // 4 Stunden
- });
- return json({ success: true });
+ const admin = await prisma.admin.findUnique({ where: { id: 1 } });
+ if (!admin) {
+ return json({ error: 'Kein Admin gefunden' }, { status: 500 });
}
- return json({ error: 'Falsches Passwort' }, { status: 401 });
-};
+ const isValid = await bcrypt.compare(passwort, admin.password);
+ if (!isValid) {
+ return json({ error: 'Falsches Passwort' }, { status: 401 });
+ }
+
+ cookies.set('admin_session', 'true', {
+ path: '/',
+ httpOnly: true,
+ sameSite: 'strict',
+ maxAge: 60 * 60 * 4 // 4 Stunden
+ });
+
+ return json({ success: true });
+};
\ No newline at end of file
diff --git a/tsconfig.json b/tsconfig.json
index 0b2d886..a3995c4 100644
--- a/tsconfig.json
+++ b/tsconfig.json
@@ -9,7 +9,12 @@
"skipLibCheck": true,
"sourceMap": true,
"strict": true,
- "moduleResolution": "bundler"
+ "module": "ESNext",
+ "target": "ES2020",
+ "moduleResolution": "Bundler"
+ },
+ "ts-node": {
+ "esm": true
}
// Path aliases are handled by https://svelte.dev/docs/kit/configuration#alias
// except $lib which is handled by https://svelte.dev/docs/kit/configuration#files