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