Skip to content

Commit

Permalink
[FE] feat : 로그인 검사 커스텀 훅 생성 및 설정 (#40)
Browse files Browse the repository at this point in the history
  • Loading branch information
parksil0 committed Nov 19, 2021
1 parent 36bcc46 commit 8973277
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
23 changes: 23 additions & 0 deletions front/hooks/useLoginCheck.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { useRouter } from 'next/router';
import React, { useEffect } from 'react';
import { useCookies } from 'react-cookie';

const useLoginCheck = () => {
const [cookies] = useCookies();
const router = useRouter();
return useEffect(() => {
const { pathname } = router;
const { accessToken } = cookies;

if (accessToken) {
if (pathname === '/login' || pathname === '/login/email' || pathname === 'signup') {
router.back();
}
router.push('/map');
} else {
router.push('/login');
}
}, [cookies, router.pathname]);
};

export default useLoginCheck;
3 changes: 3 additions & 0 deletions front/pages/_app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,11 @@ import withRedux from 'next-redux-wrapper';

import { store } from '../state/store';
import '../styles/globals.css';
import useLoginCheck from '../hooks/useLoginCheck';

function MyApp({ Component, pageProps }: AppProps) {
useLoginCheck();

return (
<Provider store={store}>
<Component {...pageProps} />
Expand Down

0 comments on commit 8973277

Please sign in to comment.