lucia-auth / lucia

Authentication, simple and clean
https://lucia-auth.com
MIT License
8.32k stars 447 forks source link

[Bug]: @lucia-auth/adapter-prisma #1566

Closed YpsilonTM closed 1 month ago

YpsilonTM commented 1 month ago

Package

​@lucia-auth/adapter-prisma

Describe the bug

Well I haven't found the exact issue, because it confusing to find who is causing it. But my auth.ts is causing an issue when I build it with sveltekit and the node adapter. The problem is it uses __dirname wich is not supported in esm and as far as I can tell its looking to come from the prisma adapter. But i am not 100% sure.

My auth.ts:

import { dev } from '$app/environment'
import { env } from '$env/dynamic/private'
import { Discord } from 'arctic'
import { Lucia } from 'lucia'
import { adapter } from './prisma'

export const lucia = new Lucia(adapter, {
    sessionCookie: {
        attributes: {
            secure: !dev,
        },
    },
    getUserAttributes: (attributes) => {
        return {
            discordId: attributes.discordId,
            discordName: attributes.discordName,
            discordAvatar: attributes.discordAvatar,
        }
    },
})

declare module 'lucia' {
    interface Register {
        Lucia: typeof lucia
        DatabaseUserAttributes: DatabaseUserAttributes
    }

    interface DatabaseUserAttributes {
        discordId: string
        discordName: string
        discordAvatar?: string
    }
}

export const discordAuth = new Discord(env.DISCORD_CLIENT_ID, env.DISCORD_CLIENT_SECRET, env.ORIGIN + env.DISCORD_CALLBACK_ENDPOINT)

The error:

   D:\Development\fivem\hometown-website   master ●  ?1 ~2  node .\build\index.js
file:///D:/Development/fivem/hometown-website/build/server/chunks/auth-DeHbUlze.js:43
      `;console.warn(`${fe("warn(prisma)")} ${u}`);}}}}function Uo(e){if(Vu(e)){zn(`Environment variables loaded from ${e}`);let r=Zn.default.config({path:e,debug:process.env.DOTENV_CONFIG_DEBUG?!0:void 0});return {dotenvResult:Bo(r),message:Ie(`
Environment variables loaded from ${fr.default.relative(process.cwd(),e)}`),path:e}}else zn(`Environment variables not found at ${e}`);return null}function Qo(e,r){return e&&r&&fr.default.resolve(e)===fr.default.resolve(r)}function Vu(e){return !!(e&&$t.default.existsSync(e))}var Go="library";function Ur(e){let r=Bu();return r||(e?.config.engineType==="library"?"library":e?.config.engineType==="binary"?"binary":Go)}function Bu(){let e=process.env.PRISMA_CLIENT_ENGINE_TYPE;return e==="library"?"library":e==="binary"?"binary":void 0}k(ei());var M=k(require$$3);k(ei());L("prisma:engines");function Yo(){return M.default.join(__dirname,"../")}M.default.join(__dirname,"../query-engine-darwin");M.default.join(__dirname,"../query-engine-darwin-arm64");M.default.join(__dirname,"../query-engine-debian-openssl-1.0.x");M.default.join(__dirname,"../query-engine-debian-openssl-1.1.x");M.default.join(__dirname,"../query-engine-debian-openssl-3.0.x");M.default.join(__dirname,"../query-engine-linux-static-x64");M.default.join(__dirname,"../query-engine-linux-static-arm64");M.default.join(__dirname,"../query-engine-rhel-openssl-1.0.x");M.default.join(__dirname,"../query-engine-rhel-openssl-1.1.x");M.default.join(__dirname,"../query-engine-rhel-openssl-3.0.x");M.default.join(__dirname,"../libquery_engine-darwin.dylib.node");M.default.join(__dirname,"../libquery_engine-darwin-arm64.dylib.node");M.default.join(__dirname,"../libquery_engine-debian-openssl-1.0.x.so.node");M.default.join(__dirname,"../libquery_engine-debian-openssl-1.1.x.so.node");M.default.join(__dirname,"../libquery_engine-debian-openssl-3.0.x.so.node");M.default.join(__dirname,"../libquery_engine-linux-arm64-openssl-1.0.x.so.node");M.default.join(__dirname,"../libquery_engine-linux-arm64-openssl-1.1.x.so.node");M.default.join(__dirname,"../libquery_engine-linux-arm64-openssl-3.0.x.so.node");M.default.join(__dirname,"../libquery_engine-linux-musl.so.node");M.default.join(__dirname,"../libquery_engine-linux-musl-openssl-3.0.x.so.node");M.default.join(__dirname,"../libquery_engine-rhel-openssl-1.0.x.so.node");M.default.join(__dirname,"../libquery_engine-rhel-openssl-1.1.x.so.node");M.default.join(__dirname,"../libquery_engine-rhel-openssl-3.0.x.so.node");M.default.join(__dirname,"../query_engine-windows.dll.node");var ri=k(require$$2),zo=L("chmodPlusX");function ti(e){if(process.platform==="win32")return;let r=ri.default.statSync(e),t=r.mode|64|8|1;if(r.mode===t){zo(`Execution permissions of ${e} are fine`);return}let n=t.toString(8).slice(-3);zo(`Have to call chmodPlusX on ${e}`),ri.default.chmodSync(e,n);}function ni(e){let r=e.e,t=a=>`Prisma cannot find the required \`${a}\` system library in your system`,n=r.message.includes("cannot open shared object file"),i=`Please refer to the documentation about Prisma's system requirements: ${Kn("https://pris.ly/d/system-requirements")}`,o=`Unable to require(\`${Ie(e.id)}\`).`,s=pr({message:r.message,code:r.code}).with({code:"ENOENT"},()=>"File does not exist.").when(({message:a})=>n&&a.includes("libz"),()=>`${t("libz")}. Please install it and try again.`).when(({message:a})=>n&&a.includes("libgcc_s"),()=>`${t("libgcc_s")}. Please install it and try again.`).when(({message:a})=>n&&a.includes("libssl"),()=>{let a=e.platformInfo.libssl?`openssl-${e.platformInfo.libssl}`:"openssl";return `${t("libssl")}. Please install ${a} and try again.`}).when(({message:a})=>a.includes("GLIBC"),()=>`Prisma has detected an incompatible version of the \`glibc\` C standard library installed in your system. This probably means your system may be too old to run Prisma. ${i}`).when(({message:a})=>e.platformInfo.platform==="linux"&&a.includes("symbol not found"),()=>`The Prisma engines are not compatible with your system ${e.platformInfo.originalDistro} on (${e.platformInfo.archFromUname}) which uses the \`${e.platformInfo.binaryTarget}\` binaryTarget by default. ${i}`).otherwise(()=>`The Prisma engines do not seem to be compatible with your system. ${i}`);return `${o}                                                                                                                                                                                                                                                                                                                                                                                                                                       

                                                                                                                                                                         ^

ReferenceError: __dirname is not defined in ES module scope
This file is being treated as an ES module because it has a '.js' file extension and 'D:\Development\fivem\hometown-website\package.json' contains "type": "module". To treat it as a CommonJS script, rename it to use the '.cjs' file extension.    
    at requireLibrary (file:///D:/Development/fivem/hometown-website/build/server/chunks/auth-DeHbUlze.js:43:908)
    at file:///D:/Development/fivem/hometown-website/build/server/chunks/auth-DeHbUlze.js:199:7
    at requireClient (file:///D:/Development/fivem/hometown-website/build/server/chunks/auth-DeHbUlze.js:409:4)
    at require_default$1 (file:///D:/Development/fivem/hometown-website/build/server/chunks/auth-DeHbUlze.js:419:20)
    at require_default (file:///D:/Development/fivem/hometown-website/build/server/chunks/auth-DeHbUlze.js:430:7)
    at file:///D:/Development/fivem/hometown-website/build/server/chunks/auth-DeHbUlze.js:435:23
    at ModuleJob.run (node:internal/modules/esm/module_job:218:25)
    at async ModuleLoader.import (node:internal/modules/esm/loader:329:24)
    at async get_hooks (file:///D:/Development/fivem/hometown-website/build/server/index.js:184:8)
    at async Server.init (file:///D:/Development/fivem/hometown-website/build/server/index.js:3839:24)

Node.js v20.10.0

I'm sorry I don't see it right away if its actually just prisma. I have found this: https://github.com/prisma/prisma/issues/15614 https://github.com/prisma/prisma/issues/20702 https://github.com/prisma/prisma/issues/22827

I have run just sveltekit with prisma alone and then build it with the adapter node and it doesn't cause an issue

I have an older project with "lucia": "^2.7.4" and "@lucia-auth/adapter-prisma": "^3.0.2", and that project has no issues So somehow I think it might be related to the prisma-adapter not sure how yet.

YpsilonTM commented 1 month ago

My appologies this is a prisma / sveltkit issue upon further testing.