오명준

Add Login/Create account page(js)

Showing 1 changed file with 95 additions and 0 deletions
1 +var express = require('express');
2 +var app = express();
3 +var bodyParser = require('body-parser');
4 +var session = require('express-session')
5 +
6 +app.use(session({ secret: 'delicious biscuit', cookie: { maxAge: 60000 }}))
7 +app.use(bodyParser.urlencoded({ extended: false }));
8 +app.use(bodyParser.json());
9 +
10 +
11 +var users = new Array();
12 +users[0] = {
13 + "userId" : 0,
14 + "name" : "Admin",
15 + "password" : "123",
16 + "isAdmin" : true
17 +}
18 +
19 +app.get('/login', function (req, res) {
20 + // users 배열에서 찾도록 처리 해야 함
21 + // admin 여부를 확인하여 체크
22 + // req.body.id : ID
23 + // req.body.password : 패스워드
24 + if(users[req.body.id].password == req.body.password)
25 + {
26 + req.session.userId = users[req.body.id].userId;
27 + req.session.isAdmin = users[req.body.id].isAdmin;
28 + res.send("Login");
29 + }
30 + else if(users[req.body.id].password == req.body.password)
31 + res.send("PwError");
32 + else
33 + res.send("IdError");
34 +});
35 +
36 +app.get('/logout', function (req, res) {
37 + // Logout
38 + // 세션 유효 여부를 체크하고 세션 Delete
39 + if(req.session.userId != null)
40 + {
41 + req.session.destroy(function(err){});
42 + res.send("LogOut");
43 + }
44 + else
45 + res.send("Error");
46 +});
47 +
48 +var auth = function (req, res, next) {
49 + // Session Check
50 + // 어드민 여부 체크 필요
51 + if (req.session.isAdmin == true)
52 + next();
53 + else
54 + res.send("Error");
55 +
56 +};
57 +app.get('/user/:userId', auth,function (req, res) {
58 + // get User Information
59 + var userId = req.params.userId;
60 + console.log(users[userId]);
61 + res.send("OK");
62 +});
63 +app.post('/user', auth,function (req, res) {
64 + users[req.body.userId] = {
65 + "userId" : req.body.userId,
66 + "name" : req.body.name,
67 + "password" : req.body.password,
68 + "isAdmin" : false
69 + }
70 + res.send("added");
71 +});
72 +app.put('/user/:userId', auth,function (req, res) {
73 + var userId = req.params.userId;
74 + if (userId == users[userId].userId)
75 + {
76 + users[req.body.userId] = {
77 + "userId" : req.body.userId,
78 + "name" : req.body.name,
79 + "password" : req.body.password,
80 + "isAdmin" : req.body.isAdmin
81 + }
82 + console.log(users[userId]);
83 + res.send("edited");
84 + }
85 + else
86 + res.send("Error");
87 +});
88 +app.delete('/user/:userId', auth,function (req, res) {
89 + var userId = req.params.userId;
90 + delete users[userId];
91 + res.send("deleted");
92 +});
93 +// 사용자 추가 시에 admin 여부도 추가해야 함
94 +
95 +var server = app.listen(80);