user.controller.js 3.05 KB
Newer Older
Lee SeoYeon's avatar
0111    
Lee SeoYeon committed
1
2
3
4
5
6
import User from "../models/User.js"
import isLength from 'validator/lib/isLength.js'
import isEmail from "validator/lib/isEmail.js"
import bcrypt from "bcryptjs";

const signup = async (req, res) => {
Lee SeoYeon's avatar
.    
Lee SeoYeon committed
7
    const { name, email, password } = req.body //구조분해해서 하나씩
Lee SeoYeon's avatar
.    
Lee SeoYeon committed
8
    console.log(name, email, password)
Lee SeoYeon's avatar
.    
Lee SeoYeon committed
9
    try {             //적어도 3개 맥시멈 10개 가 아니면 
Lee SeoYeon's avatar
.    
Lee SeoYeon committed
10
        if (!isLength(name, { min: 3, max: 10 })) {
Lee SeoYeon's avatar
0111    
Lee SeoYeon committed
11
            return res.status(422).send('이름은 3-10자 사이입니다')
Lee SeoYeon's avatar
.    
Lee SeoYeon committed
12
        } else if (!isLength(password, { min: 6 })) {
Lee SeoYeon's avatar
0111    
Lee SeoYeon committed
13
            return res.status(422).send('비밀번호는 6자 이상입니다')
Lee SeoYeon's avatar
.    
Lee SeoYeon committed
14
        } else if (!isEmail(email)) {
Lee SeoYeon's avatar
0111    
Lee SeoYeon committed
15
16
            return res.status(422).send('유효하지 않은 이메일 형식입니다')
        }
Lee SeoYeon's avatar
.    
Lee SeoYeon committed
17
        const user = await User.findOne({ email })
Lee SeoYeon's avatar
0111    
Lee SeoYeon committed
18
19
20
21
        if (user) {
            return res.status(422).send(`${email}이 이미 사용중입니다`)
        }
        const hash = await bcrypt.hash(password, 10)
Lee SeoYeon's avatar
.    
Lee SeoYeon committed
22
        const newUser = await new User({
Lee SeoYeon's avatar
0111    
Lee SeoYeon committed
23
24
25
            name,
            email,
            password: hash
Lee SeoYeon's avatar
.    
Lee SeoYeon committed
26
        }).save() //save하면 몽고 db에 들어간다 save method가 promise
Lee SeoYeon's avatar
0111    
Lee SeoYeon committed
27
        console.log(newUser)
Lee SeoYeon's avatar
.    
Lee SeoYeon committed
28
29
        res.json(newUser) //json형식으로 바꿔서 문자열로 보낸다 client쪽으로
    } catch (error) { //다른 과정에서 에러가 나면 실행 
Lee SeoYeon's avatar
0111    
Lee SeoYeon committed
30
31
32
33
        console.log(error)
        res.status(500).send('회원가입 에러')
    }
}
Lee SeoYeon's avatar
.    
Lee SeoYeon committed
34

Lee SeoYeon's avatar
0111    
Lee SeoYeon committed
35
const userById = async (req, res, next, id) => {
Lee SeoYeon's avatar
.    
Lee SeoYeon committed
36
    try {
Lee SeoYeon's avatar
0111    
Lee SeoYeon committed
37
38
39
40
41
        const user = await User.findById(id)
        if (!user) {
            res.status(404).send('사용자를 찾을 수 없습니다')
        }
        req.profile = user
Lee SeoYeon's avatar
.    
Lee SeoYeon committed
42
        next() //만나는 순간 다음 미들웨어로 진행 없으면 가만히 있는다
Lee SeoYeon's avatar
0111    
Lee SeoYeon committed
43
44
45
46
47
48
    } catch (error) {
        console.log(error)
        res.status(500).send('사용자 아이디 검색 실패')
    }
}

Lee SeoYeon's avatar
..    
Lee SeoYeon committed
49
const getBookmark = async (req, res) => {
Lee SeoYeon's avatar
.    
Lee SeoYeon committed
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
    const { bookmark, userId } = req.body
    console.log(bookmark, userId)
    // const bookpage = req.body.bookmark
    console.log('page:', bookmark)
    // const user = req.user.userId
    console.log('userId:', userId)
    const updatedUser = await User.findOne({ _id: userId })
    if (updatedUser) {
        updatedUser.bookmark = bookmark
        updatedUser.save()
    }
    // const updatedUser = await new User({ bookmark }).save()
    // res.json(updatedUser)
    console.log(updatedUser)
    res.send(updatedUser)
Lee SeoYeon's avatar
..    
Lee SeoYeon committed
65
66
67
68
69
70
71
72
73
}

// const putBookmark = async (req, res) => {
//     const newBookmark = new Bookmark ({
//         bookmark,
//     }).save()
//     console.log(newBookmark)
//     res.json(newBookmark)
// }
Lee SeoYeon's avatar
.    
Lee SeoYeon committed
74
75
76
77
78
79
80
81
82
83
84
85
86
87
// const bookMark = async (req, res) => {
//     const {title, url} = req.body
//     console.log(title, url)
//     try {
//         let bookmark = []
//         const newBookmark = await new User ({
//             bookmark,
//         }).save() 
//     } catch (error) {
//         console.log(error)
//         res.status(500).send('사용자 아이디 검색 실패')
//     } 
// }

Lee SeoYeon's avatar
.    
Lee SeoYeon committed
88
export default { signup, userById, getBookmark }