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.
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 ?