Files
the-biergarten-app/config/auth/withPageAuthRequired.ts
Aaron William Po 4cd2ab476f Formatting changes
2023-02-20 09:09:45 -05:00

26 lines
600 B
TypeScript

import { GetServerSideProps, GetServerSidePropsContext } from 'next';
import { getLoginSession } from './session';
const withPageAuthRequired =
(fn?: GetServerSideProps) => async (context: GetServerSidePropsContext) => {
try {
const { req } = context;
await getLoginSession(req);
if (!fn) {
return { props: {} };
}
return await fn(context);
} catch (error) {
console.log(error);
return {
redirect: {
destination: '/login',
permanent: false,
},
};
}
};
export default withPageAuthRequired;