I have the following piece of code:
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 also return the session in await gssp(ctx), I'm fairly new to NextJS and I can not manage to find any related info on this.
The current method allows me to use this component in the following way:
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,
},
};
});
Which permits additional logic on the page where it is used, compared to other HOR methods.
Any way I could return the session in the main requireAuth component instead of fetching it on each page?