Skip to content

Commit

Permalink
Merge pull request #34 from so1s/SO1S-497
Browse files Browse the repository at this point in the history
SO1S-497 토큰 만료등 401시 로그인 페이지로 리다이렉트
  • Loading branch information
DPS0340 authored Nov 17, 2022
2 parents d8fa6c0 + 1b336f2 commit a5ed238
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 16 deletions.
9 changes: 3 additions & 6 deletions src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { createTheme, ThemeProvider } from '@mui/material';
import Router from './components/router';
import { WithAuth } from './hocs/with-auth';
import { WithSnackbar } from './hocs/with-snackbar';

const theme = createTheme({
Expand All @@ -17,11 +16,9 @@ const theme = createTheme({
const App = () => {
return (
<ThemeProvider theme={theme}>
<WithAuth>
<WithSnackbar>
<Router />
</WithSnackbar>
</WithAuth>
<WithSnackbar>
<Router />
</WithSnackbar>
</ThemeProvider>
);
};
Expand Down
1 change: 0 additions & 1 deletion src/components/navbar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ const NavBar: React.FC = () => {
key={e.name}
className="h-20 flex flex-grow cursor-pointer"
onClick={() => {
setPage(e);
navigate(e.uri);
}}
>
Expand Down
24 changes: 15 additions & 9 deletions src/components/router.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import { useAtom } from 'jotai';
import { BrowserRouter, Routes as ReactRoutes, Route } from 'react-router-dom';
import { accessTokenWithPersistence } from '../atoms/token';
import routes from '../constants/routes';
import { WithAuth } from '../hocs/with-auth';
import { WithHeader } from '../hocs/with-header';
import LoginRedirect from '../pages/login-redirect';
import Header from './header';
import Navbar from './navbar';
Expand All @@ -18,17 +20,21 @@ const Router: React.FC = () => {
? LoginRedirect
: e.page;
const Element = (
<div className="w-screen h-screen flex flex-grow overflow-y-hidden">
<div className="flex-1 flex-col flex-grow">
<Header />
<div className="flex h-full flex-row">
{!e.hidden && <Navbar />}
<div className="flex flex-col flex-1 h-[95vh] pt-10 overflow-y-scroll">
<Page />
<WithHeader datum={e}>
<WithAuth>
<div className="w-screen h-screen flex flex-grow overflow-y-hidden">
<div className="flex-1 flex-col flex-grow">
<Header />
<div className="flex h-full flex-row">
{!e.hidden && <Navbar />}
<div className="flex flex-col flex-1 h-[95vh] pt-10 overflow-y-scroll">
<Page />
</div>
</div>
</div>
</div>
</div>
</div>
</WithAuth>
</WithHeader>
);

return <Route path={e.uri} key={e.uri} element={Element} />;
Expand Down
18 changes: 18 additions & 0 deletions src/hocs/with-header.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { useAtom } from 'jotai';
import { useEffect } from 'react';
import { useLocation } from 'react-router-dom';
import currentPage from '../atoms/current-page';
import { WithHeaderProps } from '../types/hocs';

export const WithHeader: React.FC<WithHeaderProps> = ({ children, datum }) => {
const location = useLocation();
const [, setPage] = useAtom(currentPage);

useEffect(() => {
if (location.pathname === datum.uri) {
setPage(datum);
}
}, [datum, location.pathname]);

return <>{children}</>;
};
10 changes: 10 additions & 0 deletions src/hooks/useAuth.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { useAtom } from 'jotai';
import axios from 'axios';
import { useEffect } from 'react';
import { useLocation, useNavigate } from 'react-router-dom';
import { accessTokenWithPersistence } from '../atoms/token';
import { baseURL } from '../constants';

Expand All @@ -10,6 +11,15 @@ export const axiosInstanceRef = new Proxy({ current: axiosInstance }, {});
const useAuth = () => {
const [accessToken, setAccessToken] = useAtom(accessTokenWithPersistence);

const location = useLocation();
const navigate = useNavigate();

useEffect(() => {
if (!accessToken && location.pathname !== '/login') {
navigate('/login');
}
}, [accessToken]);

useEffect(() => {
axiosInstanceRef.current = axios.create({
baseURL,
Expand Down
1 change: 1 addition & 0 deletions src/pages/login.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { snackbarAtom } from '../atoms/snackbar';

const Login: React.FC = () => {
const [accessToken, setAccessToken] = useAtom(accessTokenWithPersistence);

const usernameRef = useRef<HTMLInputElement>(null);
const passwordRef = useRef<HTMLInputElement>(null);

Expand Down
6 changes: 6 additions & 0 deletions src/types/hocs/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
import IRouterDatum from '../../interfaces/router';

export type HoCProps = {
children?: React.ReactNode;
};

export type WithHeaderProps = HoCProps & {
datum: IRouterDatum;
};

0 comments on commit a5ed238

Please sign in to comment.