diff --git a/src/lib/auth/index.ts b/src/lib/auth/index.ts index e2d0d18..e223bac 100644 --- a/src/lib/auth/index.ts +++ b/src/lib/auth/index.ts @@ -1,71 +1,62 @@ -import { PrismaAdapter } from "@lucia-auth/adapter-prisma"; -import { Lucia, Session, User } from "lucia"; -import prisma from "../db"; -import { cache } from "react"; -import { cookies } from "next/headers"; +import { PrismaAdapter } from '@lucia-auth/adapter-prisma'; +import { Lucia, Session, User } from 'lucia'; +import prisma from '../db'; +import { cache } from 'react'; +import { cookies } from 'next/headers'; const adapter = new PrismaAdapter(prisma.session, prisma.user); export const lucia = new Lucia(adapter, { - sessionCookie: { - // this sets cookies with super long expiration - // since Next.js doesn't allow Lucia to extend cookie expiration when rendering pages - expires: false, - attributes: { - // set to `true` when using HTTPS - secure: process.env.NODE_ENV === "production" - } - }, - getUserAttributes: (attributes) => { - return { - username: attributes.username - }; - } + sessionCookie: { + // this sets cookies with super long expiration + // since Next.js doesn't allow Lucia to extend cookie expiration when rendering pages + expires: false, + attributes: { + // set to `true` when using HTTPS + secure: process.env.NODE_ENV === 'production', + }, + }, + getUserAttributes: (attributes) => { + return { + username: attributes.username, + }; + }, }); export const validateRequest = cache(async () => { - const sessionId = (await cookies()).get(lucia.sessionCookieName)?.value ?? null - - if (!sessionId) - return { - user: null, - session: null, - } - - const { user, session } = await lucia.validateSession(sessionId) - try { - if (session && session.fresh) { - const sessionCookie = lucia.createSessionCookie(session.id) - (await cookies()).set( - sessionCookie.name, - sessionCookie.value, - sessionCookie.attributes - ) - } - if (!session) { - const sessionCookie = lucia.createBlankSessionCookie() - (await cookies()).set( - sessionCookie.name, - sessionCookie.value, - sessionCookie.attributes - ) - } - } catch { - // Next.js throws error attempting to set cookies when rendering page - } - return { - user, - session, - } - }) + const sessionId = (await cookies()).get(lucia.sessionCookieName)?.value ?? null; + if (!sessionId) + return { + user: null, + session: null, + }; -declare module "lucia" { - interface Register { - Lucia: typeof lucia; - DatabaseUserAttributes: DatabaseUserAttributes; - } + const { user, session } = await lucia.validateSession(sessionId); + try { + if (session && session.fresh) { + const sessionCookie = lucia.createSessionCookie(session.id); + (await cookies()).set(sessionCookie.name, sessionCookie.value, sessionCookie.attributes); + } + if (!session) { + const sessionCookie = lucia.createBlankSessionCookie(); + (await cookies()).set(sessionCookie.name, sessionCookie.value, sessionCookie.attributes); + } + } catch { + // Next.js throws error attempting to set cookies when rendering page + } + return { + user, + session, + }; +}); + +declare module 'lucia' { + interface Register { + Lucia: typeof lucia; + DatabaseUserAttributes: DatabaseUserAttributes; + } } interface DatabaseUserAttributes { - username: string; -} \ No newline at end of file + username: string; +}