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

0726

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