mirror of
https://github.com/aaronpo97/the-biergarten-app.git
synced 2026-02-16 20:13:49 +00:00
Refactor: change directory structure of hooks to organize by task
This commit is contained in:
22
src/hooks/auth/useRedirectIfLoggedIn.ts
Normal file
22
src/hooks/auth/useRedirectIfLoggedIn.ts
Normal file
@@ -0,0 +1,22 @@
|
||||
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;
|
||||
Reference in New Issue
Block a user