user.js
3.03 KB
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
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');
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();
});
module.exports = router;