refactor: extend auth.js types globally (resolves #7)
Commit / ci (push) Has been cancelled
PullRequest / publish (pull_request) Failing after 2m41s

This commit is contained in:
2026-04-03 13:06:47 +02:00
parent 2e16cf9d51
commit 8483ab9e84
2 changed files with 15 additions and 8 deletions
+11
View File
@@ -1,3 +1,5 @@
import { DefaultSession } from '@auth/sveltekit';
// See https://svelte.dev/docs/kit/types#app.d.ts
// for information about these interfaces
declare global {
@@ -10,4 +12,13 @@ declare global {
}
}
declare module '@auth/sveltekit' {
interface Session {
user: {
id: string;
email: string;
} & DefaultSession['user'];
}
}
export {};
+4 -8
View File
@@ -1,4 +1,4 @@
import type { Session, User } from '@auth/sveltekit';
import type { Session } from '@auth/sveltekit';
import { error } from '@sveltejs/kit';
import { db } from './server/db';
import { users } from './server/db/schema';
@@ -8,21 +8,17 @@ interface Event {
auth(): Promise<Session | null>;
};
}
interface UserWithId extends User {
id: string;
email: string;
}
export async function ensureAuth(event: Event): Promise<UserWithId> {
export async function ensureAuth(event: Event): Promise<Session['user']> {
const session = await getSession(event);
if (!session) error(401, { message: 'Du muesch di zersch iiloge' });
const user = session?.user;
const user = session.user;
if (!user || !user.email || !user.id) {
error(401, { message: 'Du muesch di zersch iiloge' });
}
return { ...user, id: user.id, email: user.email }; // weird thingamajig so that ts compiler is happy
return user;
}
export async function getSession(event: Event) {
const session = await event.locals.auth();