1

I'm using the entry file at the root of my app as a data layer, for redirects depending on session, which I'm retrieving using getServerSession. If at a point no session exists, user should be redirected to /home. This is the error from vercel;

my / file;

also attached is my proxy.ts

import { NextRequest, NextResponse } from "next/server"; import { getToken } from "next-auth/jwt"; export async function proxy(req: NextRequest) { const token = await getToken({req: req as any}); if (!token || !token.role) { return NextResponse.redirect(new URL("/home", req.url)); } return NextResponse.next(); } export const config = { matcher: ["/"] } "use server"; import { authOptions } from "@vendora/auth"; import { getServerSession } from "next-auth"; import { redirect } from "next/navigation"; export async function Index() { const session = await getServerSession(authOptions); if (!session?.user.role) { redirect("/onboarding") } switch (session.user.role) { case "buyer": redirect(process.env.NEXT_PUBLIC_STORE_URL as string); case "seller": redirect(process.env.NEXT_PUBLIC_SELLER_URL as string); case "admin": redirect(process.env.NEXT_PUBLIC_ADMIN_URL as string); default: redirect("/home") } } 

Generating static pages (0/27) ... Generating static pages (6/27) Error occurred prerendering page "/home". Read more: https://nextjs.org/docs/messages/prerender-error TypeError: Cannot destructure property 'auth' of 'a' as it is undefined. at f (.next/server/chunks/711.js:17:51199) at p (.next/server/chunks/711.js:1:41685) at (.next/server/chunks/711.js:1:42254) at q (.next/server/chunks/711.js:1:42235) { digest: '3188648309' }

Export encountered an error on /home/page: /home, exiting the build. ⨯ Next.js build worker exited with code: 1 and signal: null ELIFECYCLE  Command failed with exit code 1. Error: Command "pnpm run build" exited with 1

6
  • Your home page may have a destructuration like const { auth } = a. But your "a" doesn't contains an "auth" property. I tried to simply explain the message because I don't see any error in your code. Commented Nov 24 at 13:59
  • I've tried to even remove the session checks and just return a welcome message but still got the same error. Commented Nov 24 at 14:29
  • Maybe that the cache stored an error somewhere, try removing node modules, package-lock.json, .next and then rebuild your app Commented Nov 26 at 20:40
  • 1
    I found where the issue was coming from, I was storing a redirect uri in .env which was required in a component imported in /home. That's why it was throwing auth is undefined because I hadn't added the particular env to vercel Commented Nov 27 at 22:48
  • 1
    😂😂took me a while to figure it out, it was driving me nuts Commented 10 hours ago

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.