Verify user authentication status on the server using Vercel and NextAuth

After successfully running my blog on localhost, I encountered an issue when deploying it on Vercel. The page to create a post kept returning error 504 with no additional information in the logs.

I made sure to update the NEXTAUTH_URL with the URL provided by Vercel (https).

Below is the code for the NewPostPage:

const NewPostPage = (props) => {
  if (props.messageError) {
    console.log("Error auth: " + props.messageError);
  }
  return (
    <div className={classes.pageContainer}>
      <NewPostForm />
    </div>
  );
};

export default NewPostPage

export const getServerSideProps = async (context) => {
  try {
    return await requireAuthentification(context, "admin", ({ session }) => {
      return {
        props: { session, messageError: null },
      }
    })
  }
  catch (error) {
    return {
      props: {
        messageError: error
      }
    }
  }
}

Here is the function used for authentication:

const requireAuthentification = async (context, levelAcces = "public", callback) => {
    const session = await unstable_getServerSession(context.req, context.res, authOptions)
    // rest of the function...
}

export default requireAuthentification

The issue persists despite my attempts, and debugging through logs or using GetSession has not been very helpful so far. Why is my code functioning properly on localhost but not on Vercel? How can I effectively debug my application under these circumstances?

Answer №1

While attempting for hours, I eventually decided to abandon this approach and instead conduct user authentication on the client side using useSession(), which has proven to be effective.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Create paper "cut" borders using HTML canvas or pseudo-elements

As a designer with a penchant for pain, I am striving to achieve an "art nouveau" aesthetic on paper for my NextJS project with MUI as the main design solution. Despite the abundance of CSS in the background, I am faced with the challenge of creating inner ...

The getStaticProps() function is failing to send data over to the components

I am currently learning how to use Next.js by following the guide on nextjs.org. My question is, when I use the getStaticProps function, it seems to fetch and log the data correctly inside the function. However, when I pass the data (which is the props ob ...

Challenges Faced in Navigation Across Pages Within a Complex Nested Folder Structure Using Next.js

Having some trouble with page navigation in my Next.js application that has a nested folder structure. Here's how my project folders are organized: app (site) components auth LoginForm.js SignupForm.js page.js pages LoginPage.tsx Signu ...

The issue of Next.JS fetch not caching data within the same request

I am faced with a straightforward setup where a Next.JS server-side component is responsible for fetching and displaying a post. The challenge lies in setting the page title to reflect the title of the post, requiring me to call my posts API endpoint twice ...

Is it possible to incorporate static data when building the White label app Next 13?

Currently working on Next 13 and keen to implement the new app router for my current project. We will need various static data for different versions of the app tailored for specific customers. These are not page routes like those generated by generateSta ...

Creating watermarks on PDF files using query parameters in URL with the Next.js API

Exploring the world of Next.js API for the first time has been quite a learning experience for me, especially since I am relatively new to the realm of Next.js and React. The objective behind this API route is to initiate an automatic download of a PDF co ...

What is the best way to store changing images in a Next.js application?

Is it possible to set the cache-control for images in a list of objects received from an API? Each object in the list contains a property called imageUrl, which is the link to the image. ...

When working with NextJs, you may encounter a ValidationError indicating that the configuration object is invalid. This error occurs when Webpack has been initialized with a configuration object that doesn't

After upgrading from Next v12 to v12.2.3, I encountered a problem when running "yarn dev" with a new middleware.js file in the root directory: ValidationError: Invalid configuration object. Webpack initialization error due to mismatched API schema. - Deta ...

I am deciding between using CommonJS or ES module for my sub packages in a Yarn workspace for my Next.js project. Which one

Currently working on a Next.js monorepo project using TypeScript and yarn workspace. Within the yarn workspace, there are two packages: /web and /api. The /web package is a next.js project, while /api serves as a shared subpackage utilized by /web. /my-pr ...

Activate the scroll accordion to automatically expand when the page loads

Incorporated within my FAQ page is an accordion feature. My desired functionality includes allowing the user to click on a link such as localhost:3000/faq#why-choose-us, which should prompt the page to smoothly scroll to the accordion element marked with t ...

What methods can I use to conceal a Script within a particular subDomain in Next.js?

on the main website page, there is a chat window script that needs to be hidden on any subdomain, for example: domain.com >> allow the Script ,,,, *.domain.com >> disallow the Script *.domain.com/* >> disallow the Script import { Html ...

In React, the clearInterval() function does not effectively clear intervals

Currently, I am implementing the following code: startInterval = () => { this.interval = setInterval(this.intervalFunction, 10000) } stopInterval = () => { clearInterval(this.interval) } However, a problem arises when I invoke the stopInte ...

Is there a method in NextJS to trigger an action exclusively on the server side during a newly loaded page?

At first, our team developed an _app.tsx file with getInitialProps, and included this line: if (!req) return {}; The goal was to ensure that getInitialProps only runs server-side during the initial page load. This decision was based on common advice found ...

Access to create permissions for collection "faunaDB" denied due to authorization privileges in FQL query using User Defined

I have a custom user role for security that has a predicate function for creating entries in a collection named formEntryData. When I try to create an entry without the function, it works fine. However, when I use the provided function below, I receive a p ...

Following mouse element delay issue in the latest directory of a Next.js 13 application

I'm working on a project where I want to create an element that follows the mouse as it moves across the screen. I decided to use framer motion for the animation and found a helpful example in their documentation: Check out this example from framer mo ...

In React, how do public services compare to the Angular equivalent?

In order to maintain the state of a service between different views, I utilize Angular services that are declared as public. This allows me to store the necessary information within the service's variables. For example, in View 1, I am able to scan a ...

Troubleshooting Issues with Job Scheduling in Next.js Using node-cron on Vercel Deployments

I've been attempting to configure scheduled tasks in my Next.js application hosted on Vercel using the node-cron library, but I'm facing a problem where the tasks are not being executed as expected. Despite following the official documentation an ...

React throwing a typescript error while attempting to update state based on the previous state

Hello there! I'm fairly new to working with TypeScript and I've encountered an issue with a piece of state in a child component. I'm trying to modify it based on the previous value, but every time I call the setState function, I get a type e ...

Auth.js and Next.js, route error with callback handling

When deploying the app on a server using auth.js with credentials provider, an error is encountered. Interestingly, there are no errors when running the app on localhost. The login page structure can be seen below: 'use client' import { Label } ...

My custom class is being ignored by Tailwind CSS within breakpoints

I'm attempting to incorporate some animation on the height property within the md breakpoint, but for some reason Tailwind CSS isn't applying my class. <div className={`h-12 bg-blue flex w-full text-white fixed mt-1 md:bg-white ${scrolling ? ...