Commit 54413fcf authored by seoyeon's avatar seoyeon
Browse files

0726

parent ab3ee0d6
import { useEffect, useState } from "react";
import { Redirect } from "react-router-dom";
import userApi from "../apis/user.api";
// import catchErrors from "../context/catchError";
import { useEffect, useState } from 'react'
import { Redirect } from 'react-router-dom'
import userApi from '../apis/user.api'
// import { useAuth } from '../context/auth_context'
import catchErrors from "../context/catchError";
const INIT_USER = {
email: "",
password: "",
};
email: '',
password: '',
}
const Login = () => {
// const { error, loading, login } = useAuth();
const [user, setUser] = useState(INIT_USER);
const [disabled, setDisabled] = useState(true);
const [error, setError] = useState("");
const [success, setSuccess] = useState(false);
// const { loading, login, catchErrorAuth } = useAuth()
const [user, setUser] = useState(INIT_USER)
const [disabled, setDisabled] = useState(true)
const [error, setError] = useState('')
const [success, setSuccess] = useState(false)
useEffect(() => {
const isUser = Object.values(user).every((el) => Boolean(el));
isUser ? setDisabled(false) : setDisabled(true);
}, [user]);
const isUser = Object.values(user).every((el) => Boolean(el))
isUser ? setDisabled(false) : setDisabled(true)
}, [user])
function handleChange(event) {
const { name, value } = event.target;
setUser({ ...user, [name]: value });
const { name, value } = event.target
setUser({ ...user, [name]: value })
}
async function handleSubmit(e) {
e.preventDefault();
e.preventDefault()
console.log('로그인')
try {
// setLoading(true);
// setError("");
const data = await userApi.login(user);
console.log(data);
setSuccess(true);
const data = await userApi.login(user)
console.log(data)
setSuccess(true)
} catch (error) {
// catchErrors(error, setError);
console.log('error in login', error)
catchErrors(error, setError)
} finally {
// setLoading(false);
}
}
if (success) {
alert('로그인 되었습니다')
return <Redirect to="/user" />;
return <Redirect to="/user" />
}
const { email, password } = user;
const { email, password } = user
return (
<div className="modal-content">
{error && <div className="alert alert-danger">{error}</div>}
<form onSubmit={handleSubmit}>
<div className="modal-header">
<h5 className="modal-title" id="loginModalLabel">
......@@ -87,13 +90,18 @@ const Login = () => {
</div>
</div>
<div className="modal-footer">
<button type="submit" className="btn btn-primary" disabled={disabled} data-bs-dismiss="modal">
<button
type="submit"
className="btn btn-primary"
disabled={disabled}
data-bs-dismiss="modal"
>
로그인
</button>
</div>
</form>
</div>
);
};
)
}
export default Login;
export default Login
import axios from "axios";
import { createContext, FC, useCallback, useContext, useState } from "react";
import authApi from "../apis/auth.api";
import authApi from "../apis/user.api";
import { getLocalUser } from "../utils/auth";
import baseUrl from "../utils/baseUrl";
import catchErrors from "../utils/catchErrors";
......
......@@ -9,22 +9,23 @@ const login = async (req, res) => {
console.log('login= ', req.body)
const { email, password } = req.body
const user = await User.findOne({ where: { email: email } })
if (!user)
if (!user) {
return res.status(422).send(`${email} 사용자가 존재하지 않습니다.`)
const passworMatch = await user.comparePassword(password)
if (passworMatch) {
const token = jwt.sign({ userID: user.id }, config.jwtSecret, {
expiresIn: config.jwtExpires,
})
res.cookie(config.cookieName, token, {
path: '/',
httpOnly: true,
secure: true,
})
res.json({ user })
} else {
res.status(401).send('비밀번호가 일치하지 않습니다.')
const passworMatch = await user.comparePassword(password)
if (passworMatch) {
const token = jwt.sign({ userID: user.id }, config.jwtSecret, {
expiresIn: config.jwtExpires,
})
res.cookie(config.cookieName, token, {
path: '/',
httpOnly: true,
secure: true,
})
res.json(user)
} else {
res.status(401).send('비밀번호가 일치하지 않습니다.')
}
}
} catch (error) {
console.log(error)
......@@ -56,24 +57,22 @@ const signup = async (req, res) => {
} else if (!isLength(email, { min: 3, max: 10 })) {
return res.status(422).send('아이디는 3-10자 사이입니다')
}
const hash = await bcrypt.hash(password, 10)
const newUser = await User.create({
id: id,
name: name,
email: email,
password: hash,
password: password,
gender: gender,
phone: phone,
}).then (_ =>
console.log("회원가입 정보",id, name, email, hash, gender, phone)
)
}).then(_ =>
console.log('회원가입 정보', id, name, email, password, gender, phone),
)
} catch (error) {
console.log(error)
return res.status(500).send('회원가입 중 에러')
}
}
export default {
login,
signup,
......
......@@ -8,6 +8,9 @@
"dev": "nodemon -r esm index.js",
"build": "tsc -p ."
},
"nodemonConfig": {
"ignore": ["test/*", "docs/*", "client/*"]
},
"repository": {
"type": "git",
"url": "https://compmath.korea.ac.kr/gitlab/research/bora_it.git"
......
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