Commit e399d413 authored by Choi Ga Young's avatar Choi Ga Young
Browse files

회원가입 서버연결

parent c83ea733
...@@ -2,8 +2,8 @@ import axios from "axios"; ...@@ -2,8 +2,8 @@ import axios from "axios";
const signup = async (user) => { const signup = async (user) => {
const url = `/api/auth/signup`; const url = `/api/auth/signup`;
const { data } = await axios.post(url, user); const { data, status } = await axios.post(url, user);
return data return {data, status}
} }
const authApi = { const authApi = {
......
import React from 'react'; import React, { useState } from 'react';
import { Formik } from 'formik'; import { Formik } from 'formik';
import * as Yup from 'yup'; import * as Yup from 'yup';
import authApi from '../../apis/auth.api';
const SignupForm = () => { const SignupForm = () => {
const [success, setSuccess] = useState(false);
return ( return (
<> <>
<div className="py-5"> <div className="py-5">
...@@ -34,9 +36,14 @@ const SignupForm = () => { ...@@ -34,9 +36,14 @@ const SignupForm = () => {
.required('학번을 입력해주세요.') .required('학번을 입력해주세요.')
.min(7, '학번을 정확히 입력해주세요.'), .min(7, '학번을 정확히 입력해주세요.'),
})} })}
onSubmit={(values, { setSubmitting }) => { onSubmit={async (values, { setSubmitting }) => {
try {
const result = await authApi.signup(values)
// console.log('회원가입 요청 후 result 확인', result, '|', result.status)
} catch (error) {
console.log(error)
}
setTimeout(() => { setTimeout(() => {
alert(JSON.stringify(values, null, 2));
setSubmitting(false); setSubmitting(false);
}, 400); }, 400);
}} }}
......
import express from 'express' import express from 'express'
import cookieParser from 'cookie-parser' import cookieParser from 'cookie-parser'
import mainRouter from './routes/index.js'
const app = express() const app = express()
app.use(express.json()) app.use(express.json())
app.use(express.urlencoded({ extended: true })) app.use(express.urlencoded({ extended: true }))
app.use(cookieParser()) app.use(cookieParser())
app.use('/api', mainRouter)
export default app export default app
\ No newline at end of file
import jwt from "jsonwebtoken";
import { User } from '../db/index.js';
const signup = async (req, res) => {
console.log('server/signup req.body', req.body)
const { userId, password, userName, userStudNum } = req.body;
try {
const findId = await User.findOne({ where: { userID: userId } });
console.log('findId type check', findId)
if (findId) {
throw new Error("이미 있는 회원정보 입니다.");
}
await User.create({
userID: userId,
password: password,
userName: userName,
studNum: userStudNum
});
res.status(201).json("success")
} catch (error) {
console.log(error)
res.status(500).send(error.message || "회원가입 에러발생")
}
// 이건 나중에 정보 있을 때 확인
}
const login = async (req, res) => {
console.log('server/login req.body', req.body)
}
export default {
signup,
login
}
\ No newline at end of file
...@@ -7,9 +7,32 @@ const UserModel = (sequelize) => { ...@@ -7,9 +7,32 @@ const UserModel = (sequelize) => {
const User = sequelize.define( const User = sequelize.define(
"user", "user",
{ {
userID:{ userID: {
type: DataTypes.STRING type: DataTypes.STRING
}, },
userName: {
type: DataTypes.STRING
},
password: {
type: DataTypes.STRING
},
studNum: {
type: DataTypes.STRING
}
} }
) );
}
\ No newline at end of file User.beforeSave(async (user) => {
// if (!user.changed("password")) {
// return;
// }
if (user.password) {
const hashedPassword = await bcrypt.hash(user.password, 10);
user.password = hashedPassword;
}
});
return User
};
export default UserModel;
\ No newline at end of file
import express from "express";
import userRouter from './user.route.js';
const router = express.Router();
router.use('/auth', userRouter)
export default router;
import express from 'express';
import userCtrl from '../controllers/user.controller.js';
const router = express.Router();
router
.route("/signup")
.post(userCtrl.signup)
router
.route("/login")
.get(userCtrl.login)
export default router;
\ No newline at end of file
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