Compare commits
1 Commits
master
...
c416057349
| Author | SHA1 | Date | |
|---|---|---|---|
|
c416057349
|
@@ -7,6 +7,6 @@
|
||||
%sveltekit.head%
|
||||
</head>
|
||||
<body data-sveltekit-preload-data="hover">
|
||||
<div class="flex flex-col min-h-screen">%sveltekit.body%</div>
|
||||
<div style="display: contents" class="flex flex-col min-h-screen">%sveltekit.body%</div>
|
||||
</body>
|
||||
</html>
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
import { type Handle, redirect } from '@sveltejs/kit';
|
||||
import type { Handle } from '@sveltejs/kit';
|
||||
import { dev } from '$app/environment';
|
||||
import * as auth from '$lib/server/auth';
|
||||
import { sequence } from '@sveltejs/kit/hooks';
|
||||
|
||||
const handleAuth: Handle = async ({ event, resolve }) => {
|
||||
const sessionId = event.cookies.get(auth.sessionCookieName);
|
||||
@@ -30,16 +29,4 @@ const handleAuth: Handle = async ({ event, resolve }) => {
|
||||
return resolve(event);
|
||||
};
|
||||
|
||||
|
||||
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);
|
||||
export const handle: Handle = handleAuth;
|
||||
|
||||
@@ -6,17 +6,25 @@
|
||||
let { class: className, ...rest }: {class: string | undefined | null, rest: { [p: string]: unknown }} = $props();
|
||||
|
||||
let isLoading = $state(false);
|
||||
async function onSubmit() {
|
||||
// event.preventDefault();
|
||||
isLoading = true;
|
||||
|
||||
setTimeout(() => {
|
||||
isLoading = false;
|
||||
}, 3000);
|
||||
}
|
||||
</script>
|
||||
|
||||
<div class={cn("grid gap-6", className)} {...rest}>
|
||||
<form method="get" action="/auth/authentik">
|
||||
<Button type="submit" onclick={() => {isLoading=true}}>
|
||||
<a href="/auth/authentik">
|
||||
<Button disabled={isLoading} onclick={onSubmit}>
|
||||
{#if isLoading}
|
||||
<LucideLoaderCircle class="mr-2 h-4 w-4 animate-spin" />
|
||||
{:else}
|
||||
<img class="mr-2 h-4 w-4" alt="Authentik Logo" src="https://auth.cazzzer.com/static/dist/assets/icons/icon.svg" />
|
||||
{/if}
|
||||
Sign in with Authentik
|
||||
Authentik
|
||||
</Button>
|
||||
</form>
|
||||
</a>
|
||||
</div>
|
||||
|
||||
@@ -41,10 +41,6 @@ export async function invalidateSession(sessionId: string): Promise<void> {
|
||||
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) {
|
||||
const [result] = await db
|
||||
.select({
|
||||
|
||||
@@ -1,9 +1,9 @@
|
||||
import { Authentik } from 'arctic';
|
||||
import * as env from '$env/static/private';
|
||||
import { Authentik } from "arctic";
|
||||
import * as env from "$env/static/private"
|
||||
|
||||
export const authentik = new Authentik(
|
||||
env.AUTH_DOMAIN,
|
||||
env.AUTH_CLIENT_ID,
|
||||
env.AUTH_CLIENT_SECRET,
|
||||
env.AUTH_REDIRECT_URI
|
||||
);
|
||||
const domain = env.AUTH_DOMAIN;
|
||||
const clientId = env.AUTH_CLIENT_ID;
|
||||
const clientSecret = env.AUTH_CLIENT_SECRET;
|
||||
const redirectURI = env.AUTH_REDIRECT_URI;
|
||||
|
||||
export const authentik = new Authentik(domain, clientId, clientSecret, redirectURI);
|
||||
|
||||
@@ -1,9 +0,0 @@
|
||||
import type { LayoutServerLoad } from "./$types";
|
||||
|
||||
export const load: LayoutServerLoad = async (event) => {
|
||||
const { user } = event.locals;
|
||||
|
||||
return {
|
||||
user
|
||||
};
|
||||
};
|
||||
@@ -1,30 +1,19 @@
|
||||
<script lang="ts">
|
||||
import '../app.css';
|
||||
import { page } from '$app/stores';
|
||||
import { cn } from '$lib/utils';
|
||||
|
||||
const { data, children } = $props();
|
||||
const { user } = data;
|
||||
let { children } = $props();
|
||||
</script>
|
||||
|
||||
<header class="p-4 sm:flex">
|
||||
<span class=" mr-6 font-bold sm:inline-block">My App</span>
|
||||
<nav class="flex items-center gap-6 text-sm">
|
||||
<a href="/" class={cn("hover:text-foreground/80 transition-colors",
|
||||
$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}
|
||||
<header class="p-4">
|
||||
<h1 class="text-2xl font-bold">My App</h1>
|
||||
<nav>
|
||||
<a href="/">Home</a>
|
||||
<a href="/about">About</a>
|
||||
</nav>
|
||||
</header>
|
||||
<main class="flex-grow p-4">
|
||||
{@render children()}
|
||||
</main>
|
||||
|
||||
<!--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">
|
||||
<footer class="p-4 text-center">
|
||||
<p>© 2024</p>
|
||||
</footer>
|
||||
|
||||
@@ -1,18 +1,7 @@
|
||||
<script lang="ts">
|
||||
import { AuthForm } from '$lib/components/app/auth-form';
|
||||
|
||||
const { data } = $props();
|
||||
const { user } = data;
|
||||
import { AuthForm } from "$lib/components/app/auth-form";
|
||||
</script>
|
||||
|
||||
<svelte:head>
|
||||
<title>VpGen</title>
|
||||
</svelte:head>
|
||||
|
||||
<h1>Welcome to SvelteKit</h1>
|
||||
|
||||
{#if user }
|
||||
<p>Hi {user.name}</p>
|
||||
{:else}
|
||||
<AuthForm class="p-4" />
|
||||
{/if}
|
||||
<AuthForm />
|
||||
|
||||
@@ -1,14 +0,0 @@
|
||||
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, "/");
|
||||
}
|
||||
};
|
||||
@@ -1,40 +0,0 @@
|
||||
<script lang="ts">
|
||||
import { invalidate, invalidateAll } from '$app/navigation';
|
||||
import { Button } from '$lib/components/ui/button';
|
||||
import { LucideLoaderCircle, LucideLogOut, LucideRefreshCw } from 'lucide-svelte';
|
||||
|
||||
let { data } = $props();
|
||||
let isLoadingSignOut = $state(false);
|
||||
|
||||
function refetch() {
|
||||
console.log("refetching");
|
||||
invalidate((url) => {
|
||||
console.log("invalidation url", url);
|
||||
return true;
|
||||
});
|
||||
invalidateAll();
|
||||
}
|
||||
</script>
|
||||
|
||||
<svelte:head>
|
||||
<title>User Profile</title>
|
||||
</svelte:head>
|
||||
|
||||
<p>
|
||||
{JSON.stringify(data.user)}
|
||||
</p>
|
||||
|
||||
<Button onclick={refetch}>
|
||||
<LucideRefreshCw class="mr-2 h-4 w-4" />
|
||||
Invalidate Data
|
||||
</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>
|
||||
Reference in New Issue
Block a user