add authentik login

This commit is contained in:
2024-10-31 02:00:15 -07:00
parent aab19fa6c7
commit 0901e242eb
8 changed files with 144 additions and 12 deletions

View File

@@ -6,8 +6,8 @@
let { class: className, ...rest }: {class: string | undefined | null, rest: { [p: string]: unknown }} = $props();
let isLoading = $state(false);
async function onSubmit(event: Event) {
event.preventDefault();
async function onSubmit() {
// event.preventDefault();
isLoading = true;
setTimeout(() => {
@@ -17,12 +17,14 @@
</script>
<div class={cn("grid gap-6", className)} {...rest}>
<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}
Authentik
</Button>
<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}
Authentik
</Button>
</a>
</div>

View File

@@ -3,6 +3,8 @@ import { sha256 } from '@oslojs/crypto/sha2';
import { encodeBase32LowerCaseNoPadding, encodeHexLowerCase } from '@oslojs/encoding';
import { db } from '$lib/server/db';
import * as table from '$lib/server/db/schema';
import type { RequestEvent } from '@sveltejs/kit';
import { dev } from '$app/environment';
const DAY_IN_MS = 1000 * 60 * 60 * 24;
@@ -25,6 +27,16 @@ export async function createSession(userId: string): Promise<table.Session> {
return session;
}
export function setSessionTokenCookie(event: RequestEvent, sessionId: string, expiresAt: Date) {
event.cookies.set(sessionCookieName, sessionId, {
path: '/',
sameSite: 'lax',
httpOnly: true,
expires: expiresAt,
secure: !dev,
});
}
export async function invalidateSession(sessionId: string): Promise<void> {
await db.delete(table.session).where(eq(table.session.id, sessionId));
}
@@ -33,7 +45,7 @@ export async function validateSession(sessionId: string) {
const [result] = await db
.select({
// Adjust user table here to tweak returned data
user: { id: table.user.id, username: table.user.username },
user: { id: table.user.id, username: table.user.username, name: table.user.name },
session: table.session
})
.from(table.session)

9
src/lib/server/oauth.ts Normal file
View File

@@ -0,0 +1,9 @@
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
);