swa07016

token 기반 mypick username set

This diff is collapsed. Click to expand it.
...@@ -14,7 +14,6 @@ ...@@ -14,7 +14,6 @@
14 "axios": "^0.19.2", 14 "axios": "^0.19.2",
15 "bootstrap": "^4.5.0", 15 "bootstrap": "^4.5.0",
16 "http-proxy-middleware": "^1.0.4", 16 "http-proxy-middleware": "^1.0.4",
17 - "mdbreact": "^4.27.0",
18 "react": "^16.13.1", 17 "react": "^16.13.1",
19 "react-app-polyfill": "^1.0.6", 18 "react-app-polyfill": "^1.0.6",
20 "react-cookies": "^0.1.1", 19 "react-cookies": "^0.1.1",
......
...@@ -69,16 +69,17 @@ const UserCards = (props) => { ...@@ -69,16 +69,17 @@ const UserCards = (props) => {
69 'http://localhost:5000/api/datas', 69 'http://localhost:5000/api/datas',
70 // localhost로 바꾸기 70 // localhost로 바꾸기
71 ); 71 );
72 - setDatas(result.data); 72 + setDatas(result.data);
73 }; 73 };
74 fetchData(); 74 fetchData();
75 - setUsername(cookie.load('username')); 75 + setUsername(props.username);
76 + // setUsername(cookie.load('username'));
76 }, [username]); 77 }, [username]);
77 78
78 const LogoutHandler = (e) => { 79 const LogoutHandler = (e) => {
79 e.preventDefault(); 80 e.preventDefault();
80 localStorage.removeItem('user'); 81 localStorage.removeItem('user');
81 - cookie.remove('username'); 82 + // cookie.remove('username');
82 props.isLogin(false); 83 props.isLogin(false);
83 return ; 84 return ;
84 } 85 }
......
...@@ -3,9 +3,6 @@ import NavBar from '../components/NavBar'; ...@@ -3,9 +3,6 @@ import NavBar from '../components/NavBar';
3 import MealCard from '../components/MealCard'; 3 import MealCard from '../components/MealCard';
4 import { CustomInput } from 'reactstrap'; 4 import { CustomInput } from 'reactstrap';
5 import { Container, Row, Col } from "reactstrap"; 5 import { Container, Row, Col } from "reactstrap";
6 -
7 -import { Card, CardText, CardBody, CardTitle, } from "reactstrap";
8 -
9 import axios from 'axios'; 6 import axios from 'axios';
10 import Loading from '../components/Loading'; 7 import Loading from '../components/Loading';
11 8
...@@ -190,9 +187,6 @@ const MenuPage = (props) => { ...@@ -190,9 +187,6 @@ const MenuPage = (props) => {
190 </Container>) 187 </Container>)
191 : <Loading value="Loading.."/> 188 : <Loading value="Loading.."/>
192 } 189 }
193 -
194 -
195 -
196 </> 190 </>
197 ); 191 );
198 } 192 }
......
...@@ -9,6 +9,7 @@ import { Container } from 'reactstrap'; ...@@ -9,6 +9,7 @@ import { Container } from 'reactstrap';
9 9
10 const MypickPage = () => { 10 const MypickPage = () => {
11 const [isLogin, setIsLogin] = useState(false); 11 const [isLogin, setIsLogin] = useState(false);
12 + const [userName, setUserName] = useState('');
12 const authApi = () => { 13 const authApi = () => {
13 const user = JSON.parse(localStorage.getItem('user')); 14 const user = JSON.parse(localStorage.getItem('user'));
14 return fetch('/api/auth', { 15 return fetch('/api/auth', {
...@@ -21,7 +22,7 @@ const MypickPage = () => { ...@@ -21,7 +22,7 @@ const MypickPage = () => {
21 .then(result => { 22 .then(result => {
22 if(result.message === 'valid token') { 23 if(result.message === 'valid token') {
23 setIsLogin(true); 24 setIsLogin(true);
24 - 25 + setUserName(result.username);
25 } else if(result.message === 'expired token') { 26 } else if(result.message === 'expired token') {
26 // alert('토큰이 만료되었습니다. 로그인 해주세요.'); 27 // alert('토큰이 만료되었습니다. 로그인 해주세요.');
27 setIsLogin(false); 28 setIsLogin(false);
...@@ -42,7 +43,7 @@ const MypickPage = () => { ...@@ -42,7 +43,7 @@ const MypickPage = () => {
42 { 43 {
43 isLogin ? 44 isLogin ?
44 (<> 45 (<>
45 - <UserCards isLogin={setIsLogin}/> 46 + <UserCards username={userName} isLogin={setIsLogin}/>
46 </>) 47 </>)
47 : 48 :
48 (<> 49 (<>
......
...@@ -601,6 +601,11 @@ ...@@ -601,6 +601,11 @@
601 "safe-buffer": "^5.0.1" 601 "safe-buffer": "^5.0.1"
602 } 602 }
603 }, 603 },
604 + "jwt-decode": {
605 + "version": "2.2.0",
606 + "resolved": "https://registry.npmjs.org/jwt-decode/-/jwt-decode-2.2.0.tgz",
607 + "integrity": "sha1-fYa9VmefWM5qhHBKZX3TkruoGnk="
608 + },
604 "locate-path": { 609 "locate-path": {
605 "version": "3.0.0", 610 "version": "3.0.0",
606 "resolved": "https://registry.npmjs.org/locate-path/-/locate-path-3.0.0.tgz", 611 "resolved": "https://registry.npmjs.org/locate-path/-/locate-path-3.0.0.tgz",
......
...@@ -21,6 +21,7 @@ ...@@ -21,6 +21,7 @@
21 "express": "^4.17.1", 21 "express": "^4.17.1",
22 "iconv-lite": "^0.5.1", 22 "iconv-lite": "^0.5.1",
23 "jsonwebtoken": "^8.5.1", 23 "jsonwebtoken": "^8.5.1",
24 + "jwt-decode": "^2.2.0",
24 "mysql": "^2.18.1" 25 "mysql": "^2.18.1"
25 } 26 }
26 } 27 }
......
...@@ -4,6 +4,7 @@ const fs = require("fs"); ...@@ -4,6 +4,7 @@ const fs = require("fs");
4 const mysql = require("mysql"); 4 const mysql = require("mysql");
5 const iconv = require("iconv-lite"); 5 const iconv = require("iconv-lite");
6 const jwt = require("jsonwebtoken"); 6 const jwt = require("jsonwebtoken");
7 +const jwt_decode = require('jwt-decode');
7 8
8 const bcrypt = require("bcrypt"); 9 const bcrypt = require("bcrypt");
9 const saltRounds = 10; 10 const saltRounds = 10;
...@@ -144,13 +145,17 @@ app.post("/api/signin", (req, res) => { ...@@ -144,13 +145,17 @@ app.post("/api/signin", (req, res) => {
144 145
145 // ?? ??? 146 // ?? ???
146 app.get('/api/auth', (req, res) => { 147 app.get('/api/auth', (req, res) => {
147 - // ?? ?? 148 +
149 +
150 + const user = jwt_decode(req.headers.authorization);
151 + console.log(user.name);
148 try { 152 try {
149 // ?? ??? ??? ??(req.headers.authorization)? ???? ???? ?? ?? 153 // ?? ??? ??? ??(req.headers.authorization)? ???? ???? ?? ??
150 req.decoded = jwt.verify(req.headers.authorization, jwt_secret_key.value); 154 req.decoded = jwt.verify(req.headers.authorization, jwt_secret_key.value);
151 return res.status(200).json({ 155 return res.status(200).json({
152 code: 200, 156 code: 200,
153 - message: 'valid token' 157 + message: 'valid token',
158 + username: user.name
154 }); 159 });
155 } 160 }
156 161
......