Next.js Discord

Discord Forum

issues with next build, kinde auth and prisma

Unanswered
Abyssinian posted this in #help-forum
Open in Discord
AbyssinianOP
Hello, it has been a while since I've used Next.js, and I can't seem to figure out what I am doing wrong or how to approach this flow. Whenever, I build my application I get the following issue.

 Creating an optimized production build ...
 ✓ Compiled successfully
 ✓ Linting and checking validity of types    
 ✓ Collecting page data    
   Generating static pages (0/8)  [    ]Error: User not found
    at o (.../.next/server/app/(main)/journal/page.js:1:3557)
Error: User not found
    at o (.../.next/server/app/(main)/journal/page.js:1:3557)

Error occurred prerendering page "/journal". Read more: https://nextjs.org/docs/messages/prerender-error

Error: User not found
    at o (.../.next/server/app/(main)/journal/page.js:1:3557)
 ✓ Generating static pages (8/8) 

> Export encountered errors on following paths:
        /(main)/journal/page: /journal


page in question
import { RecordCard } from "@/components/record-card"
import { getUserIdFromKindeId } from "@/lib/auth"
import prisma from "@/lib/prisma"

export default async function JournalPage() {
  const { id: userId } = await getUserIdFromKindeId();

  const records = await prisma.journalRecord.findMany({
    where: { userId }
  });

  return (
      <>
        {records.map((record) => (
            <RecordCard key={record.id} record={record} />
        ))}
    </>
  );
}


import "server-only"
import { getKindeServerSession } from "@kinde-oss/kinde-auth-nextjs/server"
import prisma from "@/lib/prisma"
import { redirect } from "next/navigation"

export async function getUserIdFromKindeId() {
  const { getUser } = getKindeServerSession();
  const kindeUser = await getUser();

  if (!kindeUser) redirect("/login");

  const user = await prisma.user.findUniqueOrThrow({
    where: {
      kindeId: kindeUser?.id,
    }
  });
  return user;
}

i have tried throwing if statements here and there but the build still fails. my middleware should also make sure kindeUser is present in these pages

11 Replies

@Abyssinian Hello, it has been a while since I've used Next.js, and I can't seem to figure out what I am doing wrong or how to approach this flow. Whenever, I build my application I get the following issue. Creating an optimized production build ... ✓ Compiled successfully ✓ Linting and checking validity of types ✓ Collecting page data Generating static pages (0/8) [ ]Error: User not found at o (.../.next/server/app/(main)/journal/page.js:1:3557) Error: User not found at o (.../.next/server/app/(main)/journal/page.js:1:3557) Error occurred prerendering page "/journal". Read more: https://nextjs.org/docs/messages/prerender-error Error: User not found at o (.../.next/server/app/(main)/journal/page.js:1:3557) ✓ Generating static pages (8/8) > Export encountered errors on following paths: /(main)/journal/page: /journal page in question tsx import { RecordCard } from "@/components/record-card" import { getUserIdFromKindeId } from "@/lib/auth" import prisma from "@/lib/prisma" export default async function JournalPage() { const { id: userId } = await getUserIdFromKindeId(); const records = await prisma.journalRecord.findMany({ where: { userId } }); return ( <> {records.map((record) => ( <RecordCard key={record.id} record={record} /> ))} </> ); } tsx import "server-only" import { getKindeServerSession } from "@kinde-oss/kinde-auth-nextjs/server" import prisma from "@/lib/prisma" import { redirect } from "next/navigation" export async function getUserIdFromKindeId() { const { getUser } = getKindeServerSession(); const kindeUser = await getUser(); if (!kindeUser) redirect("/login"); const user = await prisma.user.findUniqueOrThrow({ where: { kindeId: kindeUser?.id, } }); return user; } i have tried throwing if statements here and there but the build still fails. my middleware should also make sure kindeUser is present in these pages
console.log(kindeUser)
try if you see the user object?
@Ray `console.log(kindeUser)` try if you see the user object?
AbyssinianOP
its null
[kindeUser from getUserIdFromKindeId] null

which I guess makes sense because the build doesnt have a session
@Abyssinian its null [kindeUser from getUserIdFromKindeId] null which I guess makes sense because the build doesnt have a session
I think you would need to use generateStaticParams to pre-render those pages
btw, if the page is depended on session, it should be a dynamic page
AbyssinianOP
so maybe a force-dynamic ? but if every site needs to be authenticated isnt the whole app going to be dynamic o-o
@Abyssinian so maybe a `force-dynamic` ? but if every site needs to be authenticated isnt the whole app going to be dynamic o-o
you could build the static page and use middleware for authentication
@Ray you could build the static page and use middleware for authentication
AbyssinianOP
i guess that pages that dont need the kindeUserId can stay static where if i need to use the kindeUser object ill have to make it dynamic

I do have middleware protecting routes
yeah
AbyssinianOP
generateStaticParmams is for /[id] pages with dynamic segments i guess that wouldnt apply here since mine is just a /journal page