정의왕

Complete Auth routing

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