Skip to content

Commit

Permalink
로그인 + authentication 리팩토링
Browse files Browse the repository at this point in the history
  • Loading branch information
eun-hak committed Jun 20, 2024
1 parent 2218bd2 commit 5f59cdd
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 23 deletions.
4 changes: 3 additions & 1 deletion src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,11 @@ import Main from './pages/Main';
import NoticeDetail from './pages/NoticeDetail';
import NoticeUpdate from './pages/NoticeUpdate';
import NoticeWrite from './pages/NoticeWrite';

import { AuthorizationProvider } from './providers/Authentication.tsx';
const App = () => {
return (
<BrowserRouter>
<AuthorizationProvider>
<Routes>
<Route path="/" element={<LoginAPi />} />
<Route path="/Main" element={<Main />} />
Expand All @@ -20,6 +21,7 @@ const App = () => {
<Route path="/Update" element={<NoticeUpdate />} />
<Route path="/Write" element={<NoticeWrite />} />
</Routes>
</AuthorizationProvider>
</BrowserRouter>
);
};
Expand Down
20 changes: 2 additions & 18 deletions src/api/login.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import apiAxios from './apiAxios';
import { setCookie, getCookie } from '../utils/Cookies';
import { setCookie } from '../utils/Cookies';
import { useNavigate } from 'react-router-dom';
import CustomRouter from '../utils/Routers';

export interface LoginDataType {
loginId: string;
Expand All @@ -18,6 +17,7 @@ const LoginAPi = () => {
console.log(r.data);
setCookie('member', r.data.UserCount);
navigate('/main');
window.location.reload();
})
.catch(e => {
e.response.data, alert('아이디 또는 비밀번호를 다시 입력해주세요');
Expand All @@ -28,19 +28,3 @@ const LoginAPi = () => {
};

export default LoginAPi;

// export const login = async (loginData: LoginDataType) => {
// const navigation = CustomRouter();

// await apiAxios
// .post('/v2/admin/login', loginData)
// .then(r => {
// setCookie('Accesstoken', r.data.AccessToken);
// console.log(r.data);
// setCookie('member', r.data.UserCount);
// })

// .catch(e => {
// e.response.data, alert('아이디 또는 비밀번호를 다시 입력해주세요');
// });
// };
22 changes: 22 additions & 0 deletions src/providers/Authentication.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { getCookie } from '../utils/Cookies';
import { useNavigate, useLocation } from 'react-router-dom';
import { ReactNode, useEffect } from 'react';

interface AuthenticationProviderProps {
children: ReactNode;
}

export const AuthorizationProvider = ({
children,
}: AuthenticationProviderProps) => {
const navigate = useNavigate();
const token = getCookie('Accesstoken');


useEffect(() => {
if (!token) {
navigate('/');
}
}, [token]);
return <>{children}</>;
};
39 changes: 35 additions & 4 deletions src/utils/Cookies.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,40 @@
import { Cookies } from 'react-cookie';

/* eslint-disable */

export interface CookieGetOptions {
doNotParse?: boolean;
doNotUpdate?: boolean;
}
export interface CookieSetOptions {
path?: string;
expires?: Date;
maxAge?: number;
domain?: string;
secure?: boolean;
httpOnly?: boolean;
sameSite?: boolean | 'none' | 'lax' | 'strict';
}
export interface CookieChangeOptions {
name: string;
value?: any;
options?: CookieSetOptions;
}

const cookies = new Cookies();

export const setCookie = (name: string, value: string, option?: any) => {
return cookies.set(name, value, { ...option });
export const setCookie = (
name: string,
value: string,
options?: CookieSetOptions
) => {
return cookies.set(name, value, { ...options });
};
export const getCookie = (name: string) => {
return cookies.get(name);

export const getCookie = (name: string, options?: CookieGetOptions) => {
return cookies.get(name, options);
};

export const removeCookie = (name: string, options?: CookieSetOptions) => {
return cookies.remove(name, options);
};

0 comments on commit 5f59cdd

Please sign in to comment.