Commit f54a6948 authored by CHAERIN KIM's avatar CHAERIN KIM
Browse files

find 완료-가영change연결 필요

parent 2bfc9c64
import React, { useState, useEffect } from 'react';
function Change() {
return (
<div>
<div className="container">change
</div>
</div>
)
}
export default Change
\ No newline at end of file
import React, { useState } from 'react';
import { Field, Form, Formik } from 'formik';
import * as Yup from 'yup';
import axios from 'axios';
import 'bootstrap/dist/css/bootstrap.css';
import { Link, Redirect } from 'react-router-dom';
function Find() {
const [state, setState] = useState(false);
if (state) {
return <Redirect to="/change" />;
}
return (
<div className="d-flex flex-column justify-content-between vh-100">
<Formik
initialValues={{ id: '', question: '', answer: '' }}
validationSchema={Yup.object({
id: Yup.string()
.required('학번을 입력해주세요.'),
answer: Yup.string()
.required('답변을 입력해주세요.'),
})}
onSubmit={(values, { setSubmitting }) => {
axios({
method: 'post',
url: '/login/find',
data: values,
}).then(res => {
if (res.status === 404) return alert(res.data.error)
setState(true);
})
.catch(err => {
alert(err.error)
});
console.log(values);
setTimeout(() => {
setSubmitting(false);
}, 400); // finish the cycle in handler
}}
>
{({
errors,
touched,
handleSubmit,
getFieldProps, // contain values, handleChange, handleBlur
isSubmitting,
}) => (
<div className="row justify-content-center align-items-center">
<form onSubmit={handleSubmit} className="col-sm-3">
<div className="form-group mb-4">
<input
className={(touched.id && errors.id ? 'form-control is-invalid' : "form-control")}
type="number"
name="id"
{...getFieldProps('id')}
placeholder="Input Student Id"
/>
{touched.id && errors.id ? (
<div className="invalid-feedback text-left">{errors.id}</div>
) : null}
</div>
<div className="form-group mb-4">
<label>본인 확인 질문</label>
<Field as="select" name="question">
<option value="">질문을 선택하세요</option>
<option value="life">자신의 인생 좌우명은?</option>
<option value="school">자신이 다녔던 초등학교의 이름은?</option>
<option value="place">기억에 남는 추억의 장소는?</option>
</Field>
</div>
<div className="form-group mb-4">
<input
className={(touched.answer && errors.answer ? 'form-control is-invalid' : "form-control")}
type="text"
name="answer"
{...getFieldProps('answer')}
placeholder="Input answer" />
{touched.answer && errors.answer ? (
<div className="invalid-feedback text-left">{errors.answer}</div>
) : null}
</div>
<button type="submit" className="btn btn-dark" disabled={isSubmitting}>
submit
</button>
<button><Link to="/login">로그인</Link></button>
<button><Link to="/"></Link></button>
</form>
</div>
)}
</Formik>
</div>
);
}
// const withFormik = Formik({
// mapPropsToValues: () => ({ color: '' }),
// validationSchema: Yup.object().shape({
// color: Yup.string().required('Color is required!'),
// }),
// handleSubmit: (values, { setSubmitting }) => {
// setTimeout(() => {
// alert(JSON.stringify(values, null, 2));
// setSubmitting(false);
// }, 1000);
// },
// displayName: 'BasicForm', // helps with React DevTools
// });
// const MyForm = props => {
// const {
// values,
// touched,
// errors,
// dirty,
// isSubmitting,
// handleChange,
// handleBlur,
// handleSubmit,
// handleReset,
// } = props;
// return (
// <form onSubmit={handleSubmit}>
// <label htmlFor="email" style={{ display: 'block' }}>
// Color
// </label>
// <select
// name="color"
// value={values.color}
// onChange={handleChange}
// onBlur={handleBlur}
// style={{ display: 'block' }}
// >
// <option value="" label="Select a color" />
// <option value="red" label="red" />
// <option value="blue" label="blue" />
// <option value="green" label="green" />
// </select>
// {errors.color &&
// touched.color &&
// <div className="input-feedback">
// {errors.color}
// </div>}
// <button
// type="button"
// className="outline"
// onClick={handleReset}
// disabled={!dirty || isSubmitting}
// >
// Reset
// </button>
// <button type="submit" disabled={isSubmitting}>
// Submit
// </button>
// <DisplayFormikState {...props} />
// </form>
// );
// };
export default Find;
...@@ -8,7 +8,7 @@ function Home() { ...@@ -8,7 +8,7 @@ function Home() {
<Menu /> <Menu />
<div className="container"> <div className="container">
home home
<button><Link to="/">로그인</Link></button> <button><Link to="/login">로그인</Link></button>
<button><Link to="/signup">회원가입</Link></button> <button><Link to="/signup">회원가입</Link></button>
</div> </div>
</div> </div>
......
...@@ -7,10 +7,10 @@ import axios from 'axios'; ...@@ -7,10 +7,10 @@ import axios from 'axios';
import 'bootstrap/dist/css/bootstrap.css'; import 'bootstrap/dist/css/bootstrap.css';
const Log = styled.div` const Log = styled.div`
background-color: #981e1e; background-color: #7B031D;
` `
const Logo = styled.div` const Logo = styled.div`
background-color: #E76A6A; background-color: rgb(239, 218, 200);
` `
...@@ -97,7 +97,7 @@ function Login() { ...@@ -97,7 +97,7 @@ function Login() {
</button> </button>
<button><Link to="/home"></Link></button> <button><Link to="/home"></Link></button>
<div></div> <div></div>
<Link to="/signup">비밀번호를 잊으셨나요?</Link> <Link to="/find">비밀번호를 잊으셨나요?</Link>
<div></div> <div></div>
<Link to="/signup">회원이 아니신가요?</Link> <Link to="/signup">회원이 아니신가요?</Link>
</form> </form>
......
import React, { useState } from 'react'; import React, { useState } from 'react';
import { Formik } from 'formik'; import { Field, Formik } from 'formik';
import * as Yup from 'yup'; import * as Yup from 'yup';
import axios from 'axios'; import axios from 'axios';
import 'bootstrap/dist/css/bootstrap.css'; import 'bootstrap/dist/css/bootstrap.css';
...@@ -15,7 +15,7 @@ function Signup() { ...@@ -15,7 +15,7 @@ function Signup() {
return ( return (
<div className="d-flex flex-column justify-content-between vh-100"> <div className="d-flex flex-column justify-content-between vh-100">
<Formik <Formik
initialValues={{ name: '', id: '', password: '', password2: ''}} initialValues={{ name: '', id: '', password: '', password2: '', question: '', answer: '' }}
validationSchema={Yup.object({ validationSchema={Yup.object({
name: Yup.string() name: Yup.string()
.required('이름을 입력해주세요.'), .required('이름을 입력해주세요.'),
...@@ -28,6 +28,8 @@ function Signup() { ...@@ -28,6 +28,8 @@ function Signup() {
.required('비밀번호를 다시 입력해주세요.') .required('비밀번호를 다시 입력해주세요.')
.min(8, '8자 이상 입력해주세요.') .min(8, '8자 이상 입력해주세요.')
.oneOf([Yup.ref("password"), null], '비밀번호가 일치하지 않습니다.'), .oneOf([Yup.ref("password"), null], '비밀번호가 일치하지 않습니다.'),
answer: Yup.string()
.required('답변을 입력해주세요.'),
})} })}
onSubmit={(values, { setSubmitting }) => { onSubmit={(values, { setSubmitting }) => {
axios({ axios({
...@@ -72,7 +74,7 @@ function Signup() { ...@@ -72,7 +74,7 @@ function Signup() {
<div className="form-group mb-4"> <div className="form-group mb-4">
<input <input
className={(touched.id && errors.id ? 'form-control is-invalid' : "form-control")} className={(touched.id && errors.id ? 'form-control is-invalid' : "form-control")}
type="number" type="text"
name="id" name="id"
{...getFieldProps('id')} {...getFieldProps('id')}
placeholder="Input Student Id" placeholder="Input Student Id"
...@@ -105,6 +107,26 @@ function Signup() { ...@@ -105,6 +107,26 @@ function Signup() {
<div className="invalid-feedback text-left">{errors.password2}</div> <div className="invalid-feedback text-left">{errors.password2}</div>
) : null} ) : null}
</div> </div>
<div className="form-group mb-4">
<label>본인 확인 질문</label>
<Field as="select" name="question">
<option value="">질문을 선택하세요</option>
<option value="life">자신의 인생 좌우명은?</option>
<option value="school">자신이 다녔던 초등학교의 이름은?</option>
<option value="place">기억에 남는 추억의 장소는?</option>
</Field>
</div>
<div className="form-group mb-4">
<input
className={(touched.answer && errors.answer ? 'form-control is-invalid' : "form-control")}
type="text"
name="answer"
{...getFieldProps('answer')}
placeholder="Input answer" />
{touched.answer && errors.answer ? (
<div className="invalid-feedback text-left">{errors.answer}</div>
) : null}
</div>
<button type="submit" className="btn btn-dark" disabled={isSubmitting}> <button type="submit" className="btn btn-dark" disabled={isSubmitting}>
Sign Up Sign Up
</button> </button>
......
...@@ -4,8 +4,7 @@ import * as serviceWorker from './serviceWorker'; ...@@ -4,8 +4,7 @@ import * as serviceWorker from './serviceWorker';
import { BrowserRouter as Router, Route, Redirect, Switch } from 'react-router-dom'; import { BrowserRouter as Router, Route, Redirect, Switch } from 'react-router-dom';
import 'bootstrap/dist/css/bootstrap.css'; import 'bootstrap/dist/css/bootstrap.css';
import axios from 'axios'; import axios from 'axios';
// import { PrivateRoute } from './PrivateRoute'; import { PrivateRoute } from './Components/PrivateRoute';
import Login from './Pages/LoginPage'; import Login from './Pages/LoginPage';
import Home from './Pages/HomePage'; import Home from './Pages/HomePage';
...@@ -13,6 +12,8 @@ import Signup from './Pages/SignupPage'; ...@@ -13,6 +12,8 @@ import Signup from './Pages/SignupPage';
import Apply from './Pages/ApplyPage'; import Apply from './Pages/ApplyPage';
import Check from './Pages/CheckPage'; import Check from './Pages/CheckPage';
import Notice from './Pages/NoticePage'; import Notice from './Pages/NoticePage';
import Find from './Pages/FindPage';
import Change from './Pages/ChangePage';
axios.defaults.validateStatus = function (status) { axios.defaults.validateStatus = function (status) {
return status < 500; // default return status < 500; // default
...@@ -21,12 +22,18 @@ axios.defaults.validateStatus = function (status) { ...@@ -21,12 +22,18 @@ axios.defaults.validateStatus = function (status) {
ReactDOM.render( ReactDOM.render(
<Router> <Router>
<Switch> <Switch>
<Route exact path="/" component={Login} /> <PrivateRoute exact path="/" component={Home} />
<Route path="/login" component={Login} />
<Route path="/home" component={Home} /> <Route path="/home" component={Home} />
<Route path="/signup" component={Signup} /> <Route path="/signup" component={Signup} />
<Route path="/apply" component={Apply} /> <Route path="/apply" component={Apply} />
<Route path="/check" component={Check} /> <Route path="/check" component={Check} />
<Route path="/notice" component={Notice} /> <Route path="/notice" component={Notice} />
<Route path="/find" component={Find} />
<Route path="/change" component={Change}/>
<Redirect path="/login" to="/" />
<Redirect path="/home" to="/" />
<Redirect path="/change/:id" to="/change"/>
</Switch> </Switch>
</Router>, </Router>,
document.getElementById('root') document.getElementById('root')
......
...@@ -2,6 +2,7 @@ const express = require('express'); ...@@ -2,6 +2,7 @@ const express = require('express');
const User = require('../schemas/user'); const User = require('../schemas/user');
const bcrypt = require("bcrypt"); const bcrypt = require("bcrypt");
const jwt = require('jsonwebtoken'); const jwt = require('jsonwebtoken');
const user = require('../schemas/user');
const router = express.Router(); const router = express.Router();
...@@ -23,7 +24,7 @@ router.post('/', function (req, res, next) { ...@@ -23,7 +24,7 @@ router.post('/', function (req, res, next) {
if (err) return res.status(500).json({ error: err }); if (err) return res.status(500).json({ error: err });
if (!users) { if (!users) {
return res.status(404).json({ error: '해당 아이디가 존재하지 않습니다.' }); return res.status(404).json({ error: '해당 학번이 존재하지 않습니다.' });
} }
bcrypt.compare(req.body.password, users.password, function (err, result) { bcrypt.compare(req.body.password, users.password, function (err, result) {
...@@ -34,7 +35,7 @@ router.post('/', function (req, res, next) { ...@@ -34,7 +35,7 @@ router.post('/', function (req, res, next) {
if (result) { if (result) {
const token = jwt.sign({ const token = jwt.sign({
id:users.id, id: users.id,
}, process.env.JWT_SECRET, { }, process.env.JWT_SECRET, {
expiresIn: '1m', expiresIn: '1m',
}); });
...@@ -49,4 +50,18 @@ router.post('/', function (req, res, next) { ...@@ -49,4 +50,18 @@ router.post('/', function (req, res, next) {
}) })
}); });
router.post('/find', function (req, res, next) {
console.log('/find post request', req.body)
User.findOne({ id: req.body.id }, 'id question answer', function (err, users) {
if (err) return res.status(500).json({ error: err });
if (users.question === req.body.question) {
if (users.answer === req.body.answer) {
return res.status(201).json({users});
}
return res.status(404).json({ error: '답변이 일치하지 않습니다.'});
}
return res.status(404).json({error: '질문을 다시 선택해주세요.'});
})
});
module.exports = router; module.exports = router;
...@@ -11,13 +11,15 @@ router.post('/', function (req, res, next) { ...@@ -11,13 +11,15 @@ router.post('/', function (req, res, next) {
if (err) return res.status(500).json({ error: err }); if (err) return res.status(500).json({ error: err });
if (users) { if (users) {
return res.status(404).json({ error: '이미 존재하는 아이디입니다.' }) return res.status(404).json({ error: '이미 존재하는 학번입니다.' })
}; };
const user = new User({ const user = new User({
name: req.body.name, name: req.body.name,
id: req.body.id, id: req.body.id,
password: req.body.password, password: req.body.password,
question: req.body.question,
answer: req.body.answer,
}); });
user.save() user.save()
...@@ -31,4 +33,6 @@ router.post('/', function (req, res, next) { ...@@ -31,4 +33,6 @@ router.post('/', function (req, res, next) {
}); });
}) })
}); });
module.exports = router; module.exports = router;
...@@ -16,6 +16,12 @@ const userSchema = new Schema({ ...@@ -16,6 +16,12 @@ const userSchema = new Schema({
type: Number, type: Number,
required: true, required: true,
}, },
question: {
type: String,
},
answer: {
type: String,
},
}); });
...@@ -35,7 +41,7 @@ userSchema.pre("save", function (next) { ...@@ -35,7 +41,7 @@ userSchema.pre("save", function (next) {
} else { } else {
next(); next();
} }
}); });
module.exports = mongoose.model('User', userSchema); module.exports = mongoose.model('User', userSchema);
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