🔓 Add config to secure instance from unauth users (#559)

This commit is contained in:
Luke Vella 2023-03-14 16:48:16 +00:00 committed by GitHub
parent e65c87bf04
commit 1b38a3cf76
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
16 changed files with 194 additions and 104 deletions

View file

@ -101,6 +101,34 @@ export const composeGetServerSideProps = (
};
};
/**
* Require user to be logged in
* @returns
*/
export const withAuth: GetServerSideProps = async (ctx) => {
if (!ctx.req.session.user || ctx.req.session.user.isGuest) {
return {
redirect: {
destination: "/login",
permanent: false,
},
};
}
return { props: {} };
};
/**
* Require user to be logged in if AUTH_REQUIRED is true
* @returns
*/
export const withAuthIfRequired: GetServerSideProps = async (ctx) => {
if (process.env.AUTH_REQUIRED === "true") {
return await withAuth(ctx);
}
return { props: {} };
};
export function withSessionSsr(
handler: GetServerSideProps | GetServerSideProps[],
options?: {