Files
the-biergarten-app/hooks/useRedirectIfLoggedIn.ts
Aaron William Po a4362a531c Add custom hooks for time distance and retrieving like count
Documentation added to all custom hooks
2023-04-03 23:32:32 -04:00

23 lines
598 B
TypeScript

import UserContext from '@/contexts/userContext';
import { useRouter } from 'next/router';
import { useContext } from 'react';
/**
* Custom React hook that redirects the user to the home page if they are logged in. This
* hook is used to prevent logged in users from accessing the login and signup pages. Must
* be used under the UserContext provider.
*
* @returns {void}
*/
const useRedirectWhenLoggedIn = (): void => {
const { user } = useContext(UserContext);
const router = useRouter();
if (!user) {
return;
}
router.push('/');
};
export default useRedirectWhenLoggedIn;