Files
the-biergarten-app/config/auth/withPageAuthRequired.ts
2023-02-05 19:27:19 -05:00

25 lines
574 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) {
return {
redirect: {
destination: '/login',
permanent: false,
},
};
}
};
export default withPageAuthRequired;