Skip to content

Commit

Permalink
feat: jwt 토큰 만료 처리
Browse files Browse the repository at this point in the history
  • Loading branch information
cofls980 committed Apr 27, 2023
1 parent f03ae6c commit 8f703ed
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 25 deletions.
23 changes: 19 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
"react-cookie": "^4.1.1",
"react-dom": "^18.2.0",
"react-icons": "^4.8.0",
"react-jwt": "^1.1.8",
"react-query": "^3.39.3",
"react-router-dom": "^6.10.0",
"react-scripts": "5.0.1",
Expand Down Expand Up @@ -59,7 +60,7 @@
]
},
"devDependencies": {
"@types/jsonwebtoken": "^9.0.1",
"@types/jsonwebtoken": "^9.0.2",
"@types/react-modal": "^3.16.0",
"@types/react-router-dom": "^5.3.3",
"@types/styled-components": "^5.1.26",
Expand Down
4 changes: 3 additions & 1 deletion src/components/pages/login/auth-page/Auth.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,9 @@ export default function Auth() {
setUserState(storeUser);
navigate(`/chat/channel`);
} catch (error) {
alert('인증 코드를 다시 입력해주세요');
alert('틀린 인증 코드입니다. 다시 로그인해주세요.');
console.log(error);
removeCookie('token');
navigate(`/`);
}
};
Expand Down Expand Up @@ -99,6 +100,7 @@ export default function Auth() {
} else {
verifyTimeOut();
alert('시간이 지났습니다. 다시 로그인해주세요.');
removeCookie('token');
navigate(`/`);
}

Expand Down
38 changes: 19 additions & 19 deletions src/components/pages/login/login-page/CheckLogin.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,14 @@ import React, { ReactNode, useEffect, useRef } from 'react';
import { RecoilRoot, useRecoilValue, useSetRecoilState } from 'recoil';
import { useNavigate } from 'react-router-dom';
import { Socket, io } from 'socket.io-client';
// import jwt from 'jsonwebtoken';
import { isExpired, decodeToken } from 'react-jwt';
import { userState } from '../../../../recoil/locals/login/atoms/atom';
import { IError } from '../../chat/chatroom-page';

interface Props {
children: ReactNode;
}

// const isTokenExpired = (token: string) => {
// const decodedToken = jwt.decode(token);
// console.log(decodedToken);
// };
// 토큰 만료 시 offline 요청, recoil 비워주기

export default function CheckLogin({ children }: Props) {
const navigate = useNavigate();
const userInfo = useRecoilValue(userState);
Expand All @@ -24,11 +18,22 @@ export default function CheckLogin({ children }: Props) {

useEffect(() => {
if (userInfo.token === '') {
socketRef.current?.close();
navigate('/');
return;
}
if (isExpired(userInfo.token) === true) {
const resetUser = {
token: '',
userId: '',
nickname: '',
imageUrl: '',
};
setUserState(resetUser);
socketRef.current?.close();
navigate('/');
return;
}
}, []);

useEffect(() => {
if (userInfo.token !== '' && socketRef.current === null) {
socketRef.current = io(`${process.env.REACT_APP_BASE_BACKEND_URL}`, {
path: '/socket.io',
Expand All @@ -38,21 +43,16 @@ export default function CheckLogin({ children }: Props) {
});
socketRef.current.emit('login');
socketRef.current.on('error', (error: IError) => {
console.log(error);
navigate('/');
});
}
return () => {
if (userInfo.token !== '' && socketRef.current === null) {
const resetUser = {
token: '',
userId: '',
nickname: '',
imageUrl: '',
};
setUserState(resetUser);
}
};
}, [socketRef.current]);
navigate('/');
});
}
});
return <div>{children}</div>;
}

0 comments on commit 8f703ed

Please sign in to comment.