mirror of
https://codeberg.org/MarkusThielker/finances.git
synced 2025-04-12 05:08:43 +00:00
67 lines
1.9 KiB
TypeScript
67 lines
1.9 KiB
TypeScript
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;
|
|
}
|