Commit 042b0a4e authored by seoyeon's avatar seoyeon
Browse files

0724

parent d9089b86
import { useEffect, useState } from "react"; import { useEffect, useState } from "react";
import { Redirect } from "react-router-dom"; import { Redirect } from "react-router-dom";
import userApi from "../apis/user.api"; import userApi from "../apis/user.api";
// import catchErrors from "../context/catchError";
const INIT_USER = { const INIT_USER = {
email: "", email: "",
password: "", password: "",
...@@ -25,6 +26,7 @@ const Login = () => { ...@@ -25,6 +26,7 @@ const Login = () => {
async function handleSubmit(e) { async function handleSubmit(e) {
e.preventDefault(); e.preventDefault();
console.log('로그인')
try { try {
// setLoading(true); // setLoading(true);
// setError(""); // setError("");
...@@ -37,12 +39,9 @@ const Login = () => { ...@@ -37,12 +39,9 @@ const Login = () => {
// setLoading(false); // setLoading(false);
} }
} }
if (success) {
return <Redirect to="/user" />;
}
if (success) { if (success) {
alert('로그인 되었습니다') alert('로그인 되었습니다')
return <Redirect to="/user" />;
} }
const { email, password } = user; const { email, password } = user;
......
import axios from "axios"; import axios from "axios";
import { useEffect, useState } from "react"; import { useEffect, useState } from "react";
import { Redirect } from "react-router-dom";
import userApi from "../apis/user.api";
// import catchErrors from "../context/catchError";
// import auth from "../context/auth_context"
const INIT_USER = { const INIT_USER = {
name: "", name: "",
idNumber1: "", idNumber1: "",
...@@ -16,6 +19,7 @@ const Signup = () => { ...@@ -16,6 +19,7 @@ const Signup = () => {
const [error, setError] = useState(""); const [error, setError] = useState("");
const [disabled, setDisabled] = useState(false); const [disabled, setDisabled] = useState(false);
const [success, setSuccess] = useState(false); const [success, setSuccess] = useState(false);
const [loading, setLoading] = useState(false);
useEffect(() => { useEffect(() => {
setDisabled( setDisabled(
...@@ -38,21 +42,23 @@ const Signup = () => { ...@@ -38,21 +42,23 @@ const Signup = () => {
async function handleSubmit() { async function handleSubmit() {
try { try {
const data = await axios.post("https://localhost:8080/api/room/1/1",user) const data = await userApi.signup(user)
// setLoading(true); // const data = await axios.post("https://localhost:8080/api/room/1/1",user)
// setError(""); setLoading(true);
// const success = await login(user.email, user.password); setError("");
// const success = await login(user.email, user.password);
console.log(data); console.log(data);
setSuccess(success); // sestSuccess(success);
} catch (error) { } catch (error) {
// catchErrors(error, setError); // catchErrors(error, setError);
} finally { } finally {
// setLoading(false); setLoading(false);
} }
} }
if (success) { if (success) {
alert('회원가입 되었습니다.') alert('회원가입 되었습니다.')
return <Redirect to="/" />;
} }
......
function catchErrors(error, displayError) {
let errorMsg
if (error.response) {
errorMsg = error.response.data
console.log(errorMsg)
}else if (error.requset) {
errorMsg = error.requset
console.log(errorMsg)
} else {
errorMsg = error.message
console.log(errorMsg)
}
displayError(errorMsg)
}
export default catchErrors
...@@ -45,8 +45,35 @@ const login = async (req, res) => { ...@@ -45,8 +45,35 @@ const login = async (req, res) => {
const signup = async (req, res) => { const signup = async (req, res) => {
try { try {
console.log("sign up= ", req.body); console.log("sign up= ", req.body);
const { id, name, email, password, gender, phone } = req.body; const { name, email, password, gender, phone } = req.body;
const user = User.create({ id, name, email, password, gender, phone }); const id = Math.floor(( Math.random() * (9999 - 1000) + 1000 ));
console.log('id:', id)
const Id = await User.findOne({ where: { id: id }})
console.log("Id 중복확인:", Id)
while (Id) {
const id = Math.floor((Math.random() * (9999 - 1000) + 1000 ));
const Id = await User.findOne({ where: {id: id}});
}
const user = await User.findOne({ where: { email: email } });
if (user)
return res.status(422).send(`${email} 이미 존재하는 사용자입니다.`);
if (!isLength(name, {min:3, max:10})) {
return res.status(422).send('이름은 3-10자 사이입니다')
} else if (!isLength(password, { min: 6 })){
return res.status(422).send('비밀번호는 6자이상 입니다')
} else if (!isLength(email,{ min:3, max:10 })) {
return res.status(422).send('아이디는 3-10자 사이입니다')
}
const newUser = User.create({
id: id,
name: name,
email: email,
password: password,
gender: gender,
phone: phone,
});
} catch (error) { } catch (error) {
console.log(error); console.log(error);
return res.status(500).send("회원가입 중 에러"); return res.status(500).send("회원가입 중 에러");
......
...@@ -26,6 +26,7 @@ const UserModel = (sequelize) => { ...@@ -26,6 +26,7 @@ const UserModel = (sequelize) => {
}, },
img: { img: {
type: DataTypes.ARRAY(DataTypes.INTEGER), type: DataTypes.ARRAY(DataTypes.INTEGER),
default:'/user.png'
}, },
roomNumber: { roomNumber: {
type: DataTypes.ARRAY(DataTypes.STRING), type: DataTypes.ARRAY(DataTypes.STRING),
......
...@@ -6,5 +6,4 @@ const router = express.Router(); ...@@ -6,5 +6,4 @@ const router = express.Router();
router.route("/login").post(userCtrl.login); router.route("/login").post(userCtrl.login);
router.route("/signup").post(userCtrl.signup); router.route("/signup").post(userCtrl.signup);
router.route("/").post(userCtrl.signup);
export default router; export default router;
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment