김건희

[Add] Login Redux Setting

import React, { useCallback, useState } from "react";
import { useDispatch } from "react-redux";
import { login } from "../../../modules/user";
import "../style/LoginPage.scss"
function LoginPage(props) {
const dispatch = useDispatch();
const [Id, setId] = useState("");
const [Password, setPassword] = useState("");
......@@ -51,12 +54,21 @@ function LoginPage(props) {
setCheckLoginError(false);
}
// login data
// login
if (!checkLoginError) {
const UserData = {
id: Id,
password: Password,
};
dispatch(login(UserData))
.then(res => {
if (res.payload.logged) {
props.history.push('/register');
} else {
alert(res.payload.error);
}
})
}
}, [checkIdError, checkPasswordError, Password]);
......
import axios from 'axios';
const REGISTER = 'user/REGISTER';
const LOGIN = 'user/LOGIN'
const InitialState = {
logged: false,
name: 'test',
id: 'test123',
password: 'test1111~',
......@@ -19,11 +21,24 @@ export function register(data) {
}
};
export function login(data) {
const req = axios.post('http://localhost:4000/login', data)
.then(res => res.date);
return {
type: LOGIN,
payload: req,
}
};
export default function (state = InitialState, action) {
switch (action.type) {
case REGISTER:
return {...state, register: action.payload};
break;
case LOGIN:
return {...state, logged: action.payload};
break;
default:
return state;
}
......