users.js
2.36 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
var express = require('express');
var router = express.Router();
var models = require('../models');
var crypto = require('crypto');
router.get('/signup', function(req, res, next){
res.render('signup');
})
router.post('/signup', async function(req, res, next){
let body = req.body;
let user = await models.users.findOne({
where:{
email: body.Email
}
});
if(user){
res.json({
message: '이메일이 중복되었습니다.',
duplicate: '1'
})
}
else{
let inputPassword = body.Password;
let salt = Math.round((new Date().valueOf() * Math.random())) + "";
let hashPassword = crypto.createHash("sha512").update(inputPassword + salt).digest("hex");
console.log(`salt ${salt}`);
console.log(`hash ${hashPassword}`);
models.users.create({
email: body.Email,
password: hashPassword,
salt: salt
})
res.json({
message: '회원가입 성공!',
duplicate: '0'
})
}
//res.redirect("signup");
})
router.get('/', function(req, res, next) {
if(req.cookies){
console.log(req.cookies);
}
res.send('respond with a resource');
});
router.get('/login', function(req, res, next){
res.render('login', {
session: req.session
});
})
router.post('/login', async function(req, res, next){
let body = req.body;
let result = await models.users.findOne({
where:{
email:body.Email
}
})
if(result === undefined){
res.json({
message: '존재하지 않는 계정입니다.'
})
}
else{
let dbPassword = result.dataValues.password;
let inputPassword = body.Password;
let salt = result.dataValues.salt;
let hashPassword = crypto.createHash("sha512").update(inputPassword + salt).digest("hex");
console.log(`salt ${salt}`);
console.log(hashPassword);
console.log(dbPassword);
if(dbPassword === hashPassword){
console.log("비밀번호 일치");
req.session.email = body.userEmail;
res.json({
message: '로그인 되었습니다.',
login: '1'
});
}
else{
console.log("비밀번호 불일치");
res.json({
message: '비밀번호가 일치하지 않습니다.',
login: '0'
})
}
}
})
router.get('/logout', function(req, res, next){
req.session.destroy();
res.clearCookie('sid');
res.redirect('login');
})
module.exports = router;