Commit b29d4e3f authored by kusang96's avatar kusang96
Browse files

Merge remote-tracking branch 'origin/jiwon' into ourMaster

parents 6549ba7e f4e8de9b
function catchErrors(error, displayError) {
let errorMsg
if (error.response) {
errorMsg = error.response.data
console.log('Error response', errorMsg)
console.log(errorMsg)
} else if (error.request) {
errorMsg = error.request
console.log('Error request', errorMsg)
console.log(errorMsg)
} else {
errorMsg = error.message
console.log('Error message', errorMsg)
console.log(errorMsg)
}
displayError(errorMsg)
}
export default catchErrors
\ No newline at end of file
export default catchErrors
\ No newline at end of file
node_modules
package-lock.json
\ No newline at end of file
package-lock.json
uploads/
\ No newline at end of file
......@@ -3,9 +3,11 @@ import fs from 'fs';
import connectDb from './schemas/index.js'
import userRouter from "./routes/user.routes.js";
import productRouter from './routes/product.routes.js';
import cartRouter from './routes/cart.routes.js';
import path from 'path'
import kakaopayRoutes from './routes/kakaopay.routes.js'
import config from './config.js'
import authRouter from './routes/auth.routes.js'
import cors from 'cors'
fs.readdir('uploads', (error) => {
......@@ -25,7 +27,9 @@ app.use(express.static(path.join(process.cwd(), 'dist')))
// app.use('/', indexRouter);
app.use('/', kakaopayRoutes)
app.use('/api/users',userRouter)
app.use('/api/auth',authRouter)
app.use('/api/product', productRouter)
app.use('/api/addcart', cartRouter)
app.listen(config.port, () => {
console.info('Server started on port %s.', config.port)
......
......@@ -3,7 +3,8 @@ const config = {
port: process.env.PORT || 3001,
jwtSecret: process.env.JWT_SECRET || 'My_Secret_Key',
mongoDbUri: process.env.MONGEDB_URI || 'mongodb://localhost/shopping-mall',
kakaoAdminKey: 'b2dda7685c5b2990684d813e362cff07'
kakaoAdminKey: 'b2dda7685c5b2990684d813e362cff07',
cookieMaxAge: 60 * 60 * 24 * 7 * 1000
}
// const config = {
......
import User from '../schemas/User.js'
import bcrypt from 'bcryptjs'
import jwt from 'jsonwebtoken'
import config from '../config.js'
const login = async(req,res)=>{
const {id, password} =req.body
console.log(id,password)
try{
const user=await User.findOne({id}).select('+password')
if(!user){
return res.status(404).send(`${id}가 존재하지 않습니다.`)
}
const passwordMatch= await bcrypt.compare(password, user.password)
if(passwordMatch){
const token=jwt.sign({userId:user._id},config.jwtSecret,{
expiresIn:'3d'
})
res.cookie('token',token,{
maxAge:config.cookieMaxAge,
httpOnly:true,
secure:config.env ==='production'
})
res.json({userId:user._id})
}else{
res.status(401).send('비밀번호가 일치하지 않습니다.')
}
}catch(error){
console.log(error)
res.status(500).send('로그인 실패. 다시 시도하세요.')
}
}
const logout =(req,res)=>{
res.clearCookie('token')
res.send('로그아웃 되었습니다.')
}
export default {login, logout}
\ No newline at end of file
import Cart from "../schemas/Cart.js";
import Product from '../schemas/Product.js'
const cart = async (req, res) => {
const { userId, productObjectId, color, size, count } = req.body
// console.log('req.body=', req.body)
// const {userId, productObjectId, }
// const { user, pro_name, price, main_image } = req.body
// color, size, count, productObjectId(productlist에서 props), userId(로컬)
try {
const product = await Product.find({ _id: productObjectId })
if (product) {
// console.log(product)
const { pro_name, price, main_image } = product[0]
const products = { productObjectId, color, size, count, pro_name, price, main_image }
// console.log(products)
const newCart = await new Cart({
userId, products
}).save()
console.log(newCart)
res.json(newCart)
}
// const newCart = await new Cart({
// user, pro_name, price, stock, main_category, sub_category, main_image
// }).save()
// const asdf = await Cart.find({ user })
// console.log(newCart)
// res.json(newCart)
} catch (error) {
console.log(error)
res.status(500).send('죄송합니다. 다시 입력해 주십시오.')
}
// try {
// const user = await
// User.findById(id)
// if (!user) {
// console.log(error)
// res.status(404).send('사용자를 찾을 수 없습니다')
// }
// req.profile = user
// next()
// } catch (error) {
// console.log(error)
// res.status(500).send('사용자 아이디 실패')
// }
}
export default { cart }
\ No newline at end of file
......@@ -21,7 +21,6 @@ const regist = async (req, res) => {
const newProduct = await new Product({
pro_name, price, stock, main_category, sub_category, description, main_imgUrl, detail_imgUrls
}).save()
console.log(newProduct)
res.json(newProduct)
} catch (error) {
console.log(error)
......
......@@ -4,42 +4,34 @@ import bcrypt from 'bcryptjs';
const signup = async (req, res) => {
console.log(req.body)
console.log('req.body.name=', req.body.name)
const { name, number1, number2, id, password, password2, tel } = req.body
const { name, number1, number2, id, password, tel } = req.body
try {
if (!isLength(password, { min: 8, max: 15 })) {
return res.status(422).json({ message: '비밀번호는 8-15자리로 입력해주세요.' })
if(!isLength(password,{min:8, max:15})){
return res.status(422).send('비밀번호는 8-15자리로 입력해주세요.')
}
// if (!isLength(name, { min: 3, max: 10 })) {
// return res.status(422).send('이름은 3-10자로 입력해주세요.')
// } else if (!isLength(password, { min: 8, max: 15 })){
// return res.status(422).json({message: '비밀번호는 8-15자리로 입력해주세요.'})
// }
// const user = await User.findOne({id})
// if (user) {
// return res.status(422).send(`${id}가 이미 사용중입니다.`)
// }
const hash = await bcrypt.hash(password, 10)
const newUser = await new User({
const user=await User.findOne({id})
if(user){
return res.status(422).send(`${id}가 이미 사용중입니다.`)
}
const hash=await bcrypt.hash(password,10)
const newUser = await new User ({
name,
number1,
number2,
id,
password: hash,
password2,
tel
password:hash,
tel,
}).save()
console.log(newUser)
res.json(newUser)
} catch (error) {
console.log(error)
res.status(500).json({ message: '죄송합니다. 다시 입력해 주십시오.' })
res.status(500).send('죄송합니다. 다시 입력해 주십시오.')
}
}
const hello = (req, res) => {
res.send('Hello from users contriller')
}
export default { signup, hello }
\ No newline at end of file
export default { signup }
\ No newline at end of file
......@@ -15,6 +15,7 @@
"bcryptjs": "^2.4.3",
"cors": "^2.8.5",
"express": "^4.17.1",
"jsonwebtoken": "^8.5.1",
"mongoose": "^5.11.9",
"multer": "^1.4.2",
"node-fetch": "^2.6.1",
......
import express from "express";
import authCtrl from '../controllers/auth.controller.js';
const router = express.Router()
router.route('/login')
.post(authCtrl.login)
router.route('/logout')
.get(authCtrl.logout)
export default router
\ No newline at end of file
import express from "express";
import cartCtrl from '../controllers/cart.controller.js';
const router = express.Router()
router.route('/')
.post(cartCtrl.cart)
// .get()
export default router
\ No newline at end of file
import express from "express";
import productCtrl from '../controllers/product.controller.js';
const router = express.Router()
router.route('/regist')
.post(productCtrl.imageUpload, productCtrl.regist)
router.route('/productone')
.get(productCtrl.getProduct)
export default router
\ No newline at end of file
......@@ -5,6 +5,5 @@ const router = express.Router()
router.route('/signup')
.post(userCtrl.signup)
.get(userCtrl.hello)
export default router
\ No newline at end of file
import mongoose from 'mongoose'
const { String, Number, Array, ObjectId } = mongoose.Schema.Types
const productschema = new mongoose.Schema ({
pro_name: {
type: String,
required: true
},
price:{
type: Number,
required: true
},
main_image: {
type: String,
required: true
},
color:{
type: String,
required: true
},
size:{
type: String,
required: true
},
productObjectId: {
type: ObjectId,
required: true
}
})
const CartSchema = new mongoose.Schema({
userId: {
type: String,
// required: true
},
products : {
type: [productschema],
required: true
}
}, {
timestamps: true
})
export default mongoose.models.Cart || mongoose.model('Cart', CartSchema)
\ No newline at end of file
import mongoose, { mongo } from 'mongoose'
import mongoose from 'mongoose'
const { String, Number } = mongoose.Schema.Types
const productschema = new mongoose.Schema ({
pro_name: {
type: String,
required: true
}
})
const ProductSchema = new mongoose.Schema({
pro_name: {
type: String,
......@@ -27,6 +20,14 @@ const ProductSchema = new mongoose.Schema({
required: true,
default: 0
},
sizes: {
type: Array,
required: true
},
colors: {
type: Array,
required: true
},
main_category: {
type: String,
required: true,
......
import mongoose from 'mongoose'
import mongoose from "mongoose";
const { String } = mongoose.Schema.Types
const UserSchema = new mongoose.Schema({
name: {
type: String,
required: true,
required: true,
},
id: {
type: String,
required: true,
unique: true
unique: true,
},
password: {
type: String,
required: true,
select: false
},
confirm_password:{
number1: {
type: String,
required: true,
select: false
},
phoneNumber: {
number2: {
type: String,
required: true,
},
role: {
tel: {
type: String,
required: true,
default: 'user',
enum: ['user', 'admin', 'root']
},
birth: {
type: String,
required: true,
},
sex: {
role: {
type: String,
required: true,
default: 'user',
enum: ['user', 'admin', 'root']
}
}, {
timestamps: true
......
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