#500 Error

1 messages · Page 1 of 1 (latest)

pearl hamlet
#

Getting 500 server error on production.
Our middleware file (this was updated in the new commit)

import type {NextRequest} from "next/server";
import {NextResponse} from "next/server";
import {validateBetaJWT, validateJWT} from "@/lib/jwtFunctions";
import {isVerified} from "@/lib/emailVerification";

export async function middleware(request: NextRequest) {
    const betaToken = request.cookies.get("beta_token")?.value;
    const token = request.cookies.get("token")?.value;

    if (
        request.nextUrl.pathname.startsWith("/api") ||
        request.nextUrl.pathname.startsWith("/beta")
    ) {
        return NextResponse.next();
    }

    if (request.nextUrl.pathname.startsWith("/redirect")) {
        const url = request.nextUrl.searchParams.get("url");
        try {
            if (url) return NextResponse.redirect(url);
            else return NextResponse.redirect(`${process.env.WEB_DOMAIN}/`);
        } catch (e) {
            return NextResponse.redirect(`${process.env.WEB_DOMAIN}/`);
        }
    }

    if (
        await validateBetaJWT(betaToken, request.headers.get("x-forwarded-for"))
    ) {
        if (request.nextUrl.pathname.startsWith("/dash")) {
            if (token && (await validateJWT(token))) {
                if (await isVerified(token)) {
                    return NextResponse.next();
                } else {
                    return NextResponse.redirect(
                        `${process.env.WEB_DOMAIN}/verify`
                    );
                }
            } else {
                const nextPath = encodeURIComponent(request.nextUrl.pathname);
                return NextResponse.redirect(
                    `${process.env.WEB_DOMAIN}/login?next=${nextPath}`
                );
            }
        } else {
            return NextResponse.next();
        }
    } else {
        return NextResponse.redirect(`${process.env.WEB_DOMAIN}/beta`);
    }
}

export const config = {matcher: "/((?!.*\\.).*)"};

raven gobletBOT
#

Hi @pearl hamlet. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave:

#

Based on the provided code, it's hard to pinpoint the exact cause of the 500 server error without more context or error messages. However, I can provide some general advice based on the information extracts you've provided.

  1. Error Handling: If an error is thrown inside a Server Component, Next.js will forward an Error object to the nearest error.js file as the error prop. During production, the Error object forwarded to the client only includes a generic message and digest property. This is a security precaution to avoid leaking potentially sensitive details included in the error to the client. The digest property contains an automatically generated hash of the error that can be used to match the corresponding error in server-side logs. source (https://nextjs.org/docs/app/building-your-application/routing/error-handling)

  2. Custom Error Page: You can customize the 500 error page by creating a pages/500.js file. This file is statically generated at build time. source (https://nextjs.org/docs/pages/building-your-application/routing/custom-error)

  3. Middleware Upgrade: If you've recently upgraded your middleware, make sure you've followed the correct steps. For example, for cases where Middleware is used to respond (such as authorization), you should migrate to use rewrite/redirect to pages that show an authorization error, login forms, or to an API Route. source (https://nextjs.org/docs/messages/middleware-upgrade-guide)

  4. Unhandled Exceptions: When an unhandled exception occurs, you can control the experience for your users with the 500 page. You can also log and track exceptions with a tool like Sentry. source (https://nextjs.org/docs/pages/building-your-application/deploying/production-checklist)

#
  1. getServerSideProps: If an error is thrown inside getServerSideProps, it will show the pages/500.js file. source (https://nextjs.org/docs/pages/building-your-application/data-fetching/get-server-side-props)

Please review these points and see if they can help you identify the issue. If the problem persists, you might want to consider logging the errors and examining the server logs for more details.

Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: