login.js
2.23 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
var express = require('express');
var app = express();
var bodyParser = require('body-parser');
var session = require('express-session')
app.use(session({ secret: 'delicious biscuit', cookie: { maxAge: 60000 }}))
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
var users = new Array();
users[0] = {
"userId" : 0,
"name" : "Admin",
"password" : "123",
"isAdmin" : true
}
app.get('/login', function (req, res) {
// users 배열에서 찾도록 처리 해야 함
// admin 여부를 확인하여 체크
// req.body.id : ID
// req.body.password : 패스워드
if(users[req.body.id].password == req.body.password)
{
req.session.userId = users[req.body.id].userId;
req.session.isAdmin = users[req.body.id].isAdmin;
res.send("Login");
}
else if(users[req.body.id].password == req.body.password)
res.send("PwError");
else
res.send("IdError");
});
app.get('/logout', function (req, res) {
// Logout
// 세션 유효 여부를 체크하고 세션 Delete
if(req.session.userId != null)
{
req.session.destroy(function(err){});
res.send("LogOut");
}
else
res.send("Error");
});
var auth = function (req, res, next) {
// Session Check
// 어드민 여부 체크 필요
if (req.session.isAdmin == true)
next();
else
res.send("Error");
};
app.get('/user/:userId', auth,function (req, res) {
// get User Information
var userId = req.params.userId;
console.log(users[userId]);
res.send("OK");
});
app.post('/user', auth,function (req, res) {
users[req.body.userId] = {
"userId" : req.body.userId,
"name" : req.body.name,
"password" : req.body.password,
"isAdmin" : false
}
res.send("added");
});
app.put('/user/:userId', auth,function (req, res) {
var userId = req.params.userId;
if (userId == users[userId].userId)
{
users[req.body.userId] = {
"userId" : req.body.userId,
"name" : req.body.name,
"password" : req.body.password,
"isAdmin" : req.body.isAdmin
}
console.log(users[userId]);
res.send("edited");
}
else
res.send("Error");
});
app.delete('/user/:userId', auth,function (req, res) {
var userId = req.params.userId;
delete users[userId];
res.send("deleted");
});
// 사용자 추가 시에 admin 여부도 추가해야 함
var server = app.listen(80);