login.js 1.49 KB
Newer Older
CHAERIN KIM's avatar
CHAERIN KIM committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
const express = require('express');
const User = require('../schemas/user');
const bcrypt = require("bcrypt");
const jwt = require('jsonwebtoken');

const router = express.Router();

router.get('/', function (req, res, next) {
    console.log('/login get request', req.body)
    User.find({})
        .then((signups) => {
            res.json(signups);
        })
        .catch((err) => {
            console.error(err);
            next(err);
        });
});

router.post('/', function (req, res, next) {
    console.log('/login post request', req.body)
    User.findOne({ id: req.body.id }, 'id password', function (err, users) {
        if (err) return res.status(500).json({ error: err });

        if (!users) {
            return res.status(404).json({ error: '해당 아이디가 존재하지 않습니다.' });
        }

        bcrypt.compare(req.body.password, users.password, function (err, result) {
            if (err) {
                console.log(err)
                return res.status(500).json({ error: err });
            }

            if (result) {
                const token = jwt.sign({
                    id:users.id,
                }, process.env.JWT_SECRET, {
CHAERIN KIM's avatar
CHAERIN KIM committed
39
                    expiresIn: '1m',
CHAERIN KIM's avatar
CHAERIN KIM committed
40
41
42
43
44
45
46
47
48
49
50
51
52
                });
                return res.status(201).json({
                    token,
                    users,
                });
            }
            res.status(404).json({ error: '비밀번호를 다시 입력해주세요.' });
        });

    })
});

module.exports = router;