I'm using NextJS and NextAuthJS in a monorepo to authenticate users for a dashboard style application. As the NextAuthJS example suggests I have an auth.ts file defining the options for the AuthHandler. For NextAuth, i have to import the AuthOptions into all of my routes which i want to be serverside authenticated.
The Problem is that, said code, which defines the AuthOptions seems to be executed during build. I know this because the code relies on environment variables. The logic I implemented to make sure my enviorment variables are present throws an error during build since the environment variables are, by design, not present during build.
I'm unsure why NextJS is trying to statically build the pages, since all code that uses these authoptions is in getServerSideProps.
See the relevant page: (frontend code shortend as it seems irrelevant)
import { getDBData } from '../db';
import { auth } from '../auth';
import { GetServerSidePropsContext } from 'next';
export const getServerSideProps = (async (context: GetServerSidePropsContext) => {
const session = await auth(context.req, context.res);
if (session) {
const dbData = await getAliveBots();
return {
props: {
serverSession: session,
dbData,
},
};
}
return {
props: {
serverSession: null,
dbData: [],
},
};
})
export default function Index({ serverSession, dbData }) {
if (serverSession) {
return (
{dbData.map((data) => (
// render a few components with data here
))
)
}
return (
<>
</>
);
}