swa07016

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

......@@ -4,6 +4,7 @@ import AboutPage from './pages/AboutPage';
import MenuPage from './pages/MenuPage';
import SigninPage from './pages/SigninPage';
import SignupPage from './pages/SignupPage';
import MypickPage from './pages/MypickPage';
import {
BrowserRouter as Router,
......@@ -22,7 +23,7 @@ function App() {
<Route exact path="/menu" component={MenuPage}/>
<Route exact path="/signin" component={SigninPage}/>
<Route exact path="/signup" component={SignupPage}/>
{/* mypick 라우팅 */}
<Route exact path="/mypick" component={MypickPage}/>
</Switch>
</>
</Router>
......
import React, { useState } from 'react'
import NavBar from '../components/NavBar';
// auth로 로그인한 사용자일 때와 아닐때 판단해서 화면을 다르게
// 렌더링
const MypickPage = () => {
const [isLogin, setIsLogin] = useState(false);
return (
<>
<NavBar/>
{
isLogin ? <h1>mypick</h1> : <a>aaa</a>
}
</>
)
}
export default MypickPage;
......@@ -29,7 +29,7 @@ const SigninPage = (props) => {
});
if (response.message === "Token issue") {
localStorage.setItem("user_token", JSON.stringify(response.token));
localStorage.setItem("user", JSON.stringify(response.token));
alert('Login success');
props.history.push('/');
} else if(response.message === "user does not exist"){
......
......@@ -33,10 +33,6 @@ connection.connect();
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.get("/api/hello", (req, res) => {
res.send("Hello skrrrr!");
});
// datas 전달
app.get("/api/datas", (req, res) => {
iconv.extendNodeEncodings();
......@@ -68,14 +64,6 @@ app.post("/api/signup", (req, res) => {
});
});
// ????
// res.send({
// "code":200,
// "message": "success"
// })
// ????
// jwt_secret_key.value
// signin
app.post("/api/signin", (req, res) => {
const name = req.body.username;
......@@ -139,4 +127,35 @@ app.post("/api/signin", (req, res) => {
});
// ?? ???
app.get('/api/auth', (req, res) => {
// ?? ??
try {
// ?? ??? ??? ??(req.headers.authorization)? ???? ???? ?? ??
req.decoded = jwt.verify(req.headers.authorization, jwt_secret_key.value);
return res.status(200).json({
code: 200,
message: 'valid token'
});
}
// ?? ??
catch (error) {
// ????? ??? ??
if (error.name === 'TokenExpiredError') {
return res.status(419).json({
code: 419,
message: 'expired token'
});
}
// ??? ???? ???? ?? ??
return res.status(401).json({
code: 401,
message: 'invalid token'
});
}
});
app.listen(port, () => console.log(`Listening on port ${port}`));
......