user.js 3.16 KB
const express = require('express');
const router = express.Router();
const bcrypt = require('bcrypt');
const passport = require('passport');
const {isLoggedIn, isNotLoggedIn} = require("./middleware");
const models = require('../models/index');
const {searchPubTransPathAJAX} = require('../crawling');

router.get('/loadMe', isLoggedIn, (req, res, next) => {
    // console.log('loadMe요청옴', req.user);
    return res.json({user: req.user});
});

router.get('/signUp', isNotLoggedIn, (req, res, next) => {
    return res.render('SignUpComponent.vue', {
        title: '회원가입'
    });
});

router.post('/signUp', isNotLoggedIn, async (req, res, next) => {
    let {email, nickName, password} = req.body;
    try {
        let user = await models.User.findOne({
            where: {email}
        });
        if (user) {
            return res.json({user});
        }

        const hashedPassword = await bcrypt.hash(password, 10);
        const signupComplete = await models.User.create({
            email, nickName, hashedPassword
        });

        user = await models.User.findOne({
            where: {email},
            attributes: ['id', 'email', 'nickName']
        });

        return req.login(user, (err) => {
            if (err) {
                console.error(err);
                return next(err);
            }
            return res.json({me: user});
        });
    } catch (e) {
        console.error(e);
        next(e);
    }
});

router.post('/login', isNotLoggedIn, (req, res, next) => {
    passport.authenticate('local', {}, (err, user, info) => {
        if (err) {
            console.error(err);
            return next(err);
        }
        if (info) {
            console.error(info.message);
            return res.status(401).send(info.message);
        }
        req.login(user, (err) => {
            if (err) {
                console.error(err);
                return next(err);
            }
            ///////////////////////// req.session.returnURL
            // nuxt
            // return res.json({user: req.user});
            return res.json({user: req.user});
        });
    })(req, res, next);
});

router.get('/profile', isLoggedIn, (req, res, next) => {
    return res.render('profile', {title: '프로필', user: req.user});
});

router.post('/updateProfile', isLoggedIn, async (req, res, next) => {
    let {newNickName} = req.body;
    await models.User.update({
        nickName: newNickName
    }, {
        where: {email: req.user.email}
    });

    let user = await models.User.findOne({
        where: {email: req.user.email}
    });
    if (!user) {
        return res.redirect('/');
    }

    return res.render('profile', {
        title: 'profile',
        user
    })
});

router.get('/deleteProfile', async (req, res, next) => {
    let email = {email: req.user.email};
    let User = await models.User.destroy({
        where: {email}
    });
    return res.redirect('/');
});


router.get('/logout', (req, res, next) => {
    console.log('로그아웃 요청이 들어옴');
    req.logout();
    req.session.destroy();
    return res.send();
});

router.get('/test', (req, res, next) => {
    searchPubTransPathAJAX();
});

module.exports = router;