Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Bug]: Middleware with Prisma Adapter (PrismaClient) is not working #1443

Closed
MusabDev opened this issue Feb 18, 2024 · 1 comment
Closed
Labels
bug Something ain't right...

Comments

@MusabDev
Copy link

Package

lucia

Describe the bug

Hello,

First of all, I am using Prisma (with Lucia adapter) and Next.js 14.

So, when I try to call the following function in middleware.ts, it returns the error: Error: PrismaClient is unable to run in Vercel Edge Functions or Edge Middleware. As an alternative, try Accelerate: https://pris.ly/d/accelerate. (screenshot provided below).

I think this is because you shouldn't use the PrismaClient in middleware, rather to use PrismaClient from @prisma/client/edge. Check this issue for more details: prisma/prisma#21310

export const validateRequest = cache(
  async (): Promise<
    { user: User; session: Session } | { user: null; session: null }
  > => {
    const sessionId = cookies().get(lucia.sessionCookieName)?.value ?? null;
    if (!sessionId) {
      return {
        user: null,
        session: null,
      };
    }

    const result = await lucia.validateSession(sessionId);
    try {
      if (result.session && result.session.fresh) {
        const sessionCookie = lucia.createSessionCookie(result.session.id);
        cookies().set(
          sessionCookie.name,
          sessionCookie.value,
          sessionCookie.attributes,
        );
      }
      if (!result.session) {
        const sessionCookie = lucia.createBlankSessionCookie();
        cookies().set(
          sessionCookie.name,
          sessionCookie.value,
          sessionCookie.attributes,
        );
      }
    } catch {}
    return result;
  },
);

image

@MusabDev MusabDev added the bug Something ain't right... label Feb 18, 2024
@pilcrowonpaper
Copy link
Member

This isn't a bug, nor is it related to Lucia (?)

@pilcrowonpaper pilcrowonpaper closed this as not planned Won't fix, can't repro, duplicate, stale Feb 18, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something ain't right...
Projects
None yet
Development

No branches or pull requests

2 participants