Showing
3 changed files
with
150 additions
and
0 deletions
.gitignore
0 → 100644
app.js
0 → 100644
| 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({ | ||
| 7 | + secret: 'keyboard cat', | ||
| 8 | + cookie: { | ||
| 9 | + maxAge: 60000 | ||
| 10 | + } | ||
| 11 | +})) | ||
| 12 | +app.use(bodyParser.urlencoded({ | ||
| 13 | + extended: false | ||
| 14 | +})); | ||
| 15 | +app.use(bodyParser.json()); | ||
| 16 | +var users = new Array(); | ||
| 17 | + | ||
| 18 | +users[0] = { | ||
| 19 | + "userId": 0, | ||
| 20 | + "name": "jin", | ||
| 21 | + "password": "abc", | ||
| 22 | + "isAdmin": true | ||
| 23 | +} | ||
| 24 | + | ||
| 25 | +app.put('/login', function (req, res) { | ||
| 26 | + // users 배열에서 찾도록 처리 해야 함 | ||
| 27 | + // admin 여부를 확인하여 체크 | ||
| 28 | + // req.body.id : ID | ||
| 29 | + // req.body.name : 패스워드 | ||
| 30 | + const user = req.body; | ||
| 31 | + for (var i = 0; i < users.length; i++) { | ||
| 32 | + if (users[i].userId == user.userId && users[i].password == user.password) { | ||
| 33 | + req.session.userId = users[i].userId; | ||
| 34 | + req.session.isAdmin = users[i].isAdmin; | ||
| 35 | + res.send("Login"); | ||
| 36 | + return; | ||
| 37 | + } | ||
| 38 | + } | ||
| 39 | + | ||
| 40 | + res.send("Login Failed"); | ||
| 41 | + return; | ||
| 42 | + | ||
| 43 | +}); | ||
| 44 | + | ||
| 45 | +app.put('/logout', function (req, res) { | ||
| 46 | + // Logout | ||
| 47 | + // 세션 유효 여부를 체크하고 세션 Delete | ||
| 48 | + | ||
| 49 | + if (!users[req.session.userId]) { | ||
| 50 | + res.send("Error!"); | ||
| 51 | + return ; | ||
| 52 | + } | ||
| 53 | + | ||
| 54 | + req.session.userId = null; | ||
| 55 | + req.session.isAdmin = null; | ||
| 56 | + res.send("LogOut"); | ||
| 57 | + return ; | ||
| 58 | +}); | ||
| 59 | + | ||
| 60 | + | ||
| 61 | +var auth = function (req, res, next) { | ||
| 62 | + // Session Check | ||
| 63 | + // 어드민 여부 체크 필요 | ||
| 64 | + console.log(req.session.userId, req.session.isAdmin); | ||
| 65 | + if (req.session.userId != null && req.session.isAdmin == true) | ||
| 66 | + next(); | ||
| 67 | + else | ||
| 68 | + res.send("Not Admin"); | ||
| 69 | + | ||
| 70 | + return ; | ||
| 71 | +}; | ||
| 72 | + | ||
| 73 | +app.get('/users/:userId', auth, function (req, res) { | ||
| 74 | + // get User Information | ||
| 75 | + const user = req.params; | ||
| 76 | + for (var i = 0; i < users.length; i++) { | ||
| 77 | + if (users[i].userId == user.userId) { | ||
| 78 | + res.send(users[i]); | ||
| 79 | + return ; | ||
| 80 | + } | ||
| 81 | + } | ||
| 82 | + | ||
| 83 | + res.send("Not exist user"); | ||
| 84 | + return ; | ||
| 85 | +}); | ||
| 86 | + | ||
| 87 | +app.put('/users', auth, function (req, res) { | ||
| 88 | + const user = req.body; | ||
| 89 | + for (var i = 0; i < users.length; i++) { | ||
| 90 | + if (users[i].userId == user.userId) { | ||
| 91 | + users[i] = { | ||
| 92 | + "userId": user.userId, | ||
| 93 | + "name": user.name, | ||
| 94 | + "password": user.password, | ||
| 95 | + "isAdmin": user.isAdmin | ||
| 96 | + } | ||
| 97 | + res.send("Updated"); | ||
| 98 | + return ; | ||
| 99 | + } | ||
| 100 | + } | ||
| 101 | + | ||
| 102 | + res.send("Not exist user"); | ||
| 103 | + return ; | ||
| 104 | +}); | ||
| 105 | + | ||
| 106 | +app.post('/users', auth, function (req, res) { | ||
| 107 | + user = req.body; | ||
| 108 | + users[user.userId] = { | ||
| 109 | + "userId": user.userId, | ||
| 110 | + "name": user.name, | ||
| 111 | + "password": user.password, | ||
| 112 | + "isAdmin": user.isAdmin | ||
| 113 | + } | ||
| 114 | + res.send("Added"); | ||
| 115 | +}); | ||
| 116 | + | ||
| 117 | +app.delete('/users/:userId', auth, function (req, res) { | ||
| 118 | + const user = req.params; | ||
| 119 | + for (var i = 0; i < users.length; i++) { | ||
| 120 | + if (users[i].userId == user.userId) { | ||
| 121 | + users.splice(i,1); | ||
| 122 | + res.send("Deleted"); | ||
| 123 | + return ; | ||
| 124 | + } | ||
| 125 | + } | ||
| 126 | + res.send("Not exist user"); | ||
| 127 | + return ; | ||
| 128 | +}); | ||
| 129 | + | ||
| 130 | +// 사용자 추가 시에 admin 여부도 추가해야 함 | ||
| 131 | +var server = app.listen(80); | ||
| ... | \ No newline at end of file | ... | \ No newline at end of file |
package.json
0 → 100644
| 1 | +{ | ||
| 2 | + "name": "node.js-express-assignment2", | ||
| 3 | + "version": "1.0.0", | ||
| 4 | + "description": "", | ||
| 5 | + "main": "index.js", | ||
| 6 | + "dependencies": { | ||
| 7 | + "express": "^4.17.1", | ||
| 8 | + "express-session": "^1.17.1" | ||
| 9 | + }, | ||
| 10 | + "devDependencies": {}, | ||
| 11 | + "scripts": { | ||
| 12 | + "test": "echo \"Error: no test specified\" && exit 1" | ||
| 13 | + }, | ||
| 14 | + "author": "", | ||
| 15 | + "license": "ISC" | ||
| 16 | +} |
-
Please register or login to post a comment