mirror of
https://github.com/aaronpo97/the-biergarten-app.git
synced 2026-02-16 20:13:49 +00:00
Implement authentication using Passport.js
This commit is contained in:
24
config/auth/withPageAuthRequired.ts
Normal file
24
config/auth/withPageAuthRequired.ts
Normal file
@@ -0,0 +1,24 @@
|
||||
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;
|
||||
Reference in New Issue
Block a user