Files
the-biergarten-app/getServerSideProps/redirectIfLoggedIn.ts
Aaron William Po 472ead9abd Refactoring beer by id page, add delete comment
Refactored the comments ui into various new components, added the delete beer comment by id feature.
2023-03-03 21:28:44 -05:00

29 lines
705 B
TypeScript

import { GetServerSideProps, GetServerSidePropsContext, Redirect } from 'next';
import { getLoginSession } from '@/config/auth/session';
import findUserById from '@/services/User/findUserById';
const redirectIfLoggedIn = (redirect: Redirect) => {
const fn: GetServerSideProps = async (context: GetServerSidePropsContext) => {
try {
const { req } = context;
const session = await getLoginSession(req);
const { id } = session;
const user = await findUserById(id);
if (!user) {
throw new Error('Could not get user.');
}
return { redirect };
} catch {
return { props: {} };
}
};
return fn;
};
export default redirectIfLoggedIn;