7 Commits

11 changed files with 38 additions and 15 deletions

View File

@@ -21,7 +21,7 @@
<h1 class="text-3xl text-slate-400 font-bold">Tatort</h1> <h1 class="text-3xl text-slate-400 font-bold">Tatort</h1>
<div class="lg:flex lg:justify-end w-48"> <div class="lg:flex lg:justify-end w-48">
{#if data.user} {#if data.user}
<form method="POST" action="{ROUTE_NAMES.ANMELDUNG_LOGOUT}"> <form method="POST" action="{ROUTE_NAMES.LOGOUT}">
<input type="hidden" /> <input type="hidden" />
<button type="submit" class="text-sm font-semibold leading-6 text-gray-900" <button type="submit" class="text-sm font-semibold leading-6 text-gray-900"
><span ><span

View File

@@ -12,7 +12,8 @@ export const loginUser = async ({ request, cookies }: { request: Request; cookie
const token = authenticate(user, password); const token = authenticate(user, password);
if (!token) return fail(400, { user, incorrect: true }); if (!token) return fail(400, { user, incorrect: true,
message: "Ungültige Zugangsdaten" });
cookies.set(COOKIE_NAME, token, { cookies.set(COOKIE_NAME, token, {
path: ROUTE_NAMES.ROOT, path: ROUTE_NAMES.ROOT,

View File

@@ -1,4 +1,4 @@
import { redirect, type ServerLoadEvent } from '@sveltejs/kit'; import { type ServerLoadEvent } from '@sveltejs/kit';
import type { PageServerLoad } from '../anmeldung/$types'; import type { PageServerLoad } from '../anmeldung/$types';
import { ROUTE_NAMES } from '..'; import { ROUTE_NAMES } from '..';

View File

@@ -1,5 +1,6 @@
import { loginUser } from '$lib/server/authService'; import { loginUser, logoutUser } from '$lib/server/authService';
export const actions = { export const actions = {
default: ({ request, cookies }) => loginUser({ request, cookies }), login: ({ request, cookies }) => loginUser({ request, cookies }),
logout: (event) => logoutUser(event),
} as const; } as const;

View File

@@ -72,7 +72,7 @@
<div class="relative mt-5 bg-gray-50 rounded-xl shadow-xl p-3 pt-1"> <div class="relative mt-5 bg-gray-50 rounded-xl shadow-xl p-3 pt-1">
<div class="mt-10"> <div class="mt-10">
<form method="POST"> <form action="{ROUTE_NAMES.LOGIN}" method="POST">
<div> <div>
<label for="user" class="text-sm font-medium leading-6 text-gray-900">Name</label> <label for="user" class="text-sm font-medium leading-6 text-gray-900">Name</label>
<div class="mt-2"> <div class="mt-2">
@@ -103,7 +103,7 @@
</div> </div>
</div> </div>
{#if form?.incorrect} {#if form?.incorrect}
Wrong credentials <p class="block text-sm leading-6 text-red-900 mt-2">{form.message}</p>
{/if} {/if}
<div class="flex justify-end"> <div class="flex justify-end">
<Button type="submit" class="mt-5">Anmelden</Button> <Button type="submit" class="mt-5">Anmelden</Button>

View File

@@ -1,7 +1,12 @@
import { getVorgaenge } from '$lib/server/vorgangService'; import { getVorgaenge } from '$lib/server/vorgangService';
import type { PageServerLoad } from '../../(token-based)/view/$types'; import type { PageServerLoad } from '../../(token-based)/view/$types';
import { error } from '@sveltejs/kit';
export const load: PageServerLoad = async (event) => {
if (!event.locals.user) {
error(404, 'Not Found')
}
export const load: PageServerLoad = async () => {
const vorgangList = getVorgaenge(); const vorgangList = getVorgaenge();
return { return {

View File

@@ -1,6 +1,6 @@
import { Readable } from 'stream'; import { Readable } from 'stream';
import { BUCKET, client } from '$lib/minio'; import { BUCKET, client } from '$lib/minio';
import { fail } from '@sveltejs/kit'; import { fail, error } from '@sveltejs/kit';
import { v4 as uuidv4 } from 'uuid'; import { v4 as uuidv4 } from 'uuid';
import { db } from '$lib/server/dbService'; import { db } from '$lib/server/dbService';
@@ -123,3 +123,10 @@ export const actions = {
return { etag, error }; return { etag, error };
} }
}; };
export const load: PageServerLoad = async (event) => {
if (!event.locals.user) {
error(404, 'Not found')
}
};

View File

@@ -0,0 +1,8 @@
import type { PageServerLoad } from '../../(token-based)/view/$types';
import { error } from '@sveltejs/kit';
export const load: PageServerLoad = async (event) => {
if (!event.locals.user) {
error(404, 'Not Found')
}
};

View File

@@ -16,8 +16,8 @@ export const ROUTE_NAMES = {
// Anmeldung: actions // Anmeldung: actions
ANMELDUNG: '/anmeldung', ANMELDUNG: '/anmeldung',
ANMELDUNG_LOGIN: '/anmeldung?/login', LOGIN: '/?/login',
ANMELDUNG_LOGOUT: '/anmeldung?/logout', LOGOUT: '/?/logout',
ANMELDUNG_GET_VORGANG_BY_TOKEN: '/anmeldung?/getVorgangByToken', ANMELDUNG_GET_VORGANG_BY_TOKEN: '/anmeldung?/getVorgangByToken',
ANMELDUNG_VORGANG_PARAM: (vorgangToken: string) => `/anmeldung?vorgang=${vorgangToken}` ANMELDUNG_VORGANG_PARAM: (vorgangToken: string) => `/anmeldung?vorgang=${vorgangToken}`
}; };

View File

@@ -27,6 +27,7 @@ describe('Vorgang Anzeige via Token', () => {
const mockRequest = { const mockRequest = {
formData: vi.fn().mockResolvedValue(formData) formData: vi.fn().mockResolvedValue(formData)
}; };
vi.mocked(vorgangPINValidation).mockReturnValueOnce(true);
const cookiesSet = vi.fn(); const cookiesSet = vi.fn();
@@ -39,7 +40,7 @@ describe('Vorgang Anzeige via Token', () => {
let thrownRedirect: Redirect | undefined; let thrownRedirect: Redirect | undefined;
try { try {
await actions.getVorgangByToken(event); await actions.default(event);
} catch (e) { } catch (e) {
thrownRedirect = e as Redirect; thrownRedirect = e as Redirect;
} }
@@ -70,9 +71,9 @@ describe('Vorgang Anzeige via Token', () => {
set: cookiesSet set: cookiesSet
} }
}; };
const result = await actions.getVorgangByToken(event); const result = await actions.default(event);
expect(result.status).toBe(400); expect(result.status).toBe(400);
expect(result.data.message).toMatch(/fehlen|ungültig/i); expect(result.data.message).toMatch(/PIN eingeben/i);
// Cookie wird nicht gesetzt // Cookie wird nicht gesetzt
expect(cookiesSet).not.toHaveBeenCalled(); expect(cookiesSet).not.toHaveBeenCalled();
}); });

View File

@@ -4,7 +4,7 @@ import { ROUTE_NAMES } from '../../src/routes';
import { baseData, mockEvent } from '../fixtures'; import { baseData, mockEvent } from '../fixtures';
describe('+layout.server load(): Teste korrekte URL', () => { describe('+layout.server load(): Teste korrekte URL', () => {
test('Werfe redirect zu /anmeldung wenn User nicht eingeloggt', async () => { test('Werfe keinen Redirect und gebe nichts zurück', async () => {
const mockEvent = { const mockEvent = {
locals: { locals: {
user: null user: null