swa07016

/mypick 라우팅 및 '/api/auth' 인증 api 구현

...@@ -4,6 +4,7 @@ import AboutPage from './pages/AboutPage'; ...@@ -4,6 +4,7 @@ import AboutPage from './pages/AboutPage';
4 import MenuPage from './pages/MenuPage'; 4 import MenuPage from './pages/MenuPage';
5 import SigninPage from './pages/SigninPage'; 5 import SigninPage from './pages/SigninPage';
6 import SignupPage from './pages/SignupPage'; 6 import SignupPage from './pages/SignupPage';
7 +import MypickPage from './pages/MypickPage';
7 8
8 import { 9 import {
9 BrowserRouter as Router, 10 BrowserRouter as Router,
...@@ -22,7 +23,7 @@ function App() { ...@@ -22,7 +23,7 @@ function App() {
22 <Route exact path="/menu" component={MenuPage}/> 23 <Route exact path="/menu" component={MenuPage}/>
23 <Route exact path="/signin" component={SigninPage}/> 24 <Route exact path="/signin" component={SigninPage}/>
24 <Route exact path="/signup" component={SignupPage}/> 25 <Route exact path="/signup" component={SignupPage}/>
25 - {/* mypick 라우팅 */} 26 + <Route exact path="/mypick" component={MypickPage}/>
26 </Switch> 27 </Switch>
27 </> 28 </>
28 </Router> 29 </Router>
......
1 +import React, { useState } from 'react'
2 +import NavBar from '../components/NavBar';
3 +
4 +// auth로 로그인한 사용자일 때와 아닐때 판단해서 화면을 다르게
5 +// 렌더링
6 +
7 +const MypickPage = () => {
8 + const [isLogin, setIsLogin] = useState(false);
9 + return (
10 + <>
11 + <NavBar/>
12 + {
13 + isLogin ? <h1>mypick</h1> : <a>aaa</a>
14 + }
15 + </>
16 + )
17 +}
18 +
19 +export default MypickPage;
...@@ -29,7 +29,7 @@ const SigninPage = (props) => { ...@@ -29,7 +29,7 @@ const SigninPage = (props) => {
29 }); 29 });
30 30
31 if (response.message === "Token issue") { 31 if (response.message === "Token issue") {
32 - localStorage.setItem("user_token", JSON.stringify(response.token)); 32 + localStorage.setItem("user", JSON.stringify(response.token));
33 alert('Login success'); 33 alert('Login success');
34 props.history.push('/'); 34 props.history.push('/');
35 } else if(response.message === "user does not exist"){ 35 } else if(response.message === "user does not exist"){
......
...@@ -33,10 +33,6 @@ connection.connect(); ...@@ -33,10 +33,6 @@ connection.connect();
33 app.use(bodyParser.json()); 33 app.use(bodyParser.json());
34 app.use(bodyParser.urlencoded({ extended: true })); 34 app.use(bodyParser.urlencoded({ extended: true }));
35 35
36 -app.get("/api/hello", (req, res) => {
37 - res.send("Hello skrrrr!");
38 -});
39 -
40 // datas 전달 36 // datas 전달
41 app.get("/api/datas", (req, res) => { 37 app.get("/api/datas", (req, res) => {
42 iconv.extendNodeEncodings(); 38 iconv.extendNodeEncodings();
...@@ -68,14 +64,6 @@ app.post("/api/signup", (req, res) => { ...@@ -68,14 +64,6 @@ app.post("/api/signup", (req, res) => {
68 }); 64 });
69 }); 65 });
70 66
71 -// ????
72 -// res.send({
73 -// "code":200,
74 -// "message": "success"
75 -// })
76 -// ????
77 -// jwt_secret_key.value
78 -// signin
79 app.post("/api/signin", (req, res) => { 67 app.post("/api/signin", (req, res) => {
80 68
81 const name = req.body.username; 69 const name = req.body.username;
...@@ -139,4 +127,35 @@ app.post("/api/signin", (req, res) => { ...@@ -139,4 +127,35 @@ app.post("/api/signin", (req, res) => {
139 }); 127 });
140 128
141 129
130 +// ?? ???
131 +app.get('/api/auth', (req, res) => {
132 + // ?? ??
133 + try {
134 + // ?? ??? ??? ??(req.headers.authorization)? ???? ???? ?? ??
135 + req.decoded = jwt.verify(req.headers.authorization, jwt_secret_key.value);
136 + return res.status(200).json({
137 + code: 200,
138 + message: 'valid token'
139 + });
140 + }
141 +
142 + // ?? ??
143 + catch (error) {
144 + // ????? ??? ??
145 + if (error.name === 'TokenExpiredError') {
146 + return res.status(419).json({
147 + code: 419,
148 + message: 'expired token'
149 + });
150 + }
151 +
152 + // ??? ???? ???? ?? ??
153 + return res.status(401).json({
154 + code: 401,
155 + message: 'invalid token'
156 + });
157 + }
158 +});
159 +
160 +
142 app.listen(port, () => console.log(`Listening on port ${port}`)); 161 app.listen(port, () => console.log(`Listening on port ${port}`));
......