From 412cadce839ac0d49fdd1f8e8fc2a50e72ecb84a Mon Sep 17 00:00:00 2001 From: Izan Gil <66965250+SrIzan10@users.noreply.github.com> Date: Mon, 16 Dec 2024 19:32:16 +0100 Subject: [PATCH] fix: github auth --- src/lib/auth/index.ts | 125 +++++++++++++++++++++--------------------- 1 file changed, 61 insertions(+), 64 deletions(-) diff --git a/src/lib/auth/index.ts b/src/lib/auth/index.ts index 6e221ab..6089eec 100644 --- a/src/lib/auth/index.ts +++ b/src/lib/auth/index.ts @@ -1,75 +1,72 @@ -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 { GitHub } from "arctic"; +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 { GitHub } from 'arctic'; -export const github = new GitHub(process.env.GITHUB_CLIENT!, process.env.GITHUB_SECRET!, 'http://localhost:3000/auth/github/callback'); +export const github = new GitHub( + process.env.GITHUB_CLIENT!, + process.env.GITHUB_SECRET!, + `${ + process.env.NODE_ENV === 'production' ? 'https://echospace.srizan.dev' : 'http://localhost:3000' + }/auth/github/callback` +); 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 { - githubId: attributes.githubId, - 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 { + githubId: attributes.githubId, + username: attributes.username, + }; + }, }); export const validateRequest = cache(async () => { - const sessionId = 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) - 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 attempting to set cookies when rendering page - } - return { - user, - session, - } - }) + const sessionId = 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); + 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 attempting to set cookies when rendering page + } + return { + user, + session, + }; +}); + +declare module 'lucia' { + interface Register { + Lucia: typeof lucia; + DatabaseUserAttributes: DatabaseUserAttributes; + } } interface DatabaseUserAttributes { - githubId: string; - username: string; -} \ No newline at end of file + githubId: string; + username: string; +}