From 2b0c954aa584b2e5b2d2c081ec8beac710b13b53 Mon Sep 17 00:00:00 2001 From: AlexBocken Date: Wed, 19 Jul 2023 14:52:50 +0200 Subject: [PATCH] user displayed in navbar with option to logout --- src/hooks.server.ts | 4 +- src/lib/components/Header.svelte | 12 +- src/lib/components/UserHeader.svelte | 139 ++++++++++++++++++ src/routes/(main)/+layout.server.ts | 9 ++ src/routes/(main)/+layout.svelte | 8 +- .../(rezepte)/rezepte/+layout.server.ts | 11 ++ src/routes/(rezepte)/rezepte/+layout.svelte | 6 +- src/routes/(rezepte)/rezepte/+page.svelte | 1 - .../(rezepte)/rezepte/[name]/+page.server.ts | 15 -- .../(rezepte)/rezepte/[name]/+page.svelte | 2 - src/routes/(rezepte)/rezepte/[name]/+page.ts | 10 ++ 11 files changed, 189 insertions(+), 28 deletions(-) create mode 100644 src/lib/components/UserHeader.svelte create mode 100644 src/routes/(main)/+layout.server.ts create mode 100644 src/routes/(rezepte)/rezepte/+layout.server.ts delete mode 100644 src/routes/(rezepte)/rezepte/[name]/+page.server.ts create mode 100644 src/routes/(rezepte)/rezepte/[name]/+page.ts diff --git a/src/hooks.server.ts b/src/hooks.server.ts index 632ce93..d7c2630 100644 --- a/src/hooks.server.ts +++ b/src/hooks.server.ts @@ -4,8 +4,8 @@ import { redirect } from "@sveltejs/kit" import { error } from "@sveltejs/kit" export const handle : Handle = async({event, resolve}) => { - event.locals.user = await authenticateUser(event.cookies) if(event.url.pathname.startsWith('/rezepte/edit') || event.url.pathname.startsWith('/rezepte/add')){ + event.locals.user = await authenticateUser(event.cookies) if(!event.locals.user){ throw redirect(303, "/login") } @@ -14,7 +14,7 @@ export const handle : Handle = async({event, resolve}) => { } } else if(event.url.pathname.startsWith('/abrechnung')){ - console.log(event.locals.user) + event.locals.user = await authenticateUser(event.cookies) if(!event.locals.user){ throw redirect(303, "/login") } diff --git a/src/lib/components/Header.svelte b/src/lib/components/Header.svelte index b060958..4aecbf3 100644 --- a/src/lib/components/Header.svelte +++ b/src/lib/components/Header.svelte @@ -45,7 +45,7 @@ nav[hidden]{ } :global(.site_header li), -a.entry +:global(a.entry) { list-style-type:none; transition: 100ms; @@ -53,7 +53,7 @@ a.entry user-select: none; } :global(.site_header li>a), -.entry +:global(.entry) { text-decoration: none; font-family: sans-serif; @@ -65,8 +65,8 @@ a.entry :global(.site_header li:hover), :global(.site_header li:focus-within), -.entry:hover, -.entry:focus-visible +:global(.entry:hover), +:global(.entry:focus-visible) { cursor: pointer; color: var(--red); @@ -153,7 +153,7 @@ a.entry nav[hidden]{ transform: translateX(100%); } - nav a:last-child{ + :global(nav a:last-child){ margin-bottom: 2rem; } @@ -192,7 +192,7 @@ margin-top: auto; diff --git a/src/lib/components/UserHeader.svelte b/src/lib/components/UserHeader.svelte new file mode 100644 index 0000000..aca1d59 --- /dev/null +++ b/src/lib/components/UserHeader.svelte @@ -0,0 +1,139 @@ + + + +{#if username} + +{:else} + Log In +{/if} diff --git a/src/routes/(main)/+layout.server.ts b/src/routes/(main)/+layout.server.ts new file mode 100644 index 0000000..5e6f196 --- /dev/null +++ b/src/routes/(main)/+layout.server.ts @@ -0,0 +1,9 @@ +import type {LayoutServerLoad} from './$types'; +import { authenticateUser } from '$lib/js/authenticate';; + +export const load = (async ({cookies}) => { + const user = await authenticateUser(cookies) + return { + user + } +}) satisfies LayoutServerLoad; diff --git a/src/routes/(main)/+layout.svelte b/src/routes/(main)/+layout.svelte index 84ba8d0..b4dcc9f 100644 --- a/src/routes/(main)/+layout.svelte +++ b/src/routes/(main)/+layout.svelte @@ -1,6 +1,11 @@
@@ -10,5 +15,6 @@ import Header from '$lib/components/Header.svelte'
  • Git
  • Transmission
  • +
    diff --git a/src/routes/(rezepte)/rezepte/+layout.server.ts b/src/routes/(rezepte)/rezepte/+layout.server.ts new file mode 100644 index 0000000..92aab77 --- /dev/null +++ b/src/routes/(rezepte)/rezepte/+layout.server.ts @@ -0,0 +1,11 @@ +import type {LayoutServerLoad} from './$types'; +import { authenticateUser } from '$lib/js/authenticate';; + +export const load = (async ({cookies}) => { + const user = await authenticateUser(cookies) + console.log("LOAD LAYOUT") + console.log(user) + return { + user + } +}) satisfies LayoutServerLoad; diff --git a/src/routes/(rezepte)/rezepte/+layout.svelte b/src/routes/(rezepte)/rezepte/+layout.svelte index fa72fc8..da4ce17 100644 --- a/src/routes/(rezepte)/rezepte/+layout.svelte +++ b/src/routes/(rezepte)/rezepte/+layout.svelte @@ -1,6 +1,9 @@
    @@ -11,5 +14,6 @@ import Header from '$lib/components/Header.svelte'
  • Icon
  • Stichwörter
  • +
    diff --git a/src/routes/(rezepte)/rezepte/+page.svelte b/src/routes/(rezepte)/rezepte/+page.svelte index 54e745a..29af60f 100644 --- a/src/routes/(rezepte)/rezepte/+page.svelte +++ b/src/routes/(rezepte)/rezepte/+page.svelte @@ -34,7 +34,6 @@ h1{ {/each} {/each} -

    {data.all_brief.length}

    {#if data.user && data.user.access.includes("rezepte")} {/if} diff --git a/src/routes/(rezepte)/rezepte/[name]/+page.server.ts b/src/routes/(rezepte)/rezepte/[name]/+page.server.ts deleted file mode 100644 index b66f91f..0000000 --- a/src/routes/(rezepte)/rezepte/[name]/+page.server.ts +++ /dev/null @@ -1,15 +0,0 @@ -// import { error } from '@sveltejs/kit'; -import type { PageLoad } from "./$types"; -//import { Recipe } from '../../../models/Recipe'; -//import { dbConnect, dbDisconnect } from '../../../utils/db'; -import { error } from "@sveltejs/kit"; - -export async function load({ fetch, params, locals }) { - const res = await fetch(`/api/items/${params.name}`); - let item = await res.json(); - item.user = locals.user - if(res.status != 200){ - throw error(res.status, item.message) - } - return item; -}; diff --git a/src/routes/(rezepte)/rezepte/[name]/+page.svelte b/src/routes/(rezepte)/rezepte/[name]/+page.svelte index 6b19935..cd3582c 100644 --- a/src/routes/(rezepte)/rezepte/[name]/+page.svelte +++ b/src/routes/(rezepte)/rezepte/[name]/+page.svelte @@ -242,6 +242,4 @@ h4{ -{#if data.user && data.user.access.includes("rezepte")} -{/if} diff --git a/src/routes/(rezepte)/rezepte/[name]/+page.ts b/src/routes/(rezepte)/rezepte/[name]/+page.ts new file mode 100644 index 0000000..faecdd4 --- /dev/null +++ b/src/routes/(rezepte)/rezepte/[name]/+page.ts @@ -0,0 +1,10 @@ +import { error } from "@sveltejs/kit"; + +export async function load({ fetch, params}) { + const res = await fetch(`/api/items/${params.name}`); + let item = await res.json(); + if(!res.ok){ + throw error(res.status, item.message) + } + return item; +}