Below is the code snippet I am working with:
export const requireAuth = (gssp: GetServerSideProps) => {
return async (ctx: GetServerSidePropsContext) => {
const { req } = ctx;
let session = null;
if (req?.headers?.cookie) {
session = await getSession(req?.headers?.cookie);
};
if (!session) {
return {
redirect: {
permanent: false,
destination: '/login',
},
};
};
return await gssp(ctx);
};
};
I am trying to figure out how to also include the session in await gssp(ctx). As I am new to NextJS, I have not been able to find any relevant information on this.
The current approach allows me to use this component as follows:
export const getServerSideProps: GetServerSideProps = requireAuth(async _ctx => {
let account;
let stats = {};
if (_ctx?.req?.headers?.cookie) {
account = await getSession(_ctx?.req?.headers?.cookie);
}
try {
const X = fetchSomeData...;
} catch (error) {
console.log('Pages/Index Fetching Error: ', error);
}
return {
props: {
account: account,
data: X,
navbar: true,
footer: true,
},
};
});
This method allows for additional logic on the page where it is implemented, in comparison to other methods like HOC.
Is there a way to include the session in the main requireAuth
component instead of fetching it on each individual page?