-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi-server.js
More file actions
81 lines (68 loc) · 1.78 KB
/
api-server.js
File metadata and controls
81 lines (68 loc) · 1.78 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
const express = require('express')
const app = express()
const port = 3000
const bodyParser = require('body-parser')
const cookieParser = require('cookie-parser')
const jwt = require('jsonwebtoken')
app.use(cookieParser());
app.use(bodyParser.json())
const jwtKey = "abc1234567";
const members = [
{
id: 3,
name: "도서관",
loginId: "lib",
loginPw: "africa"
},
{
id: 4,
name: "홍길동",
loginId: "a",
loginPw: "1"
}
]
app.get('/api/account', (req, res) => {
if (req.cookies && req.cookies.token) {
jwt.verify(req.cookies.token, jwtKey, (err, decoded) => {
if (err) {
return res.sendStatus(401);
}
res.send(decoded);
})
}
else {
res.sendStatus(401);
}
})
app.post('/api/account', (req, res) => {
const loginId = req.body.loginId;
const loginPw = req.body.loginPw;
const member = members.find(m => m.loginId === loginId && m.loginPw === loginPw);
if (member) {
const options = {
domain: "localhost",
path: "/",
httpOnly: true
};
const token = jwt.sign({
id: member.id,
name: member.name,
}, jwtKey, {
expiresIn: "15m",
issuer: "africalib"
});
res.cookie("token", token, options);
res.send(member);
} else {
res.sendStatus(404);
}
})
app.delete('/api/account', (req, res) => {
if (req.cookies && req.cookies.token) {
res.clearCookie("token");
}
res.sendStatus(200);
})
app.listen(port, () => {
console.log(`Example app listening at http://localhost:${port}`)
})