3 Commits

Author SHA1 Message Date
3b2ed4ddea routing improvements 2024-11-02 00:53:16 -07:00
7169bf0fb1 layout improvements 2024-11-02 00:34:21 -07:00
31d23c5e87 improve layout, nav; add sign out on user page 2024-11-01 00:06:33 -07:00
11 changed files with 86 additions and 45 deletions

View File

@@ -7,6 +7,6 @@
%sveltekit.head% %sveltekit.head%
</head> </head>
<body data-sveltekit-preload-data="hover"> <body data-sveltekit-preload-data="hover">
<div style="display: contents" class="flex flex-col min-h-screen">%sveltekit.body%</div> <div class="flex flex-col min-h-screen">%sveltekit.body%</div>
</body> </body>
</html> </html>

View File

@@ -1,6 +1,7 @@
import type { Handle } from '@sveltejs/kit'; import { type Handle, redirect } from '@sveltejs/kit';
import { dev } from '$app/environment'; import { dev } from '$app/environment';
import * as auth from '$lib/server/auth'; import * as auth from '$lib/server/auth';
import { sequence } from '@sveltejs/kit/hooks';
const handleAuth: Handle = async ({ event, resolve }) => { const handleAuth: Handle = async ({ event, resolve }) => {
const sessionId = event.cookies.get(auth.sessionCookieName); const sessionId = event.cookies.get(auth.sessionCookieName);
@@ -29,4 +30,16 @@ const handleAuth: Handle = async ({ event, resolve }) => {
return resolve(event); return resolve(event);
}; };
export const handle: Handle = handleAuth;
const authRequired = new Set([
'/user',
'/connections',
]);
const handleProtectedPaths: Handle = ({ event, resolve }) => {
if (authRequired.has(event.url.pathname) && !event.locals.user) {
return redirect(302, '/');
}
return resolve(event);
}
export const handle: Handle = sequence(handleAuth, handleProtectedPaths);

View File

@@ -6,25 +6,17 @@
let { class: className, ...rest }: {class: string | undefined | null, rest: { [p: string]: unknown }} = $props(); let { class: className, ...rest }: {class: string | undefined | null, rest: { [p: string]: unknown }} = $props();
let isLoading = $state(false); let isLoading = $state(false);
async function onSubmit() {
// event.preventDefault();
isLoading = true;
setTimeout(() => {
isLoading = false;
}, 3000);
}
</script> </script>
<div class={cn("grid gap-6", className)} {...rest}> <div class={cn("grid gap-6", className)} {...rest}>
<a href="/auth/authentik"> <form method="get" action="/auth/authentik">
<Button disabled={isLoading} onclick={onSubmit}> <Button type="submit" onclick={() => {isLoading=true}}>
{#if isLoading} {#if isLoading}
<LucideLoaderCircle class="mr-2 h-4 w-4 animate-spin" /> <LucideLoaderCircle class="mr-2 h-4 w-4 animate-spin" />
{:else} {:else}
<img class="mr-2 h-4 w-4" alt="Authentik Logo" src="https://auth.cazzzer.com/static/dist/assets/icons/icon.svg" /> <img class="mr-2 h-4 w-4" alt="Authentik Logo" src="https://auth.cazzzer.com/static/dist/assets/icons/icon.svg" />
{/if} {/if}
Authentik Sign in with Authentik
</Button> </Button>
</a> </form>
</div> </div>

View File

@@ -41,6 +41,10 @@ export async function invalidateSession(sessionId: string): Promise<void> {
await db.delete(table.session).where(eq(table.session.id, sessionId)); await db.delete(table.session).where(eq(table.session.id, sessionId));
} }
export function deleteSessionTokenCookie(event: RequestEvent) {
event.cookies.delete(sessionCookieName, { path: '/' });
}
export async function validateSession(sessionId: string) { export async function validateSession(sessionId: string) {
const [result] = await db const [result] = await db
.select({ .select({

View File

@@ -0,0 +1,9 @@
import type { LayoutServerLoad } from "./$types";
export const load: LayoutServerLoad = async (event) => {
const { user } = event.locals;
return {
user
};
};

View File

@@ -1,19 +1,30 @@
<script lang="ts"> <script lang="ts">
import '../app.css'; import '../app.css';
let { children } = $props(); import { page } from '$app/stores';
import { cn } from '$lib/utils';
const { data, children } = $props();
const { user } = data;
</script> </script>
<header class="p-4"> <header class="p-4 sm:flex">
<h1 class="text-2xl font-bold">My App</h1> <span class=" mr-6 font-bold sm:inline-block">My App</span>
<nav> <nav class="flex items-center gap-6 text-sm">
<a href="/">Home</a> <a href="/" class={cn("hover:text-foreground/80 transition-colors",
<a href="/about">About</a> $page.url.pathname === "/" ? "text-foreground" : "text-foreground/60")}>Home</a>
{#if user}
<a href="/user" class={cn("hover:text-foreground/80 transition-colors",
$page.url.pathname === "/user" ? "text-foreground" : "text-foreground/60")}>Profile</a>
{/if}
</nav> </nav>
</header> </header>
<main class="flex-grow p-4"> <main class="flex-grow p-4">
{@render children()} {@render children()}
</main> </main>
<footer class="p-4 text-center"> <!--https://github.com/sveltejs/kit/discussions/7585#discussioncomment-9997936-->
<!--Some shenanings needed to be done to get the footer position to stick correctly,
didn't work with display: contents-->
<footer class="p-4 relative text-center inset-x-0 bottom-0">
<p>&copy; 2024</p> <p>&copy; 2024</p>
</footer> </footer>

View File

@@ -1,9 +0,0 @@
import type { PageServerLoad } from "./$types";
export const load: PageServerLoad = async (event) => {
const { user } = event.locals;
return {
user
};
};

View File

@@ -5,6 +5,10 @@
const { user } = data; const { user } = data;
</script> </script>
<svelte:head>
<title>VpGen</title>
</svelte:head>
<h1>Welcome to SvelteKit</h1> <h1>Welcome to SvelteKit</h1>
{#if user } {#if user }

View File

@@ -0,0 +1,14 @@
import { fail, redirect } from "@sveltejs/kit";
import { invalidateSession, deleteSessionTokenCookie } from "$lib/server/auth";
import type { Actions } from "./$types";
export const actions: Actions = {
logout: async (event) => {
if (event.locals.session === null) {
return fail(401);
}
await invalidateSession(event.locals.session.id);
deleteSessionTokenCookie(event);
return redirect(302, "/");
}
};

View File

@@ -1,14 +0,0 @@
import { redirect } from "@sveltejs/kit";
import type { PageServerLoad } from "./$types";
export const load: PageServerLoad = async (event) => {
const { user } = event.locals;
if (!user) {
return redirect(302, "/");
}
return {
user
};
};

View File

@@ -1,8 +1,10 @@
<script lang="ts"> <script lang="ts">
import { invalidate, invalidateAll } from '$app/navigation'; import { invalidate, invalidateAll } from '$app/navigation';
import { Button } from '$lib/components/ui/button'; import { Button } from '$lib/components/ui/button';
import { LucideLoaderCircle, LucideLogOut, LucideRefreshCw } from 'lucide-svelte';
let { data } = $props(); let { data } = $props();
let isLoadingSignOut = $state(false);
function refetch() { function refetch() {
console.log("refetching"); console.log("refetching");
@@ -14,10 +16,25 @@
} }
</script> </script>
<svelte:head>
<title>User Profile</title>
</svelte:head>
<p> <p>
{JSON.stringify(data.user)} {JSON.stringify(data.user)}
</p> </p>
<Button onclick={refetch}> <Button onclick={refetch}>
<LucideRefreshCw class="mr-2 h-4 w-4" />
Invalidate Data Invalidate Data
</Button> </Button>
<form class="inline-flex" method="post" action="/auth?/logout">
<Button type="submit" onclick={() => {isLoadingSignOut = true}}>
{#if isLoadingSignOut}
<LucideLoaderCircle class="mr-2 h-4 w-4 animate-spin" />
{:else}
<LucideLogOut class="mr-2 h-4 w-4" />
{/if}
Sign Out
</Button>
</form>