N-FIN-79: remove unused auth.ts

This commit is contained in:
Markus Thielker 2024-04-04 00:49:52 +02:00
parent 57f3381829
commit 53247d382d
No known key found for this signature in database

View file

@ -1,67 +0,0 @@
import { Lucia } from 'lucia';
import { PrismaAdapter } from '@lucia-auth/adapter-prisma';
import { cookies } from 'next/headers';
import prisma from '@/prisma';
const adapter = new PrismaAdapter(prisma.session, prisma.user);
export const lucia = new Lucia(adapter, {
sessionCookie: {
expires: false,
attributes: {
sameSite: 'strict',
domain: process.env.NODE_ENV === 'production' ? process.env.COOKIE_DOMAIN : undefined,
secure: process.env.NODE_ENV === 'production',
},
},
getUserAttributes: (attributes) => {
return {
username: attributes.username,
};
},
});
declare module 'lucia' {
interface Register {
Lucia: typeof lucia;
DatabaseUserAttributes: DatabaseUserAttributes;
}
}
interface DatabaseUserAttributes {
username: string;
}
export function getSessionId() {
return cookies().get(lucia.sessionCookieName)?.value ?? null;
}
export async function getSession() {
const sessionId = getSessionId();
if (!sessionId) {
return null;
}
const {session} = await lucia.validateSession(sessionId);
return session;
}
export async function getUser() {
const sessionId = getSessionId();
if (!sessionId) {
return null;
}
const {user, session} = await lucia.validateSession(sessionId);
try {
if (session && session.fresh) {
const sessionCookie = lucia.createSessionCookie(session.id);
cookies().set(sessionCookie.name, sessionCookie.value, sessionCookie.attributes);
}
if (!session) {
const sessionCookie = lucia.createBlankSessionCookie();
cookies().set(sessionCookie.name, sessionCookie.value, sessionCookie.attributes);
}
} catch {
// Next.js throws error when attempting to set cookies when rendering page
}
return user;
}