김건희

[Update] Register, Login Redux Setting

...@@ -10380,6 +10380,11 @@ ...@@ -10380,6 +10380,11 @@
10380 "integrity": "sha512-cNJ8Q/EtjhQaZ71c8I9+BPySIBVEKssbPpskBfsXqb8HJ002A3KRVHfeRzwRo6mGPqsm7XuHTqNSNeS1Khig0A==", 10380 "integrity": "sha512-cNJ8Q/EtjhQaZ71c8I9+BPySIBVEKssbPpskBfsXqb8HJ002A3KRVHfeRzwRo6mGPqsm7XuHTqNSNeS1Khig0A==",
10381 "dev": true 10381 "dev": true
10382 }, 10382 },
10383 + "redux-thunk": {
10384 + "version": "2.4.1",
10385 + "resolved": "https://registry.npmjs.org/redux-thunk/-/redux-thunk-2.4.1.tgz",
10386 + "integrity": "sha512-OOYGNY5Jy2TWvTL1KgAlVy6dcx3siPJ1wTq741EPyUKfn6W6nChdICjZwCd0p8AZBs5kWpZlbkXW2nE/zjUa+Q=="
10387 + },
10383 "regenerate": { 10388 "regenerate": {
10384 "version": "1.4.2", 10389 "version": "1.4.2",
10385 "resolved": "https://registry.npmjs.org/regenerate/-/regenerate-1.4.2.tgz", 10390 "resolved": "https://registry.npmjs.org/regenerate/-/regenerate-1.4.2.tgz",
......
...@@ -18,6 +18,7 @@ ...@@ -18,6 +18,7 @@
18 "react-router-dom": "^6.3.0", 18 "react-router-dom": "^6.3.0",
19 "react-scripts": "5.0.1", 19 "react-scripts": "5.0.1",
20 "redux": "^4.1.2", 20 "redux": "^4.1.2",
21 + "redux-thunk": "^2.4.1",
21 "web-vitals": "^2.1.4" 22 "web-vitals": "^2.1.4"
22 }, 23 },
23 "scripts": { 24 "scripts": {
......
1 import React, { useCallback, useState } from "react"; 1 import React, { useCallback, useState } from "react";
2 -import { useDispatch } from "react-redux"; 2 +import { useDispatch, useSelector } from "react-redux";
3 +import { useNavigate } from "react-router-dom";
3 import { login } from "../../../modules/user"; 4 import { login } from "../../../modules/user";
4 import "../style/LoginPage.scss" 5 import "../style/LoginPage.scss"
5 6
6 function LoginPage(props) { 7 function LoginPage(props) {
7 const dispatch = useDispatch(); 8 const dispatch = useDispatch();
9 + const navigate = useNavigate();
10 +
11 + const loginResult = useSelector((state) => state.user.loginSuccess);
8 12
9 const [Id, setId] = useState(""); 13 const [Id, setId] = useState("");
10 const [Password, setPassword] = useState(""); 14 const [Password, setPassword] = useState("");
...@@ -58,20 +62,25 @@ function LoginPage(props) { ...@@ -58,20 +62,25 @@ function LoginPage(props) {
58 if (!checkLoginError) { 62 if (!checkLoginError) {
59 const UserData = { 63 const UserData = {
60 id: Id, 64 id: Id,
61 - password: Password, 65 + password: Password
62 }; 66 };
63 67
64 - dispatch(login(UserData)) 68 + dispatch(login(UserData));
65 - .then(res => { 69 + loginResult.then((result) => {
66 - if (res.payload.loginSuccess) { 70 + console.log(result);
67 - props.history.push('/register'); 71 +
68 - } else { 72 + if (result.loginSuccess === true) {
69 - alert(res.payload.error); 73 + alert('로그인 완료!');
70 - } 74 + navigate('/');
71 - }) 75 + }
76 + else {
77 + alert('로그인 실패ㅜㅜ');
78 + }
79 + })
80 +
72 } 81 }
73 82
74 - }, [checkIdError, checkPasswordError, Password]); 83 + }, [checkIdError, checkPasswordError, Password, dispatch, loginResult]);
75 84
76 return ( 85 return (
77 <div id = "body"> 86 <div id = "body">
......
...@@ -6,9 +6,9 @@ import { useNavigate } from "react-router-dom"; ...@@ -6,9 +6,9 @@ import { useNavigate } from "react-router-dom";
6 6
7 function RegisterPage(props) { 7 function RegisterPage(props) {
8 const dispatch = useDispatch(); 8 const dispatch = useDispatch();
9 - const naviagte = useNavigate(); 9 + const navigate = useNavigate();
10 10
11 - const registerResult = useSelector(state => state.user.registerSuccess); 11 + const registerResult = useSelector((state) => state.user.registerSuccess);
12 12
13 const [Name, setName] = useState(""); 13 const [Name, setName] = useState("");
14 const [Sex, setSex] = useState(""); 14 const [Sex, setSex] = useState("");
...@@ -103,15 +103,23 @@ function RegisterPage(props) { ...@@ -103,15 +103,23 @@ function RegisterPage(props) {
103 password: Password, 103 password: Password,
104 gender: Sex, 104 gender: Sex,
105 }; 105 };
106 -
107 - dispatch(register(UserData));
108 106
109 - if (registerResult) { 107 +
110 - alert('회원가입에 성공하였습니다.'); 108 + dispatch(register(UserData));
111 - naviagte('/login'); 109 + registerResult.then((result) => {
112 - } else { 110 + console.log(result);
113 - alert('회원가입에 실패하였습니다.'); 111 +
114 - } 112 + if (result.registerSuccess === '1') {
113 + alert('회원 가입 완료!');
114 + navigate('/login');
115 + }
116 + else if (result.registerSuccess === '0') {
117 + alert('중복된 아이디 존재ㅠㅠ');
118 + }
119 + else {
120 + alert('회원 가입 실패ㅜㅜ');
121 + }
122 + })
115 123
116 }; 124 };
117 125
......
...@@ -3,13 +3,14 @@ import ReactDOM from 'react-dom/client'; ...@@ -3,13 +3,14 @@ import ReactDOM from 'react-dom/client';
3 import './index.css'; 3 import './index.css';
4 import App from './App'; 4 import App from './App';
5 import { Provider } from 'react-redux'; 5 import { Provider } from 'react-redux';
6 -import { createStore } from 'redux'; 6 +import { applyMiddleware, createStore } from 'redux';
7 import reportWebVitals from './reportWebVitals'; 7 import reportWebVitals from './reportWebVitals';
8 import { BrowserRouter } from 'react-router-dom'; 8 import { BrowserRouter } from 'react-router-dom';
9 import rootReducer from './modules/Index'; 9 import rootReducer from './modules/Index';
10 import { composeWithDevTools } from 'redux-devtools-extension'; 10 import { composeWithDevTools } from 'redux-devtools-extension';
11 +import ReduxThunk from 'redux-thunk';
11 12
12 -const store = createStore(rootReducer, composeWithDevTools()); 13 +const store = createStore(rootReducer, composeWithDevTools(applyMiddleware(ReduxThunk)));
13 14
14 const root = ReactDOM.createRoot(document.getElementById('root')); 15 const root = ReactDOM.createRoot(document.getElementById('root'));
15 root.render( 16 root.render(
......
1 import axios from 'axios'; 1 import axios from 'axios';
2 2
3 -const REGISTER = 'user/REGISTER'; 3 +const USER_REGISTER = 'user/REGISTER';
4 -const LOGIN = 'user/LOGIN'; 4 +const USER_LOGIN = 'user/LOGIN';
5 5
6 -export function register(data) { 6 +export function register(dataToSubmit) {
7 - const req = axios.post('http://localhost:4000/api/register', data) 7 +
8 + const req = axios.post('http://localhost:4000/api/register', dataToSubmit)
8 .then(res => res.data); 9 .then(res => res.data);
9 10
10 return { 11 return {
11 - type: REGISTER, 12 + type: USER_REGISTER,
12 payload: req, 13 payload: req,
13 } 14 }
14 -} 15 +};
15 16
16 -export function login(data) { 17 +export function login(dataToSubmit) {
17 - const req = axios.post('http://localhost:4000/api/login', data) 18 + const req = axios.post('http://localhost:4000/api/login', dataToSubmit)
18 - .then(res => res.date); 19 + .then(res => res.data);
19 20
20 return { 21 return {
21 - type: LOGIN, 22 + type: USER_LOGIN,
22 payload: req, 23 payload: req,
23 } 24 }
24 -} 25 +};
25 26
26 export default function (state = {}, action) { 27 export default function (state = {}, action) {
27 switch (action.type) { 28 switch (action.type) {
28 - case REGISTER: 29 + case USER_REGISTER:
29 return { ...state, registerSuccess: action.payload }; 30 return { ...state, registerSuccess: action.payload };
30 - case LOGIN: 31 + break;
32 + case USER_LOGIN:
31 return { ...state, loginSuccess: action.payload }; 33 return { ...state, loginSuccess: action.payload };
34 + break;
32 default: 35 default:
33 return state; 36 return state;
34 } 37 }
......