정의왕

Complete Auth routing

...@@ -2,11 +2,9 @@ const express = require("express"); ...@@ -2,11 +2,9 @@ const express = require("express");
2 const bodyParser = require("body-parser"); 2 const bodyParser = require("body-parser");
3 const router = express.Router(); 3 const router = express.Router();
4 4
5 -
6 -
7 router.get('/',(req,res)=>{ 5 router.get('/',(req,res)=>{
8 if (req.session.name != null && req.session.name != undefined){ 6 if (req.session.name != null && req.session.name != undefined){
9 - console.log("req.session.name"); 7 + console.log(req.session.name);
10 res.sendStatus(200); 8 res.sendStatus(200);
11 } 9 }
12 else{ 10 else{
...@@ -14,8 +12,4 @@ router.get('/',(req,res)=>{ ...@@ -14,8 +12,4 @@ router.get('/',(req,res)=>{
14 res.sendStatus(401); 12 res.sendStatus(401);
15 } 13 }
16 }); 14 });
17 -
18 -
19 -
20 -
21 module.exports = router; 15 module.exports = router;
...\ No newline at end of file ...\ No newline at end of file
......
...@@ -10,15 +10,13 @@ router.get('/',(req,res)=>{ ...@@ -10,15 +10,13 @@ router.get('/',(req,res)=>{
10 10
11 //로그아웃 11 //로그아웃
12 router.post('/',(req,res)=>{ 12 router.post('/',(req,res)=>{
13 - if(result.dataValues.pw == req.body.Password){ 13 + req.session.id = null;
14 - req.session.id = null; 14 + req.session.name = null;
15 - req.session.name = null; 15 + req.session.personality = null;
16 - req.session.personality = null; 16 + req.session.status = null;
17 - req.session.status = null; 17 + req.session.isAuthorized = null;
18 - req.session.isAuthorized = null; 18 + console.log("Logout");
19 - console.log("Logout"); 19 + res.sendStatus(200);
20 - res.sendStatus(200); 20 +})
21 - }
22 - })
23 21
24 module.exports = router; 22 module.exports = router;
...\ No newline at end of file ...\ No newline at end of file
......
...@@ -41,7 +41,4 @@ router.get("/:id",(req,res)=>{ ...@@ -41,7 +41,4 @@ router.get("/:id",(req,res)=>{
41 console.log(req.params, req.query); 41 console.log(req.params, req.query);
42 req.body; 42 req.body;
43 }); 43 });
44 -
45 -
46 -
47 module.exports = router; 44 module.exports = router;
...\ No newline at end of file ...\ No newline at end of file
......
...@@ -5,21 +5,18 @@ import LoginPage from "./component/views/LoginPage/LoginPage"; ...@@ -5,21 +5,18 @@ import LoginPage from "./component/views/LoginPage/LoginPage";
5 import RegisterPage from "./component/views/RegisterPage/RegisterPage"; 5 import RegisterPage from "./component/views/RegisterPage/RegisterPage";
6 import "./static/fonts/font.css"; 6 import "./static/fonts/font.css";
7 import Auth from './hoc/auth'; 7 import Auth from './hoc/auth';
8 -
9 function App () { 8 function App () {
10 return ( 9 return (
11 <Router> 10 <Router>
12 <div> 11 <div>
13 - <Routes> 12 + <Routes>
14 - <Route exact path="/main" element={Auth(MainPage, true)} /> 13 + <Route exact path="/" element={Auth(LoginPage , false)}/>
15 - <Route exact path="/login" element={Auth(LoginPage , false)}> 14 + <Route exact path="/login" element={Auth(LoginPage , false)}/>
16 - </Route> 15 + <Route exact path="/main" element={Auth(MainPage, true)} />
17 - <Route exact path="/register" element={Auth(RegisterPage , false)}> 16 + <Route exact path="/register" element={Auth(RegisterPage , false)}/>
18 - </Route> 17 + </Routes>
19 - </Routes>
20 </div> 18 </div>
21 </Router> 19 </Router>
22 ); 20 );
23 } 21 }
24 - 22 +export default App;
25 -export default App;
...\ No newline at end of file ...\ No newline at end of file
......
...@@ -3,7 +3,6 @@ import "../style/LoginPage.scss"; ...@@ -3,7 +3,6 @@ import "../style/LoginPage.scss";
3 import Axios from 'axios' 3 import Axios from 'axios'
4 import { Icon, Input } from "semantic-ui-react" 4 import { Icon, Input } from "semantic-ui-react"
5 import { useNavigate } from "react-router-dom"; 5 import { useNavigate } from "react-router-dom";
6 -
7 function LoginPage() { 6 function LoginPage() {
8 let navigate = useNavigate(); 7 let navigate = useNavigate();
9 const [Id, setId] = useState(""); 8 const [Id, setId] = useState("");
......
1 import {Button} from "semantic-ui-react" 1 import {Button} from "semantic-ui-react"
2 +import Axios from'axios'
2 import "../style/MainPage.scss"; 3 import "../style/MainPage.scss";
3 import { useNavigate } from "react-router-dom"; 4 import { useNavigate } from "react-router-dom";
4 import { CKEditor } from '@ckeditor/ckeditor5-react'; 5 import { CKEditor } from '@ckeditor/ckeditor5-react';
5 import ClassicEditor from '@ckeditor/ckeditor5-build-classic'; 6 import ClassicEditor from '@ckeditor/ckeditor5-build-classic';
6 7
7 function MainPage(props) { 8 function MainPage(props) {
8 - const isLogin = props.isLogin;
9 const navigate = useNavigate(); 9 const navigate = useNavigate();
10 const onLogout = () => { 10 const onLogout = () => {
11 - // sessionStorage 에 user_id 로 저장되어있는 아이템을 삭제한다. 11 + Axios.post('/api/logout')
12 - sessionStorage.removeItem('isAuthorized') 12 + .then((res)=>{
13 - // App 으로 이동(새로고침) 13 + if(res.status === 200){
14 - navigate('/login'); 14 + alert("성공적으로 로그아웃하였습니다.")
15 + navigate('/login')
16 + }
17 + }) // App 으로 이동(새로고침)
15 } 18 }
16 return ( 19 return (
17 <div id="Main"> 20 <div id="Main">
...@@ -96,5 +99,4 @@ function MainPage(props) { ...@@ -96,5 +99,4 @@ function MainPage(props) {
96 </div> 99 </div>
97 ); 100 );
98 } 101 }
99 - 102 +export default MainPage;
100 -export default MainPage;
...\ No newline at end of file ...\ No newline at end of file
......
...@@ -3,14 +3,12 @@ import { useNavigate } from "react-router-dom"; ...@@ -3,14 +3,12 @@ import { useNavigate } from "react-router-dom";
3 import "../style/RegisterPage.scss"; 3 import "../style/RegisterPage.scss";
4 import { Button, Icon, Input } from "semantic-ui-react"; 4 import { Button, Icon, Input } from "semantic-ui-react";
5 import Axios from 'axios' 5 import Axios from 'axios'
6 -
7 function RegisterPage(props) { 6 function RegisterPage(props) {
8 const navigate = useNavigate(); 7 const navigate = useNavigate();
9 const [Id, setId] = useState(""); 8 const [Id, setId] = useState("");
10 const [Password, setPassword] = useState(""); 9 const [Password, setPassword] = useState("");
11 const [PasswordCheck,setPasswordCheck] = useState(""); 10 const [PasswordCheck,setPasswordCheck] = useState("");
12 const [Personality, setPersonality] = useState(""); 11 const [Personality, setPersonality] = useState("");
13 -
14 const onIdHandler = (event) => { 12 const onIdHandler = (event) => {
15 setId(event.currentTarget.value); 13 setId(event.currentTarget.value);
16 }; 14 };
......
1 import React, {useEffect} from 'react'; 1 import React, {useEffect} from 'react';
2 import Axios from 'axios'; 2 import Axios from 'axios';
3 import { useNavigate } from 'react-router-dom'; 3 import { useNavigate } from 'react-router-dom';
4 -export default function (SpecificComponent) { 4 +export default function (SpecificComponent, option, adminRoute = null) {
5 // option 5 // option
6 // null -> 아무나 출입, true -> 로그인한 유저만 출입, false -> 로그인한 유저는 출입 불가능 6 // null -> 아무나 출입, true -> 로그인한 유저만 출입, false -> 로그인한 유저는 출입 불가능
7 function AuthenticationCheck() { 7 function AuthenticationCheck() {
...@@ -9,13 +9,18 @@ export default function (SpecificComponent) { ...@@ -9,13 +9,18 @@ export default function (SpecificComponent) {
9 useEffect(() => { 9 useEffect(() => {
10 Axios.get('/api/auth') 10 Axios.get('/api/auth')
11 .then((res)=>{ 11 .then((res)=>{
12 - if(res.status === 200){ 12 + if(res.status === 200 && adminRoute){
13 navigate('/main') 13 navigate('/main')
14 } 14 }
15 + else{
16 + if(option === false){
17 + navigate('/main')
18 + }
19 + }
15 }).catch((error) => { 20 }).catch((error) => {
16 - console.log(error.response); 21 + if(option){
17 - alert('로그인이 필요합니다!'); 22 + navigate('/login')
18 - navigate('/login'); 23 + }
19 }) 24 })
20 }, []) 25 }, [])
21 26
......