0

I made a blog in which I want to verify if the user is admin before accessing the page to create a post. It works perfectly fine on the localhost but when I deploy it on vercel, it ends up with error 504 with no more informations in the logs.

I change the NEXTAUTH_URL with the one vercel gave to me (https).

Here is my 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
      }
    }
  }
}

And here is the requireAuthentification function:

const requireAuthentification = async (context, levelAcces = "public", callback) => {
    const session = await unstable_getServerSession(context.req, context.res, authOptions)
    if (!session) {
        return {
            redirect: {
                destination: '/',
                permanent: false,
            }
        }
    }
    else {
        let user = [];
        const q = query(collection(db, "users"), where("email", "==", session.user.email));
        const querySnapshot = await getDocs(q);
        user = querySnapshot.docs[0].data();
        if (user.level !== levelAcces) {
            return {
                redirect: {
                    destination: '/',
                    permanent: false,
                }
            }
        }
    }
    return callback({ session });
}

export default requireAuthentification

I tried without the getServerSideProps and it work (with no route security). I am pretty sure the issue is from unstable_getServerSession but I am not sure, because with GetSession it is the same. The log on vercel aren't very helpful (see image), even if I tried with try catch and console.log to debug it. error on browser error on vercel logs

Why my code is working on localhost but not on Vercel, even if the rest of it, like the authentification with google part, works ? How can I debug my app ?

Romano
  • 35
  • 4
  • Try to wrap those await in try {} catch {} block , and return something when error happen so the timeout doesn't happen , also console.log that error so you can start debuging – Zack Heisenberg Nov 04 '22 at 01:00

1 Answers1

0

After hours of trying, I finaly gave up this method and now I check the user authentification on the client side with useSession() which works well.

Romano
  • 35
  • 4